You are on page 1of 19

i Hc S Phm Tp.

H Ch Minh

LP TRNH WEB PHP

Chng 03: PHP nng cao

Ni dung
1. 2. 3. 4. 5. Truyn, nhn d liu Cookies Session Upload File Image

1. Truyn, Nhn d liu


Trang web nhp d liu
Cch 1: truyn d liu trc tip qua url
S dng lin kt sa li url Thng qua i tng location

Cch 2: truyn d liu thng qua form


S dng i tng <form> Nhp liu thng qua cc formfield Thc hin vic truyn d liu thng qua submit Hai phng thc gi d liu GET / POST

Trang web nhn d liu (dng cc bin ton cc)


Cch 1: $_GET[bin] Cch 2: $_POST[bin] Cch 3: $_REQUEST[bin]
3

2. Khi nim cookie


L nhng mu thng tin nh c lu trn my ca ngi s dng thng dng lu tr thng tin v ngi s dng. Kh nng to cookie ph thuc vo trnh duyt v s cho php ca ngi s dng. Trong IE, xem cc cookies c to th vo: Tools/Internet Options/Setting/View Files

2. Thit lp cookie
setcookie(tn, gi_tr, t)
Tn: Tn cookie c to ra gi_tr: gi tr c t cho cookie t: S: thi gian ht hn ca cookie

Ch :
Lnh setcookie phi c gi trc khi gi bt c ni dung g v client (Trc cc th HTML, trc echo, print) thit lp thi gian ht hn ca cookie thng s dng hm time()+khong thi gian (tnh bng giy)
5

c cookie
S dng mng $_COOKIE vi ch cc s l tn cc cookie. Ch :
Ta khng th c cookie va c thit lp ngay trong trong cng 1 trang va thit lp gi setcookie.

3. S dng SESSION
s dng session, cn khai bo: session_start(); ngay u trang PHP. Khai bo bin session: $_SESSION[tn_bin] = tr; Hy b mt bin session: unset($_SESSION[tn_bin]); Hy ton b cc bin session: session_destroy();

V d: Kim tra ng nhp dng Session

V d: Kim tra ng nhp dng Session

3. SESSION
<?php // page1.php session_start(); echo 'Welcome to page #1'; $_SESSION['favcolor'] = 'green'; $_SESSION['animal'] = 'cat'; $_SESSION['time'] = time(); // Works if session cookie was accepted echo '<br /><a href="page2.php">page 2</a>'; // Or maybe pass along the session id, if needed echo '<br /><a href="page2.php?' . SID . '">page 2</a>'; ?> <?php // page2.php session_start(); echo 'Welcome to page #2<br />'; echo $_SESSION['favcolor']; // green echo $_SESSION['animal']; // cat echo date('Y m d H:i:s', $_SESSION['time']); // You may want to use SID here, like we did in page1.php echo '<br /><a href="page1.php">page 1</a>'; ?>

10

4. Upload tp tin ln server


Gi cc file ln server (upload) l mt chc nng khng th thiu i vi cc ng dng Web-based form c th upload file, ta phi xc nh thuc tnh enctype ca form c gi tr "multipart/form multipart/formdata Vi PHP, bn c th xc nh kch thc ln nht ca file upload bng cch t thm 1 hidden field c tn MAX_FILE_SIZE trc control file

11

4. Upload tp tin ln server Mng $_FILE


Element
$ FILES['taptin FILES['taptin']['name'] taptin']['name'] $_FILES['taptin $_FILES['taptin']['tmp_name'] taptin']['tmp_name'] $_FILES['taptin $_FILES['taptin']['size'] taptin']['size'] $_FILES['taptin $_FILES['taptin']['error'] taptin']['error']

Contains Name of uploaded file Path to temporary file

Example
test.gif /tmp/phprDfZvN

Size (in bytes) of uploaded file 6835 An error code corresponding to a PHP constant MIME type of uploaded file (where given by client)
UPLOAD_ERR_FO RM_SIZE image/gif

taptin']['type'] $_FILES['taptin taptin ']['type'] $_FILES['

12

4. Upload tp tin ln server M li


Constant Name
UPLOAD_ERR_OK UPLOAD_ERR_INI_SIZE

Value
0 1

Explanation No problem File size exceeds php.ini limit set in


upload_max_filesize

UPLOAD_ERR_FORM_SIZE 2

File size exceeds limit set in hidden element named MAX_FILE_SIZE File only partially uploaded File was not uploaded
13

UPLOAD_ERR_PARTIAL UPLOAD_ERR_NO_FILE

3 4

4. Upload tp tin ln server

14

4. Upload tp tin ln server

15

5. V image

V nh t vn bn

16

6. Date

17

6. Date

<?php // Assuming today is: March 10th, 2001, 5:16:18 pm $today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm $today = date("m.d.y"); // 03.10.01 $today = date("j, n, Y"); // 10, 3, 2001 $today = date("Ymd"); // 20010310 $today = date('h-i-s, j-m-y, it is w Day z '); // 05-16-17, 10-03-01, 1631 1618 6 Fripm01 $today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // It is the 10th day. $today = date("D M j G:i:s T Y"); // Sat Mar 10 15:16:08 MST 2001 $today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:17 m is month $today = date("H:i:s"); // 17:16:17 ?>

18

Cu hi v tho lun

19 19

You might also like