You are on page 1of 19

i H c S Ph m Tp.

H Ch Minh

LP TRNH WEB PHP

Chng 03: PHP nng cao

Ni dung
1. 2. 3. 4. 5. Truy n, nh n d li u Cookies Session Upload File Image

1. Truyn, Nhn d liu


Trang web nh p d li u
Cch 1: truy n d li u tr c ti p qua url
S d ng lin k t s a l i url Thng qua i t ng location

Cch 2: truy n d li u thng qua form


S d ng i t ng <form> Nh p li u thng qua cc formfield Th c hi n vi c truy n d li u thng qua submit Hai phng th c g i d li u GET / POST

Trang web nh n d

li u (dng cc bi n ton c c)

Cch 1: $_GET bi n $_GET[bi n] Cch 2: $_POST bi n $_POST[bi n] Cch 3: $_REQUEST bi n $_REQUEST[bi n]


3

2. Khi nim cookie


L nh ng m u thng tin nh c lu trn my c a ng i s d ng th ng dng lu tr thng tin v ng i s d ng. Kh nng t o cookie ph thu c vo trnh duy t v s cho php c a ng i s d ng. Trong IE, xem cc cookies c t o th vo: Tools/Internet Options/Setting/View Files

2. Thit lp cookie
setcookie(tn, gi_tr , t)
Tn: Tn cookie c t o ra gi_tr : gi tr c t cho cookie t: S : th i gian h t h n c a cookie

Ch :
L nh setcookie ph i c g i tr c khi g i b t c n i dung g v client (Tr c cc th HTML, tr c echo, print) thi t l p th i gian h t h n c a cookie th ng s d ng hm time()+kho ng th i gian (tnh b ng giy)
5

c cookie
S d ng m ng $_COOKIE v i ch cc s l tn cc cookie. Ch :
Ta khng th c cookie v a c thi t l p ngay trong trong cng 1 trang v a thi t l p g i setcookie.

3. S dng SESSION
s d ng session, c n khai bo: session_start(); ngay u trang PHP. Khai bo bi n session: $_SESSION[tn_bi n] = tr ; H y b m t bi n session: unset($_SESSION[tn_bi n]); H y ton b cc bi n 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


G i cc file ln server (upload) l m t ch c nng khng th thi u i v i cc ng d ng Web-based form c th upload file, ta ph i xc nh thu c tnh enctype c a form c gi tr "multipart/formmultipart/formdata data V i PHP, b n c th xc nh kch th c l n nh t c a file upload b ng cch t thm 1 hidden field c tn MAX_FILE_SIZE tr c control file

11

4. Upload tp tin ln server M ng $_FILE


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

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 $_FILES['taptin']['type']

12

4. Upload tp tin ln server M l i


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 t p tin ln server

15

5. V image

nh t

vn b n

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