Lap Trinh Php Va My Sql

Download Lap Trinh Php Va My Sql

Post on 12-Nov-2014

2.685 views

Category:

Technology

6 download

DESCRIPTION

 

TRANSCRIPT

1. Ban ch o cng ngh thng tin ca c quanMySQL Ban ch o cntt ca c quan ng PHP & ng ***TI liu tham khoNgn ng lp trnh pHP H ni, 2003 1 2. Ban ch o cntt ca c quan ng PHP & MySQL Phn I : Gii thiu ngn ng lp trnh PHPchng i : Ngn ng lp trnh pHP I. Gii thiu PHP v mi trng lp trnh web. 1. PHP l g ? Ci tn PHP ban u c vit tt bi cm t Personal Home Page, v c pht trin t nm 1994 bi Rasmus Lerdorf. Lc u ch l mt b c t Perl, c s dng lu du vt ngi dng trn cc trang web. Sau , Rasmus Lerdorf pht trin PHP nh l mt my c t (Scripting engine). Vo gia nm 1997, PHP c pht trin nhanh chng trong s yu thch ca nhiu ngi. PHP khng cn l mt d n c nhn ca Rasmus Lerdorf v tr thnh mt cng ngh web quan trng. Zeev Suraski v Andi Gutmans hon thin vic phn tch c php cho ngn ng ri thng 6 nm 1998, PHP3 ra i (phin bn ny c phn m rng l *.php3). Cho n tn thi im , PHP cha mt ln c pht trin chnh thc, mt yu cu vit li b c t c a ra, ngay sau PHP4 ra i (phin bn ny c phn m rng khng phi l *.php4 m l *.php). PHP4 nhanh hn so vi PHP3 rt nhiu. PHP by gi c gi l PHP Hypertext PreProcesor.2. Ti sao phi s dng PHP Nh chng ta bit, c rt nhiu trang web c xy dng bi ngn ng HTML (HyperText Markup Language). y ch l nhng trang web tnh, ngha l chng ch cha ng mt ni dung c th vi nhng dng vn bn n thun, hnh nh ,v c th c s h tr bi ngn ng JavaScript, hoc Java Apple. Nhng trang web nh vy ngi ta thng gi l client- side. Tuy nhin, Internet v Intranets c s dng cho cc ng dng cn ti c s d liu. Cc trang ng dng nh vy c gi l trang web ng, bi v ni dung ca chng lun thay i ty thuc vo d liu v ngi s dng. PHP l ngn ng lm c nhng iu nh vy. Bng cch chy chng trnh PHP trn my ch Web server, bn c th to ra cc ng dng 2 3. Ban ch o cntt ca c quan ngPHP & MySQLc s tng tc vi c s d liu to ra nhng trang web v y c gi l trang web ng. Chng ta hy xem xt cch hot ng ca trang web c vit bng ngn ng HTML v PHP nh th no.Web server Vi cc trang HTML :+ c yu cu t browser + Tm file.htm trn server Khi c yu cu ti mt trang web t + Gi li c file.htm cho browser pha ngi s dng (browser). Web serverthc hiu ba bc sau :+ c yu cu t pha browser, Request+ Tm trang web trn server. http://domain/file.htm+ Gi trang web tr li cho browser (nu tm thy) qua mng Internet hoc Intranet . Trang html Vi cc trang PHP :Khc vi cc trang HTML, khi mt trang PHP c yu cu, web server phn tch v thi hnh cc on m PHP to ra trang HTML. Web server + c yu cu t browser iu c th hin bng bn bc sau : + Tm trang web(file.php) trn server + c yu cu t pha browser.+ Thc hin cc on m PHP + Gi tr li ni dung cho browser + Tm trang web trn server. + Thc hin cc on m PHP trn trang web sa i ni dung ca trang. + Gi tr li ni dung cho browser (y l trang HTMLc th hin th c bi trnh duyt Internet Explorer hoc trnh duyt no ). Kt xut Tm li, s khc nhau gia HTML v PHP l HTML khng c thc hin trn my Requestch Web server cn cc trang *.php http://domain/file.phpvit bng cc on m PHP c thc hintrn my ch Web server do n linh ng v mm do hn .3 4. Ban ch o cntt ca c quan ngPHP & MySQL3. Nhng im mnh ca PHP-PHP thc hin vi tc rt nhanh v hiu qu .Mt Server bnh thngc th p ng c hng triu truy cp ti trong mt ngy.PHP h tr kt ni ti rt nhiu h CSDL khc nhau:PostgreSQL,mSQL,Oracle, dbm, filePro ,Hyperware, informix,InterBase,Sybase, ... Ngoi ra cn h tr kt ni vi ODBC thng qua c th ktni vi nhiu ngn ng khc m ODBC h tr.-PHP cung cp mt h thng th vin phong ph : Do PHP ngay t uc thit k nhm mc ch xy dng v pht trin cc ng dng trnweb nn PHP cung cp rt nhiu hm xy dng sn gip thc hin cccng vic rt d dng : gi, nhn mail ,lm vic vi cc cookie, v nhiuth khc na .-PHP l mt ngn ng rt d dng, d hc v n gin hn nhiu so vicc ngn ng khc nh Perl, Java. Nu bn bit ngn ng C th mivic s hon ton thun li . -PHP c th s dng c trn nhiu h iu hnh, chng ta c th vitchng trn Unix, Lunix v cc phin bn ca Windows. V c th emm PHP ny chy trn cc h iu hnh khc m khng phi sa i lim. -PHP l ngn ng m ngun m. II. Bin, hng s v kiu d liu trong PHP. 1. Kiu d liu .PHP c ba kiu d liu c bn : interger, double v string. Ngoi ra cnc cc kiu d liu khc (nhng khng phi cc kiu d liu c bn) nharrays (cc kiu d liu mng), objects (cc kiu d liu i tng).Interger l kiu chim 4 byte b nh ,gi tr ca n trong khong 2 t ti + 2 t. Kiu Double l kiu s thc ,phm vi biu din (10-308 10308). Kiu string dng cha cc gi tr bao gm cc k t v con s . V d : 2 // y l kiu interger1.0// y l kiu double 2 // y l kiu string 4 5. Ban ch o cntt ca c quan ngPHP & MySQL2 hours // y l mt kiu string khc2. Hng sHng s l nhng gi tr khng i. Chng ta thng dng hng s lu cc gi tr khng i trong sut chng trnh nh : nhit (00C), cc gi tr thi gian ch s chuyn giao gia sng ,cha ,chiu ,ti ...a. Khai bo hng s : Ta dng hm define() khai bo hng s : define(COMPANY, Phops Bicycles); define(YELLOW, #FFFF00); define(VERSION, 4); define(NL, n);Trong v d trn chng ta dng hm define() khai bo hng s NL. Hng s ny l mt th ngt dng trong HTML. Chng ta s s dng cc hng s trong PHP nh sau : echo (Employment at . COMPANY. NL); Cch vit trn cng ging nh cc vit sau: echo (Employment at Phops Bicyclesn);Ch : hng s phi ngoi hai du v . Trng hp sau l khng c hiu lc : echo (Employment at COMPANY NL);. Khi thc hin n s cho kt qu l : Employment at COMPANY NL. Hm defined() : hm ny dng kim tra xem mt hng s no c khai bo cha. V d : if ( defined (YELLOW)) { echo (n); }5 6. Ban ch o cntt ca c quan ngPHP & MySQL b. Cc hng s c nh ngha trong PHP (Built-in Constants): h tr cho ngi lp trnh, PHP cung cp sn cc hng s nh : cc bin mi trng, cc bin ca Web server Apache ... Ngi lp trnh c th s dng hm phpinfo() xem cc gi tr ny. + Hng s nguyn : y l nhng gi tr c kiu integer. V d : 10 + Hng s thc: y l nhng gi tr c kiu double. V d : 10.00 + Hng k t : y l mt xu k t t trong du ngoc n hoc kp. V d : Ngn ng lp trnh PHP. 6 7. Ban ch o cntt ca c quan ngPHP & MySQL 3. Bin v gi tr logic.+ Cng ging vi C/C++, PHP khng c khi nim TRUE v FALSE. Cc gi tr TRUE c hiu l nhng gi tr bng 1 v gi tr FALSE l nhng gi tr bng 0 hoc xu rng . + Khi s dng bin chng ta khng cn khai bo kiu . V d : $a = 1; // $a l mt bin kiu integer. $a = 1.2; // by gi $a l mt bin kiu double. $a = A ; // by gi $a li l mt bin kiu string.+ Nu nh thc hin php ton gia bin c kiu s v kiu string, PHP s coi chui l mt dy s nh sau : $str = 222B Baker Street; Ta thy bin $str c gi tr kiu string, v nu cng s 3 vi gi tr ny th : $x = 3 + $str ; // $x = 225khi bin $x nhn c gi tr 255 v PHP cng 3 vi ba s u. Nhng nu ta in gi tr ca bin $str th echo ($str);// print : 222B Baker Street Ch rng cc php ton gia s v chui ch ng khi k t u ca chui l s . + Ta cng c th lm thay i kiu gi tr ca mt bin bng cch p kiu $a = 11.2;// bin $a c kiu double $a = (int) $a;// by gi $a c kiu integer ,gi tr l 11 $a = (double) $a; // by gi $a li c kiu double, gi tr l 11.0 $b = (string) $a ; // bin $b c kiu string ,gi tr l 11Cng phi bit rng PHP t ng chuyn i kiu rt tt. Nu tht s cn thit chng ta mi phi dng cch trn.7 8. Ban ch o cntt ca c quan ngPHP & MySQL+ Cc hm lm vic vi bingettype() : hm ny tr li kiu ca mt bin no . Gi tr tr v c th l : integer double string array object class unknown type v d : if (gettype($user_input) == integer) {$age = $user_input; } settype() : hm ny p kiu cho mt bin no . Nu thnh cng hm tr v gi tr 1 (true) ,ngc li l 0 (false). v du : $a = 7.5; settype($a, integer); if (settype($a, array)){echo (Conversion succeeded. ); }else{echo (Conversion error. ); } isset() v unset() : Hm isset() kim tra mt bin c gn gi tr hay cha, hm unset() s gii phng b nh cho mt bin no . v d :8 9. Ban ch o cntt ca c quan ng PHP & MySQL$id = 323bb; if (isset($id)) {echo (D liu c gn); }else{echo (D liu cha c gn); }unset($id);if(!isset($id)) { echo (D liu c gii phng); } empty() : Cng ging hm isset(), hm empty() s tr v gi tr 1 (true) nu mt bin l rng v ngc li 0 (false). i vi bin c kiu s gi tr bng 0 c coi l rng, bin kiu string c coi l rng nu xu l xu rng. v d:echo empty($new) ; // true$new = 1;echo empty($new);// false$new = ;echo empty($new);// true$new = 0;echo empty($new);// true$new = So 323;echo empty($new);// falseunset($new);echo empty($new);// true 9 10. Ban ch o cntt ca c quan ng PHP & MySQL III. Cc ton t + Bng cc php ton s hcPhp ton ngha V d Gii thch +Php cng7+2 Thc hin php cng gia 7 v 2 : 9- Php tr 72Thc hin php tr gia 7 v 2 : 5* Php nhn7*2Thc hin php nhn gia 7 v 2 : 14/ Php chia7/2Thc hin php chia gia 7 v 2 : 3.5 %Chia d7%2 Thc hin php chia d gia 7 v 2 : 1Ta c th vit cc php ton ngn gn nh bng sau :Khi vitTng ng vi $h += $i$h = $h + $i $h -= $i$h = $h - $i $h *= $i$h = $h * $i $h /= $i$h = $h / $i $h %= $i$h = $h % $i+ Php gn :v d :$x = 1;$y = $x + 1;$length = $area / $width;10 11. Ban ch o cntt ca c quan ngPHP & MySQL + Bng cc php ton quan hPhp nghaV d Gii thich ton== So snh bng$h == Kim tra $h v $i c bng nhau < So snh nh hn $ikhng> So snh ln hn $h < $i Kim tra $h c nh hn $i khng $i Kim tra $h c ln hn $i khng >= bng$h Kim tra $h c khc $i khng $i So snh khcKim tra $h c khc $i khng So snh khc$h != $i $h $i Cc php so snh thng dng kim tra iu kin trong cc cu lnh iu khin m ta s hc bi sau .+ Bng cc php ton logicPhp ton logic cng vi ton hng to thnh biu thc logic. Biu thclogic c th c gi tr l 1 (true) hoc 0 (false) .Ton hng a Ton hng b a && ba || b !a !b 1 11 1 0 0 1 00 1 0 1 0 10 1 1 0 0 00 0 1 1+ Cc php ton vi bin kiu string . 11 12. Ban ch o cntt ca c quan ngPHP & MySQLTa s dng du . ghp hai bin kiu string vi nhau .v d : $first = Phineas; $last = Phop; $full = $first. . $last; // $full = Phineas Phop ; echo ($full); Ta c th ghp hai xu nh sau: echo ($last. s Bicycles);//print : Phops Bicycles c th chn mt bin vo trong hng c kiu string th tn bin phi trong du ng m ngoc nhn. echo (${last}s Bicycles);+ Cc php ton thao tc mc bit.Cc php ton thao tc mc bit tc ng ln tng bit ca ton hng .K hiu ngha & AND bit |OR bit ^ XOR bitBng cc php ton nh sau :&Kt qu| Kt qu ^ Kt qu1&1 11|1 11^101&0 01|0 11^010&1 00|1 10^110&0 00|0 00^00PHP cng h tr cc php dch phi v dch tri>> : dch phi> n lm cho cc bit trong $a b dch phi i n v tr. Tng t ta c php dch tri .12 13. Ban ch o cntt ca c quan ng PHP & MySQLv d : 11 >> 2 = 2 ; v : 11 (1011) >> 2 2 (0010) + Cc php ton tng gim : - Php tng : php tng (ton t tng) tng gi tr ca ton hng ln mt n v. $a ++ : $a c s dng ri mi tng ++ $a : $a tng ri mi c s dng - Php gim : tng t nh php tng, khc l gi tr b gim i mt n v. $a -- : $a c s dng ri mi gim -- $a : $a gim ri mi c s dng v d : $a = 10; // $a bng 10 $b = $a++ ; // $a bng 11 nhng $b bng 10 $a = 10; // $a bng 10 $b = -- $a ; // $a bng 9 v $b bng 9 + Php ton iu kin. ? : Php ton iu kin cng vi ton hng to nn biu thc iu kin. Ta k hiu e1 ,e2, e3 l ba ton hng. Biu thc c dng : e1 ? e2 : e3 Nu e1 != 0 th gi tr ca biu thc iu kin l e2 Nu e1 == 0 th gi tr ca biu thc iu kin l e3 v d : tm max max = $a>$b ? a : b ;13 14. Ban ch o cntt ca c quan ngPHP & MySQL+ Ton t sizeof (i tng) : Php ton sizeof cho bit kch thc (tnh bng byte ) nh m i tng chim trong b nh. i tng y c kiu l integer, double, string. v d : $a = 10; echo sizeof($a); //s in ra mn hnh l : 4 IV. Cc cu lnh iu khin 1. Lnh if_else : y l lnh r nhnh c iu kin .a. Dng 1 : if ( biu thc ) cu lnh;Cu lnh y tng ng vi mt khi lnh. Mt khi lnh c t trong du ngoc kp. ngha : + Nu biu thc khc khng ,th cu lnh c thc hin. + Nu biu thc bng khng, th cu lnh khng c thc hin b. Dng 2 : if ( biu thc )cu lnh 1; elsecu lnh 2; ngha : + Nu biu thc khc khng ,th cu lnh 1 c thc hin. + Nu biu thc bng khng, th cu lnh 2 c thc hin Ch : * Cu lnh 1 dng 2 l lnh if_else14 15. Ban ch o cntt ca c quan ngPHP & MySQL + Nu lng else bng lng If th else thuc v If gn nht theo tng cp t trong ra ngoi.V d : $a = 10; $b = 10; $c = 3; $d = 3; $e = 12; $f = 8; if($a == $b) if($c == $d) if($e == $f) $max = $e; else $max = $f; else $max = $d; else $max = $b echo $max ; //printf max = 8+ Nu lng else t hn lng If th else thuc v If ngn nht theo tng cp t trong ra ngoi. V d : Tng ng vi : * Cu lnh 2 ca dng 2 l elseif : 15 16. Ban ch o cntt ca c quan ngPHP & MySQLBt ngun t :C th vit li nh sau:if ( biu_thc1 ) if ( biu_thc1 ) cu_lnh 1;cu_lnh 1;elseelseif (biu_thc 2) if ( biu_thc 2 )cu_lnh 2;cu_lnh 2; elseif (biu_thc 3) elsecu_lnh 3;if ( biu_thc 3 ) ... cu_lnh 3;elseif (biu_thc i)...cu_lnh i;else ... if ( biu_thc i ) else cu_lnh i; cu_lnh n; ... elsecu_lnh n;Cu lnh elseif to ra lnh r nhnh c iu kin trong thchin 1 trong n cch khc nhau.- Nu biu_thc i khc khng (i = 1,..n-1) th thc hin cu lnh i . - Nu biu_thc i bng khng (i = 1, ..n-1) th cu lnh th n c thc hin.2. Cu lnh switch : switch (biu_thc n) {case n1: cu lnh 1; break;case n2: cu lnh 2; break;...case nn: cu lnh nn; [default: cu lnh] } 16 17. Ban ch o cntt ca c quan ng PHP & MySQLCu lnh switch l cu lnh r nhnh c iu kin trong thc hin mt trong n ci r nhnh. +Nu biu_thc n = ni (i = 1, .. n) th cu lnh sau case ni c thc hin cho n khi gp lnh break hoc du ngoc } +Nu biu_thc n != mi ni (i = 1, .. n) m c nhnh Default th cc cu lnh sau Default c thc hin. V d :3. Cu lnh FOR: Dng lnh :for (biu_thc 1; biu_thc 2; biu_thc 3)cu lnh ;Lnh for l lnh to chu trnh c iu kin.Bc 1 : tnh biu_thc 1Bc 2 : tnh biu_thc 2 : Nu biu_thc 2 khc 0 th thc hin cu lnh v sang bc 3. 17 18. Ban ch o cntt ca c quan ngPHP & MySQLNu biu_thc 2 bng 0 th kt thc vng for Bc 3 : tnh biu_thc 3 v quay li bc 2. + biu_thc 1, biu_thc 2, biu_thc 3 l cc thnh phn. Mi thnh phn c th gm nhiu biu thc. Khi mi biu thc c vit cch nhau mt du phy (,). +Cc biu thc c tnh ln lt t tri qua phi +Biu thc trong biu_thc 2 quyt nh thc hin thn ca for. V d :+C th vng mt bt k thnh phn no. Nu vng mt biu_thc 2 th cu lnh lun c thc hin. Mc d vng mt vn phi c du chm phy (;) V d : +Nu vng biu_thc 1 v biu_thc 3 th : for ( ; biu_thc 2 ; ) cau_lenh ; tng ng vi : while (biu_thc 2) cau_lenh ; 4. Cu lnh WHILEwhile (biu_thc)cu lnh ; 18 19. Ban ch o cntt ca c quan ng PHP & MySQLLnh while l mt lnh to chu trnh c iu kin. iu kin thc hin c kim tra u chu trnh. Bc 1 : Tnh biu thc Nu biu thc khc khng, sang bc 2 Nu biu thc bng khng, kt thc vng while Bc 2 : Thc hin cu lnh. Quay li bc 1. Ch :+ Biu thc c th bao gm nhiu biu thc. Khi cc biu thc cvit cch nhau mt du phy ,v c tnh ln lt t tri qua phi. Biuthc cui cng quyt nh thc hin cu lnh. + Khng c php vng mt biu thc + to chu trnh v tn th while(1) {...if (biu_thc) break;... } V d : $i = 11; while (--$i) {if (my_function($i) == error) {break;}++ $number; }19 20. Ban ch o cntt ca c quan ngPHP & MySQL5. Lnh DO ... WHILEDng lnh :do { cu lnh;}while (biu_thc); Lnh do ... while l lnh to chu trnh c iu kin, trong iu kin thc hin chu trnh c kim tra cui chu trnh. Hot ng : Bc 1 : Thc hin cu lnh Bc 2 : Tnh biu thc biu_thc+Nu biu thc biu_thc khc khng th quay li bc 1+Nu biu thc biu_thc bng khng th kt thc do ... while. V d : 6. Lnh breakL lnh r nhnh khng iu kin v thng dng ra khi thn ca switch, while, do ... while, for .20 21. Ban ch o cntt ca c quan ng PHP & MySQLLnh break ch cho php thot khi thn cc lnh bn trong nht cha n. 7. Lnh continueL lnh r nhnh khng iu kin .Lnh thng dng bt u li mt chu trnh mi trong cc lnh for, while, do ... while m khng cn thc hin ht ton b thn ca ca lnh to chu trnh. 8. Khai bo tin x l include v require . s dng cc on m bn ngoi, chng ta c th s dng khai bo tin x l include v require. Cho php chng ta xy dng cc hm cc hng s, v bt k on m no sau c th chn vo cc on script. Require khc include l, n c th lm thay i ni dung ca trang hin ti khi bin dch, cc trang ny dng khai bo cc bin, cc hng s hay cc on m n gin khng c vng lp. Khi include cho php thc hin cc cu lnh phc tp c cu lnh to chu trnh. N ch s dng cc hm nh nhng hm ngoi ca chng trnh.V. Hm trong PHP1. Quy tc xy dng hmfunction tn_hm (danh sch i s hnh thc) {Thn hm .} + nh ngha hm khng nht thit phi nm ngoi thn mi hm, trong hm c th c hm khc. Nhng vic s dng mt hm khng khc nhau gia xy dng hm trong thn mt hm v ngoi mi hm . + Tn hm tu t v khc tn hm chun. + Hm c th c gi tr tr v hoc khng. + Cc cu lnh c quyn gi bt k hm no c khai bo v c nh ngha. 21 22. Ban ch o cntt ca c quan ng PHP & MySQL+ Return : - Tr mt gi tr v cho ni gi hm - L ni bo kt thc hm 2. Gi hm .+ Hm phi c xy dng (khai bo) trc khi gi . + Khi gi hm, nu c gi tr tr v thng c t trong biu thc . 3. Bin ton c v bin cc b.Thng thng PHP coi cc bin c s dng trong thn ca hm l bin cc b. Ngha l bin trong thn hm khng lm thay i gi tr ca bin ngoi hm . Mun hm lm thay i gi tr ca bin ngoi ta cn khai bo global trc bin trong thn hm . V d : $position = m; function change_pos() {$position = s; } change_pos(); echo ($position);//Prints m Ta thy bin $position gi tr khng i sau khi gi hm change_pos(). $position = m; function change_pos() {global $position;$position = s; } change_pos(); echo ($position);//Prints s22 23. Ban ch o cntt ca c quan ng PHP & MySQLTa c th vit nh trn hoc ta c th vit : $position = m; function change_pos() {GOLOBALS[$position] = s; } change_pos(); echo ($position); //Prints sVI. Bin mng trong PHP1. Mng mt chiuMng l mt bin bao gm nhiu phn t c cng tn nhng khc nhau v ch s (cc ch s ny tng dn t 0 n n). Vi ngn ng lp trnh C, cc phn t ca mng c cng kiu d liu ,nhng vi PHP th mm do hn. Cc phn t ca mng khng nht thit phi cng kiu. a. Khai bo mng mt chiu. Ta c th khai bo mng bng cch gn tn mng vi du ng m ngoc vung khng c ch s. Chng ta hy xt v d sau : $countries[] = cr; $countries[] = de; $countries[] = us; V d trn to ra mt mng gm ba phn t c ch s l 0, 1 v 2. Vic cng tng t nh ta gn : $countries[0] = cr; $countries[1] = de; $countries[2] = us; Ngoi ra cc ch s ca mng khng nht thit phi tng dn m c th c khai bo nh sau : $countries[50] = cr;23 24. Ban ch o cntt ca c quan ngPHP & MySQL$countries[20] = de; $countries[10] = us; echo ($countries[20]); // prints de Khi thm mt phn t mi vo mng chng ta c th vit $countries[] = uk;// ch s s l 51 Mt phn t mi c thm vo vi ch s l ch s ln nht ca mng cng thm mt. Ngoi ra cng c th khai bo mng mt chiu bng cu lnh array $countries = array (cr, de, us) ; echo ($countries[2]);//prints us ch s khng bt u t khng ta c th khai bo li nh sau $countries = array (1 => cr, de, us); echo ($countries[2]); //prints de Ton t => c th c s dng trc bt k mt phn t no trong mng $countries = array (cr, 7 => de, us);v khi phn t c gi tr cr c ch s l 0 cn phn t c gi tr de, us ln lt l 7 v 8. Khc vi ngn ng lp trnh C, ch s ca mng mt chiu khng ch l cc s nguyn m cn l xu k t. Ta c th khai bo nh sau : $countries[ca] = Canada; $countries[cr] = Costa Rica; $countries[de] = Germany; $countries[uk] = United Kingdom; $countries[us] = United States; echo ($countries[ca]);// print Canada Nu dng array th s l : $countries = (ca => Canada,24 25. Ban ch o cntt ca c quan ng PHP & MySQLcr => Costa Rica, de => Germany, uk => United Kingdom, us => United States);25 26. Ban ch o cntt ca c quan ngPHP & MySQLb. Lm vic vi cc phn t ca mngC th s dng vng lp for xt tng phn t ca mng. $countries = array (cr, de, us); $num_elements = count($countries); for ($i = 0 ; $i < $num_elements ; $i ++) {echo ($countries[$i] n); } V d trn s ng khi ch s ca cc phn t tng dn u. Nu ch s cc phn t khng theo mt th t no c ta c th s dng hm list() v each(). reset ($countries); while (list ($key, $value ) = each ($countries)) {echo (Ch s $key, gi tr $value); } di chuyn con tr ti phn t tip theo hoc trc ta cng c thdng hm next() - hm next() tr v l 1 (true) nu phn t tip theokhng phi l phn t cui cng, ngc li l 0 (false) v prev() - hmprev() cho gi tr tr v l 1 (true) nu phn t tip theo khng phi lphn t u tin, ngc li l 0 (false). Hm key () s cho bit ch s vhm current() s cho bit gi tr ca phn t . $arr = array (3, 4, 5, 6, 7); do {$k = key ($arr);$val = current ($arr);echo (Phn t $k = $val); }while 2. Mng hai chiuChng ta c th khai bo mng hai chiu nh sau : 26 27. Ban ch o cntt ca c quan ng PHP & MySQL $countries = array (Europs => array (de, uk), North America => array (ca, cr, us));echo ($countries[Europs][1]);// print ukecho ($countries[North America][2]); // print usTa c cu trc ca mng trn nh sau : $countries[Europs]$countries[North America] [0] [1][0] [1][2]deukca crusCng ging nh mng mt chiu ta s dng vng lp nh for, while, do... while duyt qua cc phn t ca mng.V d :$countries = array (Europs => array (de, uk), North America => array (ca, cr, us));while (list ($key1) = each ($countries)) { echo ($key1 : n); while (list ($key2, $val) = each($countries[$key1])) { echo ( - $val n)}}Khi chy chng trnh s in ra mn hnh l :Europ :- de- ukNorth America :- ca- cr- us27 28. Ban ch o cntt ca c quan ngPHP & MySQL3. Cc hm sp xp mng .PHP cung cp cho chng ta cc hm sp xp mng. Vi mi loi mng s c mt hm tng ng. Vi mng c ch s l kiu nguyn chng ta c hm sort() sp xp tng dn cc phn t ca mng, hm rsort() s sp xp cc phn t ca mng gim dn . $countries = array (us, uk, ca, cr, de); sort ($countries); while (list ($key, $val) = each ($countries)) {echo (Element $key equals $val n); } Khi chy chng trnh s l : Element 0 equals ca Element 1 equals cr Element 2 equals de Element 3 equals uk Element 4 equals usVi mng ch s c kiu string th dng hm asort (), arsort () sp xp mng theo chiu tng dn v gim dn. Nu bn dng cc hm sort() v rsort() th cc ch s c kiu string s chuyn thnh cc ch s c kiu nguyn.$countries = array(us => United States, uk => United Kingdom, ca => Canada, cr => Costa Rica, de => Germany); asort ($countries); while (list($key, $val) = each($countries)) {echo Chi so $key bang $val n; 28 29. Ban ch o cntt ca c quan ngPHP & MySQL} Kt qu khi chy chng trnh : Chi so ca bang Canada Chi so cr bang Costa Rica Chi so de bang Germany Chi so uk bang United Kingdom Chi so us bang United States Nhng nu thay dng asort ($countries); bng sort ($countries); kt qu s l Chi so 0 bang Canada Chi so 1 bang Costa Rica Chi so 2 bang Germany Chi so 3 bang United Kingdom Chi so 4 bang United States sp xp mng tng dn hay gim dn theo ch s chng ta c hm ksort() tng dn v krsort() gim dn . $countries = array(e => United States, d => United Kingdom, c => Canada, b => Costa Rica, a => Germany); ksort ($countries); while (list($key, $val) = each($countries)) { echo Chi so $key bang $val n; } Kt qu : Chi so a bang Germany 29 30. Ban ch o cntt ca c quan ngPHP & MySQLChi so b bang Costa Rica Chi so c bang Canada Chi so d bang United Kingdom Chi so e bang United States VII. Lp trnh hng i tng (OOP)1. nh ngha lp .Chng ta c th nh ngha lp bng ton t class, v trong mi mt lp chng ta s xy dng cc phng thc v thuc tnh cho lp . Gi s mun nh ngha lp Sesssion chng ta lm nh sau : nh ngha thuc tnh cho lp chng ta t t kho var trc thuc tnh . Tip theo chng ta s nh ngha cc phng thc cho lp trn. Phng thc cn xy dng u tin l Session() sau n cc phng thc khc. 2. S dng lp c nh ngha. khai bo mt i tng c kiu thuc lp xy dng ta dng ton t new nh sau : Welcome to my website, > VIII. Tm hiu thm v HTML Phn mt chng ta tm hiu s lc v ngn ng HTML. Trong mc ny chng ta s trnh by thm v n vi trng tm l FORM ca HTML. L do l v khi mun giao tip gia HTML v PHP script th khng th khng bit n FORM. FORM l cng c ph bin nht chuyn d liu t HTML n PHP script (client ln server). Thng thng, khi vit mt script, ngi ta thit k mt FORM sao cho n cung cp y nhng thng tin m script cn x l. 1. Cu trc ca mt FORMMt FORM c m u bng th v kt thc bng th .Trong FORM c :- Thuc tnh : FORM thng c ba thuc tnh, ba thuc tnh ny nm trong th FORM m u ca mt FORM : action, method, enctype . - Cc th c dng trong FORM : input, select, textarea, hn, p, hr, dir, dl, menu, ol, ul, address, blockquote, [isindex]. pre . - FORM c ba thnh phn chnh : textarea, select, input . - FORM c th c dng trong cc th : blockquote,body,dd, li. 2. Cc thuc tnhKhi to mt FORM ,cng vic u tin l xc nh thuc tnh ca n. - Thuc tnh action : Action=URL ch n script m FORM s dng. Ngoi ra n cng c th l mailto url, khi ni dung ca FORM c mail n a ch trong url .33 34. Ban ch o cntt ca c quan ngPHP & MySQL- Thuc tnh method : Method c th c gn bng GET hoc POST ,n ch ra phng thc Post hay Get s c s dng trong FORM, gi tr ngm nh l Get.- Thuc tnh enctype : Enctype=Mime_type :ch ra loi d liu s gi i. Gi tr ngm nh l application/x-www-form-urlencode. Trong mt trang chng ta c th s dng nhiu FORM nhng khng c lng chng vo nhau. 2. Cc thnh phn c bn ca FORM.Mt FORM ,thng thng c ba thnh phn chnh : +Textarea +Select +Input a - Textareac bt u bng th v kt thc bng th . Bn trong cha cc k t, th ny c dng lng vo trong FORM. Cc thuc tnh ca Texterea . - Name : nh ngha tn ca thnh phn, thuc tnh ny lun lun phi c. - Rows : Cho bit s hng ca hp vn bn. - Cols : Cho bit s ct ca vn bn. Wrap : Ch ra cch x l word_wraping (cn chiu di ca dng vn bn theo kch thc ca hp vn bn) trong hp thoi. Nu wrap=off, chc nng word_wraping b cm. Nu wrap=vitual, chc nng ny c bt ln, khi g vn bn vo, ta thy con tr t ng xung hng mi khi n chy n bin ca hp vn bn, k t newline (OA hex) t ng c thm vo nhng khng c gi i cng FORM. Nu wrap=physical, chc nng ny cng c bt v hot ng nh trn nhng k t newline c gi i cng vi FORM. 34 35. Ban ch o cntt ca c quan ngPHP & MySQLVn bn nm gia hai th v s c th hin nh on vn bn ngm nh trong vng d liu. V d:.. . b. Selectc bt u bng th v kt thc bng th , cp th ny c th lng trong FORM hay bt k thnh phn no ca FORM ngoi tr Texterea v Select . Select c cc thuc tnh sau : - Name : Tn ca thnh phn.- Size : Cho bit s thnh phn s hin th, gi tr ngm nh l 1, do danh sch la chn thng c th hin di dng pop-up menu. Thuc tnh ny c th khng c. - Multiple : Nu thuc tnh ny c thit lp, nhiu la chn s c chn cng mt lc, ngc li ch c chn mt item. Khi s dng Select, chng ta c th dng thm Option . .. .. .. Bn trong cha cc k t, c th c s dng lng vo Select . Cc thuc tnh Option :- Disable : nh mt la chn b cm. Khi hin th, chn la ny s b che m. - Selected : nh du chn la ny c chn, nu thuc tnh Multiple c bt trong Select, bn c th nh du Selected nhiu chn la cng lc. N dng nh du cc la chn ngm nh. - Value : Ch ra gi tr c gn cho la chn, nu khng c th ni dung ca th mc option s c gi i thay cho value. c. Input35 36. Ban ch o cntt ca c quan ng PHP & MySQLM u bng th < Input >, th ny c th dng trong bt c thnh phn no khc ca FORM ngoi tr Texterea v Select. Cc thuc tnh :- Align : C th l mt trong ba gi tr top, middle, bottom dng cn l nh vi cc vn bn xung quanh, thuc tnh ny ch c ngha vi Type=image .- Name : gn tn bin cho d liu ca thnh phn ny. Gi tr ca thuc tnh do user la chn .- Type : nh ra mt trong nhng gi tr sau checkbox, hidden, image, password, radio, reset, submit, text, file, bottom .- Checked : ch ra mt nt radio, hay mt checkbox c c chn hay khng. - Maxlength : ch ra chiu di ti a m hp thoi vn bn c th cha, thuc tnh ny ch c ngha vi input c type=text hay type=password . - Size : ch ra kch thc thc s ca hp thoi vn bn. IX. Tm lc Nh vy, chng ta kho st hu ht cc chc nng c bn ca PHP, tcc kiu d liu, khai bo bin, hng, mng cho ti cch thc hinchng trnh dng My khch/My ch (Client/Server), v nhng ccon m PHP vo cc trang HTML .v.v. Nh trn chng ta trnh by, c s d liu cho trang Web l thnh phn khng th thiu, n ng vai tr quyt nh cho chng trnh. V l do , phn tip theo chng ta s kho st ngn ng SQL v cc hm API ca PHP thc hin cc lnh truy vn trn h qun tr c s d liu MySQL.X. Cc hm API trong PHP 1. Gii thiu v MySQL .PHP h tr mt s lng ln cc hm lm vic vi c s d liu nh Oracle, Sybase, PostgreSQL, MySQL. .. Thng qua chun ODBC (Open Database Connectivity), bng cch s dng cc hm API (Application 36 37. Ban ch o cntt ca c quan ng PHP & MySQLProgramming Interface) m PHP c th lm vic c vi nhiu h qun tr c s d liu nh vy. Nu h qun tr c s d liu khng h tr ODBC ,v hn na ODBC c c im ch h tr dng chun th PHP c th lm vic vi ODBC tng trn. Nu khng mun s dng ODBC ta c th s dng cc hm API . Trong phn ny chng ta ch s dng cc hm API lm vic vi h qun tr c s d liu MySQL . 2. Cc hm c bn lm vic vi c s d liu MySQL.a) Cc hm kt ni n MySQL ServerPHP cung cp hai hm kt ni vi c s d liu MySQL : mysql_connect v mysql_pconnect . + mysql_connect () : hm ny s to ra mt lin kt ti my ch MySQL .C php : int mysql_connect (string [hostname [:port] [:/path_to_socket]], string [username], string [password]); Trong :- hostname : Tn my ch c s d liu, ni trang web s cha c s d liu. Gi tr ngm nh l localhost- :port : a ch cng, ni b my c s d liu lng nghe yu cu. Gi trngm nh l :3306 . - :/path_to_socket : Cng ging nh :port nhng ch cho h iu hnh UNIX. Gi tr ngm nh l :/tmp/mysql.sock .- username : Tn ca ngi s dng c php kt ni vo b my c s d liu.- password : Mt khu ca ngi s dng kt ni vo b my c s d liu.Hm ny tr v m s nhn dng nu kt ni thnh cng, gi tr 0 (false) nu vic kt ni c li. M s nhn dng ny s c s dng cho tt c cc yu cu ti b my c s d liu sau ny.37 38. Ban ch o cntt ca c quan ng PHP & MySQLS kt ni ny s ng li khi gi hm mysql_close() hoc kt thc on PHP script.+ mysql_pconnect() : Hm ny to ra mt lin kt bn vng vi my ch MySQL.C php :int mysql_pconnect (string [hostname [:port] [:/path_to_socket]], string[username], string [password]); Tham s v gi tr tr v ca hm ny cng ging hm mysql_connect(). S khc bit gia hai hm ny l lin kt ti my ch MySQL khng b ng li k c khi kt thc kch bn (script) PHP hay gi hm mysql_close(). Mc ch ca hm ny l lun lun duy tr lin kt ti my ch MySQL do lun c s yu cu ti my ch, trnh cho my ch phi tm kim m s nhn dng mi t gim thi gian truy cp . Ch : hm ny ch thc hin c khi PHP c nh cu hnh nh l mt module ca Web server . + mysql_close() : Hm ny hu b s kt ni ti my ch MySQL . C php : int mysql_close(int [link_identifier]);Tham s link_identifier l m s nhn dng to ra bi hm mysql_connect(). Hm tr v l True nu thnh cng, ngc li l False . b) Cc hm thao tc trn CSDL + mysql_create_db() : Hm to c s d liuC php : int mysql_create_db(string name, int [link_identifier]) ;Trong : - string name : Tn ca c s d liu cn to.- int link_identifier : M s nhn dng c cp bi hm mysql_connect() .Chng ta hon ton c th gi cu lnh SQL to c s d liu thngqua hm mysql_query() .38 39. Ban ch o cntt ca c quan ng PHP & MySQL+ mysql_drop_db() : Hm xo c s d liuC php : int mysql_drop_db(string name, int [link_identifier]); Trong : - string name : Tn ca c s d liu cn xo .- int link_identifier : M s nhn dng c cp bi hm mysql_connect() .Chng ta hon ton c th gi cu lnh SQL xo c s d liu thngqua hm mysql_query(). + mysql_select_db() : Hm cho c s d liu hot ng .C php : int mysql_select_db(string database_name, int [link_identifier]); Trong : - database_name : Tn ca c s d liu m sau ny cc hm API khc ca PHP s thc hin trn . - int link_identifier : M nhn dng c cp bi hm mysql_connect(). Cu lnh ny s gn tn c s d liu vi m nhn dng, sau ny khi lm vic vi link_identifier s bao gm c c s d liu c chn . c) Cc hm thao tc trn d liu + mysql_query() : Hm gi cu lnh SQL ti my ch MySQL . C php : int mysql_query(string query, [int link_identifier]) ; Trong : - string query : Cu lnh SQL cn gi ti my ch MySQL .- int link_identifier : M s nhn dng, n phi c thc hin trong hm mysql_select_db() trc . + mysql_db_query() : Hm gi cu lnh SQL ti my ch MySQL .C php : 39 40. Ban ch o cntt ca c quan ngPHP & MySQL int mysql_db_query(string database, string query, int [link_identifier]); Trong : - string database : Tn c s d liu cu lnh SQL s thc hin trn . - string query : Cu lnh SQL cn thc hin . - link_identifier : M s nhn dng c cp bi hm mysql_connect()Hm ny ch r cu lnh c thc hin trn c s d liu no nn trc khng cn thc hin hm mysql_select_db(); +mysql_insert_id() : Hm ly gi tr c sinh ra t cu truy vn INSERT trc C php :int mysql_insert_id([link_identifier]) ; trong : int link_identifier : M s nhn dng c cp bi hm mysql_connect() . Hm ny tr v gi tr id c sinh ra trong ct AUTO_INCREMENT bi cu truy vn trc . iu ny ch c tc dng trn link_identifier c ch ra trong hm, nu gi hm trn m khng ch nh tham s link_identifier th lin kt c m cui cng s c ch nh. Hm mysql_insert_id() tr v gi tr 0 nu cu truy vn trc khng sinh ra mt gi tr AUTO_INCREMENT. Nu ta mun gi li gi tr cho ln sau, th phi gi hm ny ngay sau cu truy vn sinh ra gi tr .+ mysql_fetch_row() :Hm tr v mt mng l gi tr ca mt bng ghi hin ti vi ch s l s th t ca cc trng (ch s bt u t 0). Sau hm s tr ti bng ghi tip theo cho ti khi gp bng ghi cui cng hm tr v gi tr false. truy xut ti cc gi tr ca ct ta vit : tn_mng[s th t]C php : array mysql_fetch_row( int result_identifier); 40 41. Ban ch o cntt ca c quan ng PHP & MySQLTrong : result_identifier l m s tr v ca hm mysql_query() hoc mysql_db_query() . V d : + mysql_fetch_array() : Hm tr v mt mng l gi tr ca mt bng ghi hin ti, sau hm s tr ti bng ghi tip theo cho ti khi gp bng ghi cui cng hm tr v gi tr false.C php : array mysql_fetch_array( int result_identifier [, int result_type] ); Trong : result_identifier l m s tr v ca hm mysql_query() hoc mysql_db_query() . truy xut n cc thnh phn ca ct : tn_bin_mng[tn_trng]; result_type l mt hng s c th nhn cc gi tr sau: -MYSQL_NUM : ch tr li mt mng cha cc ch s l s (ging nh hm mysql_fetch_row() ) -MYSQL_ASSOC: ch tr li mt mng lin kt 41 42. Ban ch o cntt ca c quan ngPHP & MySQL -MYSQL_BOTH : tr li mng cha ng cc ch s gm c cc con s v ch s lin kt .Hm ny l s m rng ca hm mysql_fetch_row(). N cho php truy cp trng d liu ca mng kt qu khng ch thng qua cc ch s l cc s m chng c th l tn ca cc trng d liu. iu ny lm cho vic lp trnh n gin v chnh xc hn. V d: + mysql_fetch_object() : Hm tr v mt i tng l gi tr ca mt bng ghi hin thi. Sau hm s tr ti bng ghi tip theo cho ti khi gp bng ghi cui cng hm tr v gi tr false. truy xut ti cc gi tr ca ct ta vit tn_object->tn_ct .C php : object mysql_fetch_object(int result_identifier); Trong : result_identifier l m s tr v ca hm mysql_query() hoc mysql_db_query() . V d : 42 43. Ban ch o cntt ca c quan ng PHP & MySQL +mysql_fetch_assoc() :ly v mt dng kt qu nh l mt mng lin kt . c php: array mysql_fetch_assoc(int result_identifier) Trong : result_identifier l m s tr v ca hm mysql_query() hoc mysql_db_query() . Hm tr v mt mng tng ng vi mt bn ghi c ly v v tr li FALSE nu khng c bn ghi no. Hm ny tng ng vi hm array mysql_fetch_array() vi tham s result_type l : MYSQL_ASSOC v d : 43 44. Ban ch o cntt ca c quan ngPHP & MySQL+mysql_data_seek() Di chuyn con tr bn trong tp kt qu (c c sau khi cu truy vn SELECT c thc hin) C php: bool mysql_data_seek(int result_identifier, int row_number);Trong : result_identifier l m s tr v ca hm mysql_query(), mysql_db_query(), mysql_list_tables(), mysql_list_dbs() .row_number l ch s ca bn ghi m cn t con tr vo .Hm tr v true nu thnh cng, false nu li . Hm ny s di chuyn con tr bn trong tp kt qu (c ch r bi tham i result_identifier) n dng c m bng tham i row_number.Cc dng trong tp kt qu c bt u t 0V d: 44 45. Ban ch o cntt ca c quan ngPHP & MySQL+ mysql_num_rows() :tr li s dng trong result_identifier (ni cha kt qu ca cu lnh SQL c thc hin) c php: mysql_num_rows(int result_identifier) ; Trong : result_identifier l m s tr v ca hm mysql_query(), mysql_db_query(), mysql_list_tables(), mysql_list_dbs() . +mysql_affected_rows() c php : int mysql_affected_rows(int [link_identifier]) ; Trong :int link_identifier l m s nhn dng, n phi c thc hin trong hm mysql_select_db() trc . Hm tr v s dng b tc ng bi mt cu truy vn SQL :INSERT, UPDATE, DELETE trc theo tham s link_identifier. Nu link_identifier khng c ch nh th m kt ni trc s c ch nh. Ch : - Nu cu lnh SQL trc l DELETE m khng c mnh WHERE th ton b cc bn ghi trong bng b xo nhng hm mysql_affected_rows() s tr v gin tr 0. -Hm ny khng c tc dng i vi cu lnh truy vn SELECT. ly c s dng tr v (s dng b tc ng) bi cu lnh SELECT ta dng hm mysql_num_rows(). +mysql_result() : ly d liu t result_identifier c php : mixed mysql_result(int result_identifier, int row, mixed [field]);Trong : result_identifier l m s tr v ca hm mysql_query(), mysql_db_query(), mysql_list_tables(), mysql_list_dbs() . row l bn ghi m ta s ly d liu field l trng trong dng row m ta s ly d liu . Cc tham s result_identifier v row phi c, cn tham field l ty chn. Hm s tr li cc ni dung ca dng row v ct field t tp kt qu c ch nh bi bin result_identifier. Nu i s field khng c ch nh r th trng tip theo ca bn ghi s c tr v . 45 46. Ban ch o cntt ca c quan ng PHP & MySQLV d: +mysql_free_result() : Hm gii phng vng b nh c lin kt vi result_identifier . c php: mysql_free_result(int result_identifier) ;Trong : result_identifier l m s tr v ca hm mysql_query(), mysql_db_query(), mysql_list_tables(), mysql_list_dbs() .Hm ny ch c dng nu nh bn nh gi thy rng kch bn ca bn s dng qu nhiu b nh khi ang chy. Gi hm ny trn mt trnh x l kt qu s gii phng ton b d liu lin kt trong b nh . Ngoi ra cn cc hm khc: string mysql_tablename (int result_identifier, int i) Hm tr li tn ca bng/csdl ti ch s i trong result_identifier. string mysql_field_name (int result_identifier, int field_index) Hm tr li tn ca trng ti v tr field_index trong m result_identifier int mysql_list_dbs ([int link_identifier]) Hm tr li mt result_identifier l danh sch bin CSDL trn MySQL Server nu thnh cng, li tr v false . int mysql_list_tables (string database [, int link_identifier]) Hm tr v danh sch tt c cc bng trong mt CSDL MySQL, thnh cng tr v mt result identifier, gi tr false nu c li .46 47. Ban ch o cntt ca c quan ngPHP & MySQL int mysql_list_fields (string database_name, string table_name [, int link_identifier]) Hm tr v thng tin lin quan n mt bng d liu. int mysql_num_fields (int result_identifier) Tr v s trng trong tp kt qu . int mysql_num_rows (int result_identifier) Tr v s bn ghi trong tp kt qu, hm ny ch c gi i vi cc cu lnh SELECT , ly li s bn ghi c tr li t cc lnh :INSERT, UPDATE hoc DELETE, dng mysql_affected_rows(). string mysql_field_type (int result_identifier, int field_index) Hm tr v kiu d liu ca trng ti v tr field_index trong m result_identifier . int mysql_field_len (int result_identifier, int field_offset) Hm tr v di ca trng c ch nh thng qua tham s field_offset . array mysql_fetch_lengths (int result_identifier) tr v mt mng tng ng vi cc di ca mi trng trong bn ghi c ly v bi hm mysql_fetch_row() hoc false nu c li. int mysql_errno ([int link_identifier]) Hm tr v m li t hm thao tc CSDL MySQL trc ,tr v gi tr 0 nu khng c li . string mysql_error ([int link_identifier]) Hm tr v xu thng bo li t hm thao tc CSDL MySQL trc, tr v xu rng nu khng c li . object mysql_fetch_field (int result_identifier [, int field_offset]) Ly thng tin v trng t tp kt qu ri tr li nh mt i tng. 47 48. Ban ch o cntt ca c quan ng PHP & MySQLXI. Session v Cookie 1. Session l g ? HTTP l giao thc khng c xy dng theo cch c th lu gi c trng thi gia hai ln giao dch. Khi mt ngi dng yu cu truy nhp mt trang Web, ri sau ngi dng ny li tip tc yu cu truy nhp i vi trang Web khc th HTTP khng th bit c rng l hai yu cu t cng mt ngi dng. tng ca vic iu khin phin lm vic l c th lu vt ca mt ngi dng trong sut mt phin lm vic. Nu chng ta lm c iu ny th s d dng cung cp mt truy nhp cho ngi dng, t ta c th lu vt trng thi ca ngi dng v c th thc hin vic mua bn trn mng. Session trong PHP c iu khin bi mt gi tr ID duy nht gi l sessionID, gi tr ny s c t ng sinh ra v m ha. SessionID c sinh ra bi PHP v c lu tr pha client trong sut mt phin giao dch. N c th c lu tr trn cc Cookie my ngi dng hay truyn ln cc URL. SessionID c tc dng nh mt kho bn c th ng k nhng bin c bit gi l bin session. Ni dung ca nhng bin ny c cha trn Server. SessionID l nhng thng tin ch thy c pha client. Nu ti thi im no ca mt kt ni n trang Web ca bn, sessionID c th thy c trn cookie hay URL, bn c th truy nhp nhng bin session ch trn Server phin lm vic . 2) Cookie l g ? Cookie l nhng mu tin nh m trang script c th cha trn cc my khch (client). Bn c th thit lp mt cookie trn mt my ngi dng bng cch gi mt HTTP header c cha d liu theo dng sau: Set-Cookie:Name=VALUE;[expires=DATE;][path=PATH;] [domain=DOMAIN_NAME;][secure] cu lnh ny s to ra mt cookie c tn gi l NAME vi gi tr l VALUE. Trng expires s thit lp ngy m cookie s ht hiu lc, path v domain c th c s dng ch nh cc URL (ni m cookie s c gi i). T 48 49. Ban ch o cntt ca c quan ng PHP & MySQL kho secure c ngha l cookie s khng gi i trn qu mt kt ni HTTP chun. Khi mt browser kt ni ti mt URL, trc tin n kim tra cc cookie c lu tr trn my. Nu c bt k mt cookie no c lin quan n a ch URL va c kt ni, chng s c truyn tr li cho server. 3) Thit lp cc cookie t PHP Ta c th thit lp cc cookie trong PHP bng cch s dng hm: int setcookie (string name [,string value [,int expire [,string path [,string domain [, int secure]]]]]);Nhng tham i ca hm tng ng vi nhng tham i ca Set-Cookie header trn. Nu ta thit lp cookie nh sau:setcookie ("TestCookie", "Test Value"); th khi ngi dng n thm trang k tip trong site ca ta (hoc reload trang hin ti) ta s phi truy nhp vo bin vi tn l "TestCookie" c cha gi tr l "Test Value", ta ch c th truy nhp ti n thng qua bin mng $HTTP_COOKIE_VARS [] ca PHP . Ta c th xo mt cookie bng cch gi li hm setcookie() vi tham i nh sau: tn ca cookie l tn ca cookie cn xo v khng c trng gi tr. 4) S dng kt hp cookie vi session i vi cookie c mt s vn sau y: Mt vi webrowser khng th truy cp c ti cc cookie (khng h tr cookie) v mt s ngi dng khng c cc cookie trn browser ca h. y l l do PHP s dng c hai cch thc :cookie v URL method. Khi s dng PHP session, ta s khng phi thit lp cc cookie, nhng hm session s lu gi nhng thng tin ny cho chng ta. xem ni dung ca cc cookie c thit lp bi session ta s dng hm: session_get_cookie_params(). Hm ny s tr v mt mng lin kt m cc phn t ca mng cha cc thng tin nh: lifetime, path, domain,.. .49 50. Ban ch o cntt ca c quan ng PHP & MySQL thit lp cc tham s cho session cookie ta dng hm: void session_set_cookie_params (int lifetime [, string path [, string domain]])5) Lu gi sessionID PHP s s dng cc cookie mc nh cng vi session. Nu c th c, mt cookie s c thit lp cha SessionID. Mt cch s dng cc SessionID trn URL l dch PHP cng vi la chn enable-tran-sid. Cch na l ta c th a sessionID vo trong th link. SessionID c cha trong hng SID. lm c iu ny, ta thm vo cui th link hng SID dng n nh l phng thc GET. V d: