Professional Documents
Culture Documents
H Ch Minh
Ni dung
1. 2. 3. 4. 5. Truy n, nh n d li u Cookies Session Upload File Image
Trang web nh n d
li u (dng cc bi n ton c c)
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();
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
11
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
12
Value
0 1
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
14
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