Lap trinh php va my sql

Download Lap trinh php va my sql

Post on 12-Nov-2014

635 views

Category:

Technology

4 download

DESCRIPTION

 

TRANSCRIPT

1. Ban ch o cng ngh thng tin ca c quanMySQLBan ch o cntt ca c quan ng PHP &ng *** TI liu tham khoNgn ng lp trnh pHPH ni, 20031 2. Ban ch o cntt ca c quan ng PHP & MySQL Phn I : Gii thiu ngn ng lp trnh PHP chng 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, vc pht trin t nm 1994 bi Rasmus Lerdorf. Lc u ch l mt b ct 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 thchca nhiu ngi. PHP khng cn l mt d n c nhn ca RasmusLerdorf v tr thnh mt cng ngh web quan trng. Zeev Suraski vAndi Gutmans hon thin vic phn tch c php cho ngn ng rithng 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 chnhthc, 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). PHP4nhanh hn so vi PHP3 rt nhiu. PHP by gi c gi l PHP HypertextPreProcesor. 2. Ti sao phi s dng PHP Nh chng ta bit, c rt nhiu trang web c xy dng bi ngnng HTML (HyperText Markup Language). y ch l nhng trang webtnh, ngha l chng ch cha ng mt ni dung c th vi nhng dng vnbn 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 cnti 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 sdng. PHP l ngn ng lm c nhng iu nh vy. Bng cch chychng 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 gil trang web ng.Chng ta hy xem xt cch hot ngca trang web c vit bng ngn ng HTMLv PHP nh th no. Web serverVi cc trang HTML :+ c yu cu t browser+ Tm file.htm trn serverKhi c yu cu ti mt trang web t + Gi li c file.htm cho browserpha ngi s dng (browser). Web server thc 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 chobrowser (nu tm thy)qua mng Internet hoc Intranet . Trang htmlVi cc trang PHP : Khc vi cc trang HTML, khi mt trang PHP c yu cu, webserver phn tch v thi hnh cc on m PHP to ra trang HTML. Web server+ c yu cu t browseriu 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 sai ni dung ca trang.+ Gi tr li ni dung cho browser (y l trang HTML c th hin th c bi trnh duyt Internet Explorerhoc trnh duyt no ). Kt xut Tm li, s khc nhau gia HTML v PHPl HTML khng c thc hin trn myRequest ch Web server cn cc trang *.php http://domain/file.php vit bng cc on m PHP c thc hin trn my ch Web server do n linh ng v mm do hn .3 4. Ban ch o cntt ca c quan ngPHP & MySQL 3. Nhng im mnh ca PHP -PHP thc hin vi tc rt nhanh v hiu qu .Mt Server bnh thng c 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 kt ni vi nhiu ngn ng khc m ODBC h tr. -PHP cung cp mt h thng th vin phong ph : Do PHP ngay t u c thit k nhm mc ch xy dng v pht trin cc ng dng trn web nn PHP cung cp rt nhiu hm xy dng sn gip thc hin cc cng vic rt d dng : gi, nhn mail ,lm vic vi cc cookie, v nhiu th khc na . -PHP l mt ngn ng rt d dng, d hc v n gin hn nhiu so vi cc ngn ng khc nh Perl, Java. Nu bn bit ngn ng C th mi vic s hon ton thun li . -PHP c th s dng c trn nhiu h iu hnh, chng ta c th vit chng trn Unix, Lunix v cc phin bn ca Windows. V c th em m PHP ny chy trn cc h iu hnh khc m khng phi sa i li m. -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 cn c cc kiu d liu khc (nhng khng phi cc kiu d liu c bn) nh arrays (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 tti + 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 interger 1.0// y l kiu double2 // y l kiu string 4 5. Ban ch o cntt ca c quan ngPHP & MySQL2 hours // y l mt kiu string khc 2. 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 gitr 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 sNL. 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 chiu lc : echo (Employment at COMPANY NL);. Khi thc hin n s chokt 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 : ccbin mi trng, cc bin ca Web server Apache ... Ngi lp trnh c th sdng 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 hockp.V d : Ngn ng lp trnh PHP.6 7. Ban ch o cntt ca c quan ngPHP & MySQL3. 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 lnhng 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 nyth :$x = 3 + $str ; // $x = 225 khi bin $x nhn c gi tr 255 v PHP cng 3 vi ba s u.Nhng nu ta in gi tr ca bin $str thecho ($str);// print : 222B Baker StreetCh rng cc php ton gia s v chui ch ng khi k t u cachui l s .+ Ta cng c th lm thay i kiu gi tr ca mt bin bng cch pkiu$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 11 Cng phi bit rng PHP t ng chuyn i kiu rt tt. Nu tht scn thit chng ta mi phi dng cch trn. 7 8. Ban ch o cntt ca c quan ngPHP & MySQL+ Cc hm lm vic vi bin gettype() : hm ny tr li kiu ca mt bin no . Gi tr tr v cth l : integerdoublestringarrayobjectclassunknown typev d :if (gettype($user_input) == integer){ $age = $user_input;}settype() : hm ny p kiu cho mt bin no . Nu thnh cnghm 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 trhay 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 sgi tr bng 0 c coi l rng, bin kiu string c coi l rng nu xu lxu 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);// false unset($new); echo empty($new);// true9 10. Ban ch o cntt ca c quan ng PHP & MySQLIII. 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 : 1 Ta 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 thichton == 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 $iSo snh khcKim tra $h c khc $i khngSo snh khc$h != $i$h $iCc php so snh thng dng kim tra iu kin trong cc cu lnhiu khin m ta s hc bi sau . + Bng cc php ton logic Php ton logic cng vi ton hng to thnh biu thc logic. Biu thc logic c th c gi tr l 1 (true) hoc 0 (false) . Ton hng a Ton hng b a && ba || b !a !b1 11 1 0 01 00 1 0 10 10 1 1 00 00 0 1 1 + Cc php ton vi bin kiu string .11 12. Ban ch o cntt ca c quan ngPHP & MySQL Ta 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 binphi 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 bit Bng cc php ton nh sau :&Kt qu| Kt qu ^ Kt qu 1&1 11|1 11^10 1&0 01|0 11^01 0&1 00|1 10^11 0&0 00|0 00^00 PHP cng h tr cc php dch phi v dch tri >> : dch phi > n lm cho cc bittrong $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)>> 22 (0010)+ Cc php ton tng gim :- Php tng : php tng (ton t tng) tng gi tr ca ton hng lnmt 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 mtn v.$a -- : $a c s dng ri mi gim-- $a : $a gim ri mi c s dngv 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 : e3Nu e1 != 0 th gi tr ca biu thc iu kin l e2Nu e1 == 0 th gi tr ca biu thc iu kin l e3v d : tm maxmax = $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 itng 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 : 4IV. Cc cu lnh iu khin1. 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 ct 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 hinb. Dng 2 :if ( biu thc ) cu lnh 1;else cu 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 hinCh :* 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 nhttheo 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 = $becho $max ; //printf max = 8 + Nu lng else t hn lng If th else thuc v If ngn nhttheo 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 & MySQL Bt 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 ) elsecu_lnh i; cu_lnh n;...else cu_lnh n; Cu lnh elseif to ra lnh r nhnh c iu kin trong thc hin 1 trong n cch khc nhau. - Nu biu_thc i khc khng (i = 1,..n-1) th thc hin culnh i .- Nu biu_thc i bng khng (i = 1, ..n-1) th cu lnh thn 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 thchin mt trong n ci r nhnh.+Nu biu_thc n = ni (i = 1, .. n) th cu lnh sau case ni cthc hin cho n khi gp lnh break hoc du ngoc }+Nu biu_thc n != mi ni (i = 1, .. n) m c nhnh Default thcc 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 1 Bc 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 forBc 3 : tnh biu_thc 3 v quay li bc 2.+ biu_thc 1, biu_thc 2, biu_thc 3 l cc thnh phn. Mi thnhphn c th gm nhiu biu thc. Khi mi biu thc c vit cch nhaumt 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 2th cu lnh lun c thc hin. Mc d vng mt vn phi c du chmphy (;)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 hinc kim tra u chu trnh.Bc 1 : Tnh biu thcNu biu thc khc khng, sang bc 2Nu biu thc bng khng, kt thc vng whileBc 2 : Thc hin cu lnh.Quay li bc 1.Ch : + Biu thc c th bao gm nhiu biu thc. Khi cc biu thc c vit cch nhau mt du phy ,v c tnh ln lt t tri qua phi. Biu thc cui cng quyt nh thc hin cu lnh.+ Khng c php vng mt biu thc+ to chu trnh v tn thwhile(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 kinthc hin chu trnh c kim tra cui chu trnh.Hot ng :Bc 1 : Thc hin cu lnhBc 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 caswitch, 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 chan.7. Lnh continueL lnh r nhnh khng iu kin .Lnh thng dng bt u limt chu trnh mi trong cc lnh for, while, do ... while m khng cn thchin 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 khaibo tin x l include v require. Cho php chng ta xy dng cc hm cchng 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 hinti khi bin dch, cc trang ny dng khai bo cc bin, cc hng shay cc on m n gin khng c vng lp. Khi include cho phpthc hin cc cu lnh phc tp c cu lnh to chu trnh. N ch sdng 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 khcnhau 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 hm2. 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 lbin cc b. Ngha l bin trong thn hm khng lm thay i gi tr cabin ngoi hm . Mun hm lm thay i gi tr ca bin ngoi ta cnkhai bo global trc bin trong thn hm .V d :$position = m;function change_pos(){ $position = s;}change_pos();echo ($position);//Prints mTa 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 s 22 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 khcnhau v ch s (cc ch s ny tng dn t 0 n n). Vi ngn ng lp trnhC, cc phn t ca mng c cng kiu d liu ,nhng vi PHP th mm dohn. 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 mngoc 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 thc 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 deKhi thm mt phn t mi vo mng chng ta c th vit$countries[] = uk;// ch s s l 51Mt phn t mi c thm vo vi ch s l ch s ln nht ca mngcng thm mt. Ngoi ra cng c th khai bo mng mt chiu bng culnh 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 deTon t => c th c s dng trc bt k mt phn t no trongmng$countries = array (cr, 7 => de, us); v khi phn t c gi tr cr c ch s l 0 cn phn t c gi trde, us ln lt l 7 v 8. Khc vi ngn ng lp trnh C, ch s ca mngmt chiu khng ch l cc s nguyn m cn l xu k t. Ta c th khaibo nh sau :$countries[ca] = Canada;$countries[cr] = Costa Rica;$countries[de] = Germany;$countries[uk] = United Kingdom;$countries[us] = United States;echo ($countries[ca]);// print CanadaNu 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 scc phn t khng theo mt th t no c ta c th s dng hm list() veach().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 th dng hm next() - hm next() tr v l 1 (true) nu phn t tip theo khng phi l phn t cui cng, ngc li l 0 (false) v prev() - hm prev() cho gi tr tr v l 1 (true) nu phn t tip theo khng phi l phn t u tin, ngc li l 0 (false). Hm key () s cho bit ch s v hm 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);}while2. 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 uk echo ($countries[North America][2]); // print us Ta c cu trc ca mng trn nh sau : $countries[Europs]$countries[North America][0] [1][0] [1][2] deukca crus Cng 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 - uk North America : - ca - cr - us 27 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 loimng s c mt hm tng ng. Vi mng c ch s l kiu nguyn chng tac hm sort() sp xp tng dn cc phn t ca mng, hm rsort() s spxp 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 caElement 1 equals crElement 2 equals deElement 3 equals ukElement 4 equals us Vi mng ch s c kiu string th dng hm asort (), arsort () spxp mng theo chiu tng dn v gim dn. Nu bn dng cc hm sort() vrsort() th cc ch s c kiu string s chuyn thnh cc ch s c kiunguyn. $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 CanadaChi so cr bang Costa RicaChi so de bang GermanyChi so uk bang United KingdomChi so us bang United StatesNhng nu thay dng asort ($countries); bng sort ($countries); ktqu s lChi so 0 bang CanadaChi so 1 bang Costa RicaChi so 2 bang GermanyChi so 3 bang United KingdomChi so 4 bang United States sp xp mng tng dn hay gim dn theo ch s chng ta c hmksort() 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 Germany29 30. Ban ch o cntt ca c quan ngPHP & MySQLChi so b bang Costa RicaChi so c bang CanadaChi so d bang United KingdomChi so e bang United StatesVII. Lp trnh hng i tng (OOP)1. nh ngha lp .Chng ta c th nh ngha lp bng ton t class, v trong mi mtlp 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 thuctnh .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 thckhc.2. S dng lp c nh ngha. khai bo mt i tng c kiu thuc lp xy dng ta dng tont 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. Trongmc ny chng ta s trnh by thm v n vi trng tm l FORM caHTML. L do l v khi mun giao tip gia HTML v PHP script th khngth khng bit n FORM. FORM l cng c ph bin nht chuyn dliu t HTML n PHP script (client ln server).Thng thng, khi vit mt script, ngi ta thit k mt FORM sao cho ncung 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 nmtrong 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 sdng. Ngoi ra n cng c th l mailto url, khi ni dung ca FORMc 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 hocPOST ,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 sgi i. Gi tr ngm nh l application/x-www-form-urlencode.Trong mt trang chng ta c th s dng nhiu FORM nhng khngc lng chng vo nhau.2. Cc thnh phn c bn ca FORM.Mt FORM ,thng thng c ba thnh phn chnh :+Textarea+Select+Inputa - 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 phic.- 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 vnbn theo kch thc ca hp vn bn) trong hp thoi. Nu wrap=off, chcnng 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 nbin ca hp vn bn, k t newline (OA hex) t ng c thm vo nhngkhng c gi i cng FORM. Nu wrap=physical, chc nng ny cngc bt v hot ng nh trn nhng k t newline c gi i cng viFORM.34 35. Ban ch o cntt ca c quan ngPHP & MySQLVn bn nm gia hai th v s c th hinnh on vn bn ngm nh trong vng d liu.V d: .. . b. Selectc bt u bng th v kt thc bng th , cpth ny c th lng trong FORM hay bt k thnh phn no ca FORMngoi 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. Thuctnh ny c th khng c.- Multiple : Nu thuc tnh ny c thit lp, nhiu la chn s cchn 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 bche m.- Selected : nh du chn la ny c chn, nu thuc tnhMultiple c bt trong Select, bn c th nh du Selected nhiu chn lacng lc. N dng nh du cc la chn ngm nh.- Value : Ch ra gi tr c gn cho la chn, nu khng c th nidung ca th mc option s c gi i thay cho value.c. Input 35 36. Ban ch o cntt ca c quan ng PHP & MySQLM u bng th < Input >, th ny c th dng trong bt c thnhphn 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 viType=image . - Name : gn tn bin cho d liu ca thnh phn ny. Gi tr cathuc 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 haykhng.- 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, t cc kiu d liu, khai bo bin, hng, mng cho ti cch thc hin chng trnh dng My khch/My ch (Client/Server), v nhng cc on m PHP vo cc trang HTML .v.v.Nh trn chng ta trnh by, c s d liu cho trang Web l thnhphn 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 caPHP thc hin cc lnh truy vn trn h qun tr c s d liu MySQL.X. Cc hm API trong PHP1. Gii thiu v MySQL .PHP h tr mt s lng ln cc hm lm vic vi c s d liu nhOracle, Sybase, PostgreSQL, MySQL. .. Thng qua chun ODBC (OpenDatabase Connectivity), bng cch s dng cc hm API (Application36 37. Ban ch o cntt ca c quan ng PHP & MySQLProgramming Interface) m PHP c th lm vic c vi nhiu h qun trc s d liu nh vy. Nu h qun tr c s d liu khng h tr ODBC ,vhn na ODBC c c im ch h tr dng chun th PHP c th lm vicvi ODBC tng trn. Nu khng mun s dng ODBC ta c th s dngcc hm API .Trong phn ny chng ta ch s dng cc hm API lm vic vi hqun 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 Server PHP 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 chMySQL . 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 sd liu. Gi tr ngm nh l localhost - :port : a ch cng, ni b my c s d liu lng nghe yu cu. Gi tr ngm nh l :3306 .- :/path_to_socket : Cng ging nh :port nhng ch cho h iu hnhUNIX. Gi tr ngm nh l :/tmp/mysql.sock . - username : Tn ca ngi s dng c php kt ni vo b my cs d liu. - password : Mt khu ca ngi s dng kt ni vo b my c sd 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 ttc 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 thcon PHP script. + mysql_pconnect() : Hm ny to ra mt lin kt bn vng vi mych 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 hmmysql_connect(). S khc bit gia hai hm ny l lin kt ti my chMySQL khng b ng li k c khi kt thc kch bn (script) PHP hay gihm mysql_close(). Mc ch ca hm ny l lun lun duy tr lin kt timy ch MySQL do lun c s yu cu ti my ch, trnh cho my ch phitm 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 nhl 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 hmmysql_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 liu C 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 hmmysql_connect() . Chng ta hon ton c th gi cu lnh SQL to c s d liu thng qua hm mysql_query() . 38 39. Ban ch o cntt ca c quan ng PHP & MySQL+ mysql_drop_db() : Hm xo c s d liu C 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 hmmysql_connect() . Chng ta hon ton c th gi cu lnh SQL xo c s d liu thng qua 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 APIkhc 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 khilm 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 tronghm 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 nntrc khng cn thc hin hm mysql_select_db();+mysql_insert_id() :Hm ly gi tr c sinh ra t cu truy vn INSERT trcC php : int mysql_insert_id([link_identifier]) ;trong :int link_identifier : M s nhn dng c cp bi hmmysql_connect() .Hm ny tr v gi tr id c sinh ra trong ct AUTO_INCREMENTbi cu truy vn trc . iu ny ch c tc dng trn link_identifier cch ra trong hm, nu gi hm trn m khng ch nh tham s link_identifierth 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 sinhra 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 ls th t ca cc trng (ch s bt u t 0). Sau hm s tr ti bng ghitip theo cho ti khi gp bng ghi cui cng hm tr v gi tr false. truyxut 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 hms tr ti bng ghi tip theo cho ti khi gp bng ghi cui cng hm tr vgi 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 (gingnh hm mysql_fetch_row() )-MYSQL_ASSOC: ch tr li mt mng lin kt41 42. Ban ch o cntt ca c quan ngPHP & MySQL -MYSQL_BOTH : tr li mng cha ng cc ch s gm c cc cons v ch s lin kt . Hm ny l s m rng ca hm mysql_fetch_row(). N cho php truycp trng d liu ca mng kt qu khng ch thng qua cc ch s l cc sm chng c th l tn ca cc trng d liu. iu ny lm cho vic lptrnh n gin v chnh xc hn.V d:+ mysql_fetch_object() : Hm tr v mt i tng l gi tr ca mt bng ghihin thi. Sau hm s tr ti bng ghi tip theo cho ti khi gp bng ghi cuicng hm tr v gi tr false. truy xut ti cc gi tr ca ct ta vittn_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 linkt .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 liFALSE nu khng c bn ghi no. Hm ny tng ng vi hmarray mysql_fetch_array() vi tham s result_type l : MYSQL_ASSOCv 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 vnSELECT 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 bitham i result_identifier) n dng c m bng tham i row_number. Cc dng trong tp kt qu c bt u t 0 V d: 44 45. Ban ch o cntt ca c quan ngPHP & MySQL+ mysql_num_rows() :tr li s dng trong result_identifier (ni cha ktqu 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 hintrong 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. Nulink_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 thton 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 cs dng tr v (s dng b tc ng) bi cu lnh SELECT ta dnghm mysql_num_rows().+mysql_result() : ly d liu t result_identifierc 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 liufield l trng trong dng row m ta s ly d liu .Cc tham s result_identifier v row phi c, cn tham field l tychn. Hm s tr li cc ni dung ca dng row v ct field t tp kt quc ch nh bi bin result_identifier. Nu i s field khng c chnh 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 viresult_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 cabn s dng qu nhiu b nh khi ang chy. Gi hm ny trn mt trnh xl 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_identifierint mysql_list_dbs ([int link_identifier])Hm tr li mt result_identifier l danh sch bin CSDL trn MySQL Servernu 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 cngtr v mt result identifier, gi tr false nu c li . 46 47. Ban ch o cntt ca c quan ngPHP & MySQLint mysql_list_fields (string database_name, string table_name [, intlink_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 lnhSELECT , ly li s bn ghi c tr li t cc lnh :INSERT, UPDATEhoc 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 mresult_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 cly 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 nukhng c li .string mysql_error ([int link_identifier])Hm tr v xu thng bo li t hm thao tc CSDL MySQL trc, tr vxu 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 Cookie1. Session l g ?HTTP l giao thc khng c xy dng theo cch c th lu gi ctrng thi gia hai ln giao dch. Khi mt ngi dng yu cu truy nhp mttrang Web, ri sau ngi dng ny li tip tc yu cu truy nhp i vitrang Web khc th HTTP khng th bit c rng l hai yu cu t cngmt ngi dng. tng ca vic iu khin phin lm vic l c th lu vt ca mt ngidng trong sut mt phin lm vic.Nu chng ta lm c iu ny th s d dng cung cp mt truy nhp chongi dng, t ta c th lu vt trng thi ca ngi dng v c th thchin vic mua bn trn mng.Session trong PHP c iu khin bi mt gi tr ID duy nht gi lsessionID, gi tr ny s c t ng sinh ra v m ha. SessionID csinh 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 ccURL.SessionID c tc dng nh mt kho bn c th ng k nhng bin cbit 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 imno ca mt kt ni n trang Web ca bn, sessionID c th thy ctrn cookie hay URL, bn c th truy nhp nhng bin session ch trnServer 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 cchgi 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 domainc 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 & MySQLkho secure c ngha l cookie s khng gi i trn qu mt kt ni HTTPchun.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 chURL va c kt ni, chng s c truyn tr li cho server.3) Thit lp cc cookie t PHPTa c th thit lp cc cookie trong PHP bng cch s dng hm:int setcookie (string name [,string value [,int expire [,string path [,stringdomain [, int secure]]]]]);Nhng tham i ca hm tng ng vi nhng tham i ca Set-Cookieheader 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 tranghin ti) ta s phi truy nhp vo bin vi tn l "TestCookie" c cha gi trl "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 nhsau:tn ca cookie l tn ca cookie cn xo v khng c trng gi tr.4) S dng kt hp cookie vi sessioni vi cookie c mt s vn sau y:Mt vi webrowser khng th truy cp c ti cc cookie (khng h trcookie) v mt s ngi dng khng c cc cookie trn browser ca h. yl 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 hmsession s lu gi nhng thng tin ny cho chng ta. xem ni dung ca cc cookie c thit lp bi session ta s dnghm:session_get_cookie_params(). Hm ny s tr v mt mng lin kt m ccphn 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 [, stringdomain]])5) Lu gi sessionIDPHP s s dng cc cookie mc nh cng vi session. Nu c th c, mtcookie s c thit lp cha SessionID.Mt cch s dng cc SessionID trn URL l dch PHP cng vi lachn enable-tran-sid.Cch na l ta c th a sessionID vo trong th link. SessionID c chatrong hng SID. lm c iu ny, ta thm vo cui th link hng SID dng n nh l phng thc GET.V d: