Está en la página 1de 58

Chng IV Nghin cu PHP, Mysql v cng ngh AJAx

Phn A : Gii thiu PHP C th on c vic khp nhau l mt tnh hung rt nhy cm ."my password" khng ging nh "My password" .Hy c gng thay i nhng gi tr khc ng nhp vo pass v cch vn hnh ca ngn ng lp trnh khi khai pass khng ging nhau. Ngoi tnh nng so snh trn PHP thm 2 tnh nng mi cho bit nu 2 gi tr bng nhau nhng cng phi kim tra khi chng cng kiu. Nhng kiu ny c quan h mt Thit vi nhau tr khi chng s dng 1 tn hiu ging nhau: Operand === Name Ging nhau Khng ging nhau Sample $Var $var2 $Var $var2 === Description Gi tr 1 bng gi tr 2 v thuc cng mt dng c ng khng? Gi tr 1 khc gi tr 2 v thuc cng mt dng c ng khng?

!==

!==

Php ton Logic : iu g s xy ra nu mun xem xt hn nhiu iu kin bit n ng hay sai ? Quay li v d ca mt user login buc kim tra chc chn xem tn ca user v pass c khp nhau hay khng ta c th lm nh sau : <?Php $storedusername = "user"; $enteredusername = "user"; $storedpassword = "mypassword"; $enteredpassword = "mypassword"; if ($storedusername == $enteredusername AND $storedpassword == $enteredpassword) { print "The entered username and password were correct!"; } else { print "The entered username and password were wrong!"; } ?> K t "AND" nm gia hai iu kin cho thy ngn ng lp trnh phi kim tra c hai phn ca cu lnh l ng . Nu ng th kt qu s c thng bo : "The entered username and password were correct!" PHP cng h tr mt s tnh nng khc cho phng thc Logic ny

Operand AND OR XOR && ||

Name And Or Xor And Or

Sample $Var $var2 $Var $var2 $Var $var2 $Var $var2 AND OR XOR &&

Description Gi tr l True nu c 2 u l true True nu mt trong 2 l true True nu 2 bin khc gi tr True nu c 2 u true True nu mt trong 2 l true Ph nh gi tr ca $Var . True nu gi tr ca $Var = False , l False nu gi tr ca $Var = True.

$Var || $var2

Not

!$Var

Sau khi xem bng ny chng ta s bn khon ti sao lai c hai tnh nng khc nhau ca AND v OR.Mt phn ca cu tr li ny nm trong phng thc yu tin Php ton u tin : Xem v d sau : $X = 9 + 1 * 10 Php nhn thng c u tin trc php cng v th phi thc hin nh sau : ( 1 nhn 10 cng 9 =19) Cc phng thc yu tin tng t cng c ng dng trong chng trnh PHP .Hy xem biu di y xem cc th t php tnh.Cc php tnh c lit k cng mt hng c u tin nh nhau: Th t u tin cao nht */% +-. < <= > >= && || And Xor Th t u tin thp nht Or C th dng ngoc n cho php tnh c u tin thc hin trc

$X = (9 + 1) * 10 Nh vy l php tnh trong ngoc c thc hin trc v kt qu =100 Cui cng : Trong bi vit ny ti mi ch tm tt s lc nhng phng thc vn hnh chung nht thng c s dng trong chng trnh PHP .C c rt nhiu cc tnh nng khc m ta c th khm ph ra ng dng trong ngn ng lp trnh Tip theo chng ta s im qua cc tnh nng khc v cch iu khin cu trc da vo cc nh ngha c gii thiu trn . Bin trong PHP: Bin trong PHP c th hin bi du dollar $ v theo sau l tn ca bin. Tn bin khng phn bit ch hoa hay ch thng. Tn bin phi bt u bng mt ch ci hay mt du gch ni_, theo sau l cc ch ci, ch s hay l du gch ni. V d: var = "Bob"; $Var = "Joe"; echo "$var, $Var"; Trong PHP3 th cc bin phi lun lun c gn gi tr. Cn trong PHP4 th cung cp thm mt cch khc gn gi tr cho bin: gn theo tham chiu. iu ny c ngha l mt bin mi c th tham chiu n mt bin nguyn thu. S thay i ca bin mi s tc ng n bin nguyn thu v ngc li. N cng c ngha l khng c sao chp, do vic kt gn s din ra nhanh hn. thc hin gn bng tham chiu, ch cn thm & u tn ca bin c gn.V d sau s in ra dng 'My name is Bob' hai ln: <?Php $foo = 'Bob'; // gn gi tr 'Bob' cho $foo $bar = &$foo; // Tham chiu $foo qua $bar. $Bar = "My name is $bar"; // thay i $bar... echo $bar; echo $foo; // $foo cng b thay i. ?> Phm vi ca bin: Cc bin trong PHP c th l mt trong bn loi phm vi sau: - Bin cc b. - Tham s cc hm. - Bin ton cc. - Bin static. A)Bin cc b:

Mt bin c khai bo trong mt hm th c xem nh l mt bin cc b v n ch c tham chiu trong hm . Bt k vic gn gi tr bn ngoi hm u xem nh l bin hon ton khc vi bin trong hm . Ch khi thot khi hm m bin cc b c khai bo, th bin v gi tr ca n s b hu b. Bin cc b c thun li bi n loi b nhng kh nng ca cc tc ng khng d on c lm thay i kt qu t cc bin c th truy cp ton cc.V d: $x = 4; function assignx () { $x = 0; print "\$x inside function is $x. "; } assignx(); print "\$x outside of function is $x. "; Sau khi thc hin s c cc kt qu nh sau: $x inside function is 0. $X outside of function is 4. B)Tham s ca hm: Cng ging nh nhiu ngn ng lp trnh khc, trong PHP hm c cha tham s phi khai bo cc tham s trong phn u ca hm. Mc du, nhng tham s ny nhn cc gi tr n t bn ngoi hm, nhng chng s ch c th truy cp mt khi hm tn ti. Cc tham s ca hm c khai bo sau tn hm v bn trong du {. V d: function x10 ($value) { $value = $value * 10; return $value;} iu quan trng cn phi ch l mc du c th truy cp v thao tc vi cc tham s trong hm m n c khai bo, nhng n s b hu b khi hm kt thc thc hin. C)Bin ton cc: ngc li vi bin cc b, bin ton cc c th c truy cp t bt c phn no trong chng trnh .Tuy nhin, c th chnh sa, bin ton cc phi c khai bo ton cc trong hm m n c chnh sa. khai bo mt bin l ton cc ta t t kho GLOBAL pha trc. V d: $somevar = 15; function addit() { GLOBAL $somevar; $somevar++; print "Somevar is $somevar";

} addit(); Kt qu ca $somevar l 16. Tuy nhin, nu b dng lnh GLOBAL $somevar; th bin $somevar s ch c gi tr l 1, bi bin $somevar c xem nh l bin cc b trong hm addit( ). Mt cch khc khai bo mt bin l ton cc l s dng mng $GLOBALS ca PHP. V d, khai bo bin $somevar l ton cc bng cch s dng mng ny: $somevar = 15; function addit() { $GLOBALS["somevar"]; $somevar++; } addit(); print "Somevar is $somevar"; D)Bin static: Tng phn vi cc bin c khai bo trong cc tham s ca hm s b hu b khi thot khi hm, bin static s khng mt gi tr ca n khi thot khi hm v s gi nguyn gi tr khi hm c gi li ln na. C th khai bo mt bin l static bng cch dng t kho STATIC t trc tn bin. V d: function keep_track() { STATIC $count = 0; $count++; print $count; print " "; } keep_track(); keep_track(); keep_track(); Kt qu l : 1 2 3 E) Truy cp bin t trnh duyt: Khi dng PHP, cng nh cc phn mm trung gian khc, thng tin c cung cp tu chn da vo tham chiu ngi dng. D nhin thng tin s n thng qua

form. Thng tin cng c th n t nhng ni khc, nh cookie hay sessions. *)Bin form: Mt trong nhng cch ph bin l chuyn bin thng qua form. Mi thnh phn trong form ca chng ta s c gn tn v thuc tnh gi tr. Khi form c submit th name=value s c chuyn n PHP. Chng ta c th chuyn n PHP bi phng php GET hoc POST, tu thuc chng ta chn g trong thuc tnh hnh ng ca form. Mt khi form c submit, nhng thnh phn ca form s t ng tr thnh nhng bin ton cc trong PHP. *)Sessions: PHP cng ging nh ASP v ColdFusion u h tr sessions. Vy sessions l g? V c bn n l mt cch khc duy tr trng thi gia cc trang Web. Chng ta bt u mt sessions bi hm start_session(). Khi PHP s ng k mt session ID duy nht v gi session ID v cho ngi dng thng qua cookie. PHP s to nhng tp p tr trn server c th lu gi du vt ca bt k bin no. Nhng tp ny c cng tn vi session ID. Mt khi session c to chng ta c th ng k bt k bin s no. Gi tr ca n s c gi trn mt tp my ch. V nh vy trong thi gian sng ca session nhng bin ny s c thc hin bi bt c trang no trong cng mt tn min m khng cn truy cp n chng. Tuy nhin c mt s ngi dng khng cho php cookie, khi PHP gi du vt cc session ID qua cc querystring. Chng ta c th lm iu ny bng tay bng cch cho sessiong ID ph thuc queryString, hoc thay i tu chn cu hnh. thm session Id n querystring, ta dng . N s t ng in ra mt chui nh sau: PHPSESSID=07e935k3kkjr0986s9d89fr9trg8rgrg20 >Click my Page on m sau s ng k mt session ID v gn cho n gi tr l: hello world <?Php session_start(); session_register(my_var); $my_var=hello world; ?> *)Cookies: Cookies l mt mu thng tin nh c lu tr trn my tnh ngi dng. Mt cookies Cha mt on vn bn nh c th c bi my ch Web. Cookies cung cp cch c th gi du vt ngi s dng thng qua mt s dch v. Chng ta phi nh rng Web l mi trng trng thi. My ch Web s khng bit ai yu cu thng tin, cookies s gip chng ta gi thng tin ngi dng khi h di chuyn quanh site. Khi h tn ti , cookies tr thnh mt phn ca yu cu HTTP gi n

cho my ch Web. Nhng trc ht chng ta cn thit lp mt cookies. Nhng ngi pht trin s lm iu ny, ging nh mi th khc trong PHP, chng rt n gin. Dng hm setcookie() vi nhng tham s theo sau: Setcookie(name, value, time_to_expire, path, domain, security setting); V d: setcookie(mycookie, my_id, time()+ (60*60*24*30),/,.Mydomain.Com,0) Cookie ny thit lp nhng tham s nh sau: - Lu tr tn bin c tn l mycookie. - Gi tr ca bin l my_id. - Cookie c gi tr 30 ngy k t ngy thit lp. - Cookie c hiu lc trong mi trang ca domain. - N c hiu lc trong mi ch ca tn min mydomain.Com - Khng c thit lp an ton c bit *)Hng s (Constant): Hng l mt gi tr khng th chnh sa c thng qua vic thc hin chng trnh. C th nh ngha mt hng bng cch dng hm define( ). Mt khi hng c nh ngha, n khng bao gi b thay i. Ch c cc kiu d liu boolean, integer, float, string mi c th cha cc hng. C th nhn gi tr ca mt hng bng cch n gin ch nh tn ca n. Khng ging nh vi bin, khng cn khai bo $ trc mt hng. Cng c th dng hm constant(), c gi tr ca hng. S dng hm get_defined_constants( ) nhn mt danh sch tt c cc hm c nh ngha. Cc hng c nh ngha trc: PHP cung cp mt lng ln cc hng c nh ngha trc bt k script no cng c th chy c. Bin gn t trnh duyt (web browser) Quan im chung ca vic s dng PHP cng nh cc ngn ng khc l cung cp kh nng nhp thng tin theo mun ca khch. Thng thng cc thng tin ny c nhp vo thng qua mt form HTML. Nhng cng c th chng xut pht t cc ngun khc nh: HTML, cookie, session. Bin t Form ca HTML Dng thng thng nht Khch c th nhp thng tin ring l thng qua mt form HTML. Hy to trang sign.Php ch cha 100% m lnh HTML nh sau (c th t l sign.Htm cng c): <Form action=mypage.Php action=post> <Input type=text name=email> <Input type=text name=first_name> <Input type=submit name=submit value=OK>

<Input type=submit name=reset value=Cancel> </Form> Mt khi khch nhp chut vo nt SUBMIT (chp nhn) th cc bin nh $email $First_name, v $submit s c chuyn giao sang trang action l mypage.Php. Sau , trong trang mypage.Php s x l cc bin ny tu thuc vo mc ch chng trnh. rng phn ln cc ng dng trn y u s dng gi tr ca nt lnh SUBMIT. Trong trang mypage.Php phi vit cc lnh x cc thao tc ca ngi truy cp. Hy xem cch x l trong trang mypage.Php mu nh sau: <?Php If (isset($submit) && $submit=="OK") { Echo " Lap trinh PHP that la hay."; } else { ?> <Form action=mypage.Php action=post> <Input type=text name=email> <Input type=text name=first_name> <Input type=submit name=submit value=OK> <Input type=submit name=reset value=Cancel> </form> <?Php } ?> Bn hy xem k v d trn, nu nh ngi truy cp nht thng tin v nhn nt OK t trang sign.Php (cha ton m lnh html), th n chuyn sang trang mypage.Php v xut ra dng thng bo: Lap trinh PHP that la hay. Ngc li, nu

nh nhn nt Cancel th n s thc hin m lnh trong lnh Else v s hin th form buc nhp li. Bin sn c : C rt nhiu bin sn c ca PHP v Server. C th lit k mt danh sch y bng cch s dng lnh phpinfo() xem. Hy to mt file php v cho chy th xem: <?Php Phpinfo(); ?> Ta c th s dng cc bin ny bng nhiu cch thc khc nhau. Mt s bin n t PHP engine, mt s khc bt ngun t Webserver. Bin sn c ca PHP PHP_SELF Bin ny nhn gi tr l a ch hin ti ca tp tin .Php ang c duyt. a ch ny s l a ch y t gc (bt u t http://) . S s dng n khi mun truy cp li chnh trang web ang thi thnh. Xt v d sau, y l mt form tng t nh form sign.Php c dp xt qua. Nu khch thc hin thao tc khc vi submit th chnh form ny s c thi hnh li: <? If(isset($submit)) { //Xuat ra thong bao tai day Echo "Cam on ban da submit"; } else { ?> <Form name=myform method=post action=<?=$PHP_SELF?>> <Input type=text name=first_name> first name<br> <Input type=text name=last_name> last name<br> <Input type=submit name=submit value=submit>

</Form> <? } ?> HTTP_POST_VARS y l mt mng cha tt c cc bin c chuyn tip thng qua POST method t mt form. C th truy cp tng bin ring r nh l mt phn t ca mng lin hp (v d: $PHP_POST_VARS["myname"]). HTTP_GET_VARS y l mt mng cha tt c cc bin c chuyn tip thng qua GET method. C th truy cp tng bin ring r nh l mt phn t ca mng lin hp (v d: $PHP_GET_VARS["myname"]). HTTP_COOKIE_VARS Tt c cc cookie chuyn n trnh duyt u c th c truy xut trong mng lin hp ny. N bao gm c session cookie. Nu cn thc mc cookie s thi hnh nh th no th hy xem hm phpinfo() bit c trnh duyt ang chuyn n server nhng g. Isset( ) Hm thc hin kim tra bin c cha gi tr hay khng: Hm ny thc hin vic kim tra bin c cha gi tr hay khng. N s tr v gi tr TRUE hoc FALSE. Nu bin cha c xc lp th tr isset() s l false. Bn hy xem xt v d sau, n thi hnh mt query MySQL. bit rng mt field trong database c th cha tr null hoc chui rng. Vi vic s dng hm isset() s kim tra v phn bit c hai gi tr trn. Trong on lnh PHP bn di. Trong bin $query l mt pht biu SELECT ly d liu submit t form ca user. $Result = mysql_query($query) or Die (mysql_error()); $Number_cols = mysql_num_fields($result); Echo "<b>query: $query</b><br>\n"; //Layout table header Echo "<table border = 1>\n"; Echo "<tr align=center>\n"; For ($i=0; $i<$number_cols; $i++) { Echo "<th>", mysql_field_name($result, $i), "</th>\n"; } Echo "</tr>\n";//end table header

//Layout table body While ($row = mysql_fetch_row($result)) { Echo "<tr align=left>\n"; For ($i=0; $i<$number_cols; $i++) { Echo "<td>"; If (!Isset($row[$i])) //test for null value {echo "NULL";} Else {echo $row[$i];} Echo "</td>\n"; } Echo "</tr>\n"; } Echo "</table>"; Lu rng du chm than (!) c ngha l ph nh. Tc l nu $var c gi tr null th: Isset($var) cho ra gi tr False !Isset($var) cho ra gi tr True Empty() Hm empty() c v ngc ngo so vi hm isset(). N s cho ra tr True nu $var c tr null, chui rng hoc s 0. Hm ny thng c s dng kim tra xem user c nhp tr vo trong form hay khng: If(empty($first_name)) { Echo "Ban can phai nhap ten cua minh"; Exit; } Is_int( ) Hm ny kim tra bin c phi l s nguyn hay khng. C 2 c php khc cho cng kt qu nh n l: is_integer v is_long(). S dng hm ny khi khng chc rng bin l mt tr nguyn hay chui. V d: $A = "222"; $B = 22; Is_int($a) cho ra tr False Is_int($b) cho ra tr True Tng t s c mt lot hm kim tra kiu ca bin sau y: Is_double() Kim tra s kiu double (du phy ng). Hm thay th: is_float() v is_real().

Is_string( ) Kim tra kiu chui. Is_array( ) Kim tra kiu mng. Is_bool( ) Kim tra kiu boolean (TRUE v FALSE) Is_object( ) Kim tra bin kiu object. S tm hiu kiu object trong cc phn sau. Gettype( ) Hm ny s cho bit kiu ca bin nh: string, double, integer, array, hoc boolean. Ngoi ra n c tr v cc kiu nh object, class. S kho st k v vic lp trnh hng i tng trong cc phn sau bit thm v object v class.Lu tr ca hn gettype() tr v lun l mt chui: "string", "integer", "double" v.V. Bn hy xem v d sau: $Str = "Day la mot chuoi"; $Type = gettype($str); If ($type == "string") { Echo "Dung vay"; } i kiu ca bin (p kiu) Bn s s dng 3 cch i kiu ca bin. Phng php type casting Phng php ny rt n gin: ch cn ghi tn kiu ra, ng Ngoc n li, ri t trc bin. Tc khc bin s b i theo kiu m mun. Cch thc: (kiu) $bin V d: $A = 1; $B = (string)$a; //s 1 s bin thnh chui 1 Echo gettype($a), "<br>\n"; Echo gettype($b), "<br>\n"; Kt qu cho ra l: Integer String S dng hm settype( ) Hm ny c 2 i s. Th nht l tn bin, th nh l kiu. u im ca n l n c th cho ra kt qu FALSE nu nh vic hon i khng c. Cch thc: settype($bin, "kiu") V d: $A = 1; Settype($a, "string"); S dng hm intval( ), doubleval( ), v stringval( ) Phng php ny thng p dng nhanh trong khi tnh ton. C l nhn tn hm cng bit c chc nng ca n ri. Hy xt v d sau:

$A = "43";/ /43 l kiu chui $B = (intval($a) * 2); Hm: y l mt trong nhng thun li nht trong pht trin ng dng. Cc hm cho php pht trin vic s dng li v d dng chnh sa cc thnh phn m tht s hu ch khi Pht trin cc ng dng Web tng t nh trong quan nim v tin ch. Cc kt qu ca hm rt ngn gn, d hiu v d c. Vy hm l g? Hm l mt on cc m lnh vi mt mc ch c th v phi c gn mt tn duy nht. Tn hm c th c gi ti bt k u trong chng trnh, cho php cc on m th hin bi tn ca n c thc hin lp li khi cn thit. y l mt thun li bi cng mt on m lnh ch c vit mt ln nhng c th c chnh sa d dng khi cn thit. To mt hm PHP l mt qu trnh n gin. C th to mt hm ti bt k ni no trong chng trnh PHP. Tuy nhin, cho mc ch t chc c th thy rng s thun li khi t tt c cc hm c d nh s s dng trong script ti u mi file script. Mt phng thc khc cho vic t chc hm m c th gim i s d tha ng k v tng vic s dng li l t cc hm trong nhng file ring r( c xem nh l mt th vin). iu ny l thun li bi c th dng cc hm lp i lp li trong nhng ng dng khc nhau m khng to ra cc vic copy d tha v do gim bt cc nguy c gy li do vic vit li. Mt hm thng bao gm ba phn phn bit: Tn ca hm. Cp du ngoc ( ) cha cc tham s tu chn nhp vo. Phn thn ca hm, nm trong cp du { }. V d khai bo mt hm sau: function display_copyright($site_name) { print "Copyright &copy 2000 $site_name. All Rights Reserved."; } Cc hm lng nhau: Cc hm c th lng nhau. iu ny tht s hu ch cho cc chng trnh ln v nh, khi n thm vo mt mc modul ho khc vo trong ng dng, kt qu s tng ln ng k trong vic qun l m. Quay li vi v d display_copyright trn, c th gii hn nhu cu chnh sa ngy thng cng vi vic dng mt hm lng nh ngha trc ca PHP trong hm display_copyright(): function display_copyright($site_name) {

print "Copyright &copy". Date("Y"). " $site_name. All Rights Reserved."; } Bn cng c th lng cc khai bo hm trong mt hm khc. Tuy nhin, vic lng mt khai bo hm khng ng rng n s c bo v trong phm vi gii hn m n c khai bo. Hn th na, mt hm c lng vo khng tha k cc tham s nhp vo ca "cha m" n, cc tham s phi c chuyn sang hm c lng ch khi chng c chuyn sang hm khc. Tuy nhin, vic lng cc khai bo hm l tht s hu ch cho cc l do qun l m v m bo s r rng ca t chc m. V d: function display_footer($site_name) { function display_copyright($site_name) { print "Copyright &copy ". Date("Y"). " $site_name. All Rights Reserved."; } print "<center> <a href = \"\">home</a> | <a href = \"\">recipes</a> | <a href =\"\">events</a><br> <a href = \"\">tutorials</a> | <a href = \"\">about</a> | <a href =\"\">contact us</a><br>"; display_copyright($site_name); print "</center>"; } $site_name = "PHP Recipes"; display_footer($site_name); Sau khi thc hin,on script trn s cho kt qu nh sau: home | recipes | events tutorials | about | contact us Cc hm lng nhau trong PHP khng c bo v bi vic c gi t bt k on script no trong chng trnh, nhng chng khng th c gi li cho n sau khi cc hm cha m c gi. Vic gi th mt hm lng trc khi gi hm cha m ca n s sinh ra mt thng bo li. Cc gi tr tr v t mt hm: Vic tr v mt gi tr t hm l rt hu ch, n c thc hin bng cch gn gi tr gi hm cho mt bin. Bt k kiu d liu no cng c tr v t mt hm bao gm c kiu mng v danh sch. V d: xy dng mt hm tnh thu bn hng $price = 24.99; $tax = .06; function calculate_cost($tax, $price) { $sales_tax = $tax; return $price + ($price * $sales_tax); } // ch cch calculate_cost() tr li mt gi tr $total_cost = calculate_cost ($tax, $price);

// lm trn bin $total_cost c 2 du chm thp phn. $Total_cost = round($total_cost, 2); print "Total cost: ".$total_cost; // $total_cost = 26.49 Hm quy: Hot ng ca mt hm gi li bn thn chnh n nhiu ln tho mn mt vi php ton tht s l mt sc mnh. Nu c s dng mt cch ng n, cc vic gi hm quy c th tit kim c khong trng khng ng v d tha trong mt script v c bit hu ch cho vic thc hin cc th tc lp i lp li. V d s dng mt hm lp quy tnh mt tp hp cc s integer. Function summation ($count) { if ($count != 0) : return $count + summation($count 1); endif; } $sum = summation(10); print "Summation = $sum"; Kt qu ca on m trn s l Summation=55 S dng hm quy c th ci thin tc trong mt chng trnh nu hm c gi thng xuyn.Tuy nhin,phi cn thn khi vit cc th tc quy ,nu m khng ng s dn n vic lp khng dng c. Cc hm c th thay i: Kh nng th v ca PHP l c th thc hin cc hm c th bin i c. Mt hm c kh nng thay i l mt li gi "ng" n hm m tn ca n c xc nh ti thi im thc thi. Mc du khng tht s cn thit trong hu Ht cc ng dng Web, nhng cc hm c th thay i c th gim kch thc m v phc tp mt cch ng k, thng thng loi b cc cu lnh iu kin if khng cn thit. Ta c th gi hm c th thay i c bng cch gi tn mt bin theo sau l tp cc du ngoc n( ). Trong du ngoc n l tp cc tham s tu chn nhp vo. Hnh thc thng thng ca mt hm c th thay i nh sau: $function_name(); V d :s dng hm c th thay i xc nh cc bin nhp vo. Thng ip cho ting Italia function italian() { print "Benvenuti al PHP Recipes."; } // thng ip cho ting Anh

function english() { print "Welcome to PHP Recipes."; } // gn ngn ng dng l ting Italia $language = "italian"; //thc thi hm c th thay i c. $Language(); Xy dng cc th vin hm: Cc th vin hm l mt trong nhng cch hu ch tit kim thi gian khi xy dng cc ng dng. V d, c th vit mt lot cc hm cho vic sp xp cc mng. C th s dng li cc hm ny trong cc ng dng khc nhau. Hn l vic thng xuyn vit li hay copy v dn cc hm ny vo cc script mi, n tht s tin li khi t tt c cc hm lin quan n vic sp xp trong cng mt file phn bit. File ny s cha cc tiu d dng nhn ra,chng hn nh array_sorting.Inc. V d nh sau: <? // File: array_sorting.Inc // purpose:th vin cha cc hm dng cho vic sp xp function merge_sort($array, $tmparray, $right, $left) { . . .} function bubble_sort($array, $n) { . . .} function quick_sort($array, $right, $left) { . . .} ?> Hm th vin array_sorting.Inc ny s hot ng nh l mt ch cha cho tt c cc hm sp xp mng. iu ny tht s hu ch bi c th t chc cc hm mt cch hiu qu theo mc ch cho php d dng tm kim khi cn thit. Mt khi xy dng cho chnh mnh mt th vin hm, c th s dng cc cu lnh include() v require() ca PHP cha ton b cc file th vin vo trong mt script do lm cho tt c cc hm u c sn. C php chung ca hai cu lnh ny nh sau: include(path/filename); require(path/filename); hay cng c th nh sau: include "path/filename"; require "path/filename"; "path": ng dn tuyt i hay tng i ca filename.

Gi s mun dng th vin array_sorting.Inc trong mt script.Bn c th d dng thc hin nh sau: include ("array_sorting.Inc"); By gi th c th d dng s dng bt k hm no trong array_sorting.Inc $some_array = (50, 42, 35, 46); //s dng phng php sp xp bubble_sort( ) $Sorted_array = bubble_sort($some_array, 1); Classes v Objects 1.Class: l mt tp hp cc bin v cc hm cng lm vic vi cc bin ny. Mt class c nh ngha theo c php sau: <?Php class Cart { var $items; // Items in our shopping cart function add_item ($artnr, $num) { $this->items[$artnr] += $num; } function remove_item ($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } } } ?> V d trn nh ngha mt class c tn l Cart, cha cc mng kt hp ca cc article trong mt cart v hai hm dng thm v b cc th ra khi cart ny. Ch : trong PHP 4, ch c cc hng c khi to cho cc bin var mi c cho php khi to cc bin vi cc gi tr khng phi l hng s, cn c mt hm khi to c gi mt cch t ng khi object c dng t lp. Chng hn nh hm c gi trong mt hm dng nh sau: <?Php class Cart { var $todays_date = date("Y-m-d"); var $name = $firstname;

var $owner = 'Fred ' . 'Jones'; var $items = array("VCR", "TV"); } class Cart { var $todays_date; var $name; var $owner; var $items; function Cart() { $this->todays_date = date("Y-m-d"); $this->name = $GLOBALS['firstname']; /* etc. . . */ } } ?> Extends: Thng th cn cc lp vi cc bin v cc hm tng t cho mt lp tn ti khc. Thc t, rt tt khi nh ngha mt lp chung m c s dng trong cc n ca v chnh sa lp ny cho thch nghi vi nhu cu ca mi n. thc hin iu ny, cc class c th c m rng t cc class khc.Vic m rng hay dn xut mt lp c tt c cc bin v cc hm ca lp cha ( iu ny c xem l tha k) v nhng g thm vo trong nh ngha m rng. Khng ging nh cc ngn ng lp trnh hng i tng khc, PHP khng h tr a tha k, cho nn mt lp m rng ch lun lun ph thuc vo mt lp cha ring r ca n. Cc lp c m rng s s dng t kho extends. V d: class Named_Cart extends Cart{ var $owner; function set_owner ($name){ $this->owner = $name; } } V d trn nh ngha mt lp Named_Cart m c tt c cc bin v cc hm ca lp Cart thm v cng thm cc bin $owener v hm set_owner(). Hm to dng:

l nhng hm trong mt lp m c t ng gi khi to ra mt minh ho mi ca mt lp vi t kho new. Trong PHP4, mt lp c xem l hm dng khi n c cng tn vi tn lp m n c nh ngha trong . V d: class Auto_Cart extends Cart { function Auto_Cart() { $this->add_item ("10", 1); } } V d trn nh ngha mt lp Auto_Cart m mt Cart c mt hm dng khi to mi khi mt Auto_Cart mi c gi bng t kho new. Cc hm dng c th c cc i s v cc i s ny c th l tu chn. s dng lp m khng c tham s, tt c cc tham s n hm dng nn l tu chn bng cch cung cp cc gi tr mc nh. V d nh sau: class Constructor_Cart extends Car{ function Constructor_Cart($item = "10", $num = 1){ $this->add_item ($item, $num); } } // Shop the same old boring stuff. $Default_cart = new Constructor_Cart; // Shop for real... $different_cart = new Constructor_Cart("20", 17); Ch : trong PHP4 khi mt lp khng c hm dng th hm dng ca lp cha s c gi nu n tn ti.V d: class A { function A(){ echo "I am the constructor of A.<br>\n"; } function B() { echo "I am a regular function named B in class A.<br>\n"; echo "I am not a constructor in A.<br>\n"; } } class B extends Y function C() { echo "I am a regular function.<br>\n"; }

} // gi B() nh l mt hm dng $b = new B; Phn B : Cng ngh AJAX Gii thiu v AJAX AJAX l g ? Mi ch xut hin ln u vo thng 2 nm (2005), khi Jesse James Garrett ca cng ty AdapativePath nh ngha v tm gn li t cm t Asynchronous JavaScript+CSS+DOM+XMLHttpRequest . Ngay sau cm t AJAX c ph bin cc k nhanh chng trong cng ng pht trin Web V y l nh ngha ca Garrett v AJAX: JAX khng phi l mt cng ngh. N l tp hp ca nhiu cng ngh vi th mnh ca ring mnh to thnh mt sc mnh mi. AJAX bao gm * Th hin web theo tiu chun XHTML v CSS; * Nng cao tnh nng ng v phn hi bng DOM (Document Object Model ); * Trao i v x l d liu bng XML v XSLT; * Truy cp d liu theo kiu bt ng b (asynchronous) bng XMLHttpRequest; * V tt c cc k thut trn c lin kt li vi nhau bng JavaScript . Ajax l mt cng ngh pht trin trn nhiu cng ngh v ngn ng! Nhng im mu cht ca Ajax nm XMLHttpRequest. y l mt k thut do Microsoft khi xng v tch hp ln u tin vo IE5 di dng mt ActiveX . Mozilla tch hp cng ngh ny vo Mozilla 1.0/Netscape 6 sau . V d nhin tt c cc version ca Firefox u h tr XMLHttpRequest , hin nay c trong trnh duyt Safari 1.2 (Apple) v Opera 8 tr ln . Cc th mnh ca AJAX: Phn ny bt u i vo nhng ci nn tng nht, cn hc chm v hiu c vn ct yu: Ajax l vit tt ca Asynchronous JavaScript and XML cng ngh kt hp hai tnh nng mnh ca JavaScript c cc nh pht trin nh gi rt cao: Gi yu cu (request) n server

Phn tch v lm vic vi XML Cc ng dng Ajax xoay quanh mt tnh nng c tn l XMLHttpRequest. cc ng dng web truyn thng, khi ngi dng c mt cn thay i d liu trn trang Web, yu cu thay i c gi v server di dng HTTP request (hay cn gi postback), server s x l yu cu ny v gi tr response cha cc thng tin di dng HTML v CSS, trang HTML ny s thay th trang c. Qui trnh ny c m t l nhp-ch v ti li (click-wait-and-refresh): v d ngi dng sau khi nhn mt nt Submit trn trang web phi ch cho n khi server x l xong mi c th tip tc cng vic. M hnh tng tc trong mt ng dng Web truyn thng:

M hnh tng tc web truyn thng Ajax cho php to ra mt Ajax Engine nm gia giao tip ny. Khi , cc yu cu gi request v nhn response do Ajax Engine thc hin. Thay v tr d liu di dng HTML v CSS trc tip cho trnh duyt, web server c th gi tr d liu dng XML v Ajax Engine s tip nhn, phn tch v chuyn ho thnh XHTML + CSS cho trnh duyt hin th. Vic ny c thc hin trn client nn gim ti rt nhiu cho server, ng thi ngi s dng cm thy kt qu x l c hin th tc th m khng cn np li trang. Mt khc, s kt hp ca cc cng ngh web nh CSS v XHTML lm cho vic trnh by giao din trang web tt hn nhiu v gim ng k dung lng trang phi np. y l nhng li ch ht sc thit thc m Ajax em li.

M hnh ng dng Web truyn thng v Ajax Cn y l m hnh tng tc trong mt ng dng Web dng Ajax:

R rng im khc bit l thay v phi ti c trang web th vi AJAX ta ch cn ti v phn ca trang Web mun thay i. iu ny gip cho ng dng web ca phn hi nhanh hn, thng minh hn. Ngoi ra, im c bit quan trng trong cng ngh Ajax nm ch Asynchronous - bt ng b - tc l gi yu cu ca mnh ti server v tip tc thc thi tc v hin ti m khng cn ch tr li. Khi no server x l xong yu cu ca, n s bo hiu v ta c th n th hin nhng thay i cn thit DOM (Document Object Model). Gii thiu v DOM Document Object Model (DOM) gip phn tch mt ti liu (mt trang web chng hn) phc v cho c ch ca JavaScript. S dng DOM, cu trc ca ti liu c th c phn r theo cu trc cy v thao tc theo cc nt. y l mt kh nng c bit hu ch vit mt ng dng Ajax. Trong cc ng dng web truyn thng, trnh duyt phi ti np cc trang HTML theo mt lung t server. Trong mt ng dng AJAX, s thay i giao din ngi dng ch yu c to ra bi DOM. Cc th HTML trong trang web c t chc theo cu trc cy. Gc ca cy l th <HTML>, biu din ti liu. Trong th <BODY> biu din phn thn ca ti liu, l gc ca phn hin th ca ti liu. Trong thn ca ti liu, c cc bng, paragraph, list, v cc loi th khc vi cc th mc thp hn na. Mt biu din theo m hnh DOM ca mt trang web l mt cu trc cy, c cc phn t l cc nt, ri n cha cc nt con trong n, v c tip tc mt cch qui nh th. JavaScript lm vic vi nt gc ca trang web hin thi qua mt bin ton cc gi l document, bin ny l im bt u ca mi thao tc trn DOM. Phn t DOM c c t bi W3C. Mi phn t DOM c mt phn t cha duy nht, c hoc khng c cc phn t con, v c mt s bt k cc thuc tnh, chng c

lu

tr trong mng mc ni. Mi quan h gia cc phn t DOM c th c i chiu bi danh sch cc thnh phn HTML. Mi quan h ny l hai chiu. Sa i m hnh DOM s thay i cu trc HTML v dn n thay i cch biu din mt trang web. Lm vic vi DOM bng JavaScript: Trong mt ng dng bt k, nu mun thay i giao din ngi dng khi h ang lm vic, th phi cung cp cc phn hi li khi ngi dng gi cc yu cu. hiu r c ch lm vic vi DOM bng JavaScript, chng ta cng xt mt v d v mt trang HTML n gin. <html> <head> <link rel='stylesheet' type='text/css' href='hello.css' /> <script type='text/javascript' src='hello.js'></script> </head> <body> <p id='hello'>hello</p> <div id='empty'></div> </body> Ta thm vo cc tham chiu n cc file hello.css (dng Cascading Style Sheet) v mt file cha m ngun JavaScript l hello.js. y cng ng thi khai bo mt th <div> vi mt ID. Cn y l file hello.css cha stylesheet p dng cho cc mc trong file HTML: .declared{ color: red; font-family: arial; font-weight: normal; font-size: 16px; } .programmed{ color: blue; font-family: helvetica; font-weight: bold; font-size: 10px; } Chng ta nh ngha hai style, m t gc ca cc nt DOM (tn ca cc style l tu chn). Cc style ny khng dc dng trong file HTML, nhng chng s c p dng qua file JavaScript.

window.onload=function(){ var hello=document.getElementById('hello'); hello.className='declared'; var empty=document.getElementById('empty'); addNode(empty,"reader of"); addNode(empty,"Ajax in Action!"); var children=empty.childNodes; for (var i=0;i<children.length;i++){ children[i].className='programmed'; } empty.style.border='solid green 2px'; empty.style.width="200px"; } function addNode(el,text){ var childEl=document.createElement("div"); el.appendChild(childEl); var txtNode=document.createTextNode(text); childEl.appendChild(txtNode); } Hm window.onload() s c gi khi trang web c np. Ti thi im ny, cu trc cy DOM s c thit lp Tm kim mt DOM Node. Yu cu u tin lm vic trn DOM vi JavaScript l i tm kim mt phn t thay i. Trc ht cn bt u tham chiu qua nt gc - root node, nt ny th hin qua bin ton cc document. Mi nt trong DOM l mt nt con (hoc nt con cp hai, ba) ca document, nhng c i dn vo cy DOM, s thy mt ti liu phc tp c biu din bi DOM, v vic tm kim l rt kh khn. V th c cc cch sau tm kim mt nt nhanh chng hn. Mi phn t HTML c mt thuc tnh ID, v d nh, <p id='hello'> hay <div id='empty'></div> Mi mt nt DOM c th c mt ID gn cho n, v ID ny c th c dng tham chiu ti nt qua hm : Var hello=document.getElementById('hello');

Trong mt s trng hp, cn duyt qua cu trc cy tng bc mt, mi nt DOM c mt nt cha v nhiu nt con. Chng c th c truy cp bi cc thuc tnh parentNode v childNodes, thuc tnh parentNode tr v mt i tng DOM node khc, trong khi childNodes tr v mt mng javascript:
var children=empty.childNodes; for (var i=0;i<children.length;i++){ ... }

Mt cch khc tm kim l da trn loi th HTML, dng phng thc getElementsByTagName(). V d, document.getElementsByTagName("UL") s tr v chui tt c cc th <UL> trong ti liu. To DOM Node Trong nhiu trng hp cn to cc nt mi v thm n vo ti liu. JavaScript cung cp mt s phng thc lm iu . Cc phng thc chun to nt mi l document.createElement() v document.createTextNode(), phng thc createElement() c th c dng to ra bt k phn t HTML no, tham s l kiu ca loi th HTML; var childEl=document.createElement("div"); createTextNode() to mt nt th hin qua mt on text, thng c tm thy trong cc th v heading, div, paragraph, v list item. var txtNode=document.createTextNode("some text"); Chun DOM coi cc text node tch ri khi biu din HTML. Chng khng c cc style p t cho trc tip v v th chng yu cu t b nh hn. Mt nt khi c to ra phi c gn vo ti liu trc khi hin th trn trnh duyt, phng thc appendChild() c dng thc hin iu ny el.appendChild(childEl); Ba phng thc createElement(), createTextNode(), v appendChild() cho php thc hin hu ht cc thao tc thm mt nt vo ti liu. Thm style vo ti liu DOM cng cung cp cc phng thc chnh sa style ca cc phn t v to nh dng li cho cu trc c nh ngha trong stylesheet. Mi phn t trong trang web c th c nhiu giao din trc quan c th c p t qua DOM, nh v tr, chiu di v rng, mu sc, canh l v ng vin. Cc trnh duyt ngy nay u cung cp cc rng buc JavaScript cho php

thay i giao din mc thp v p t cc nh dng mt cch nht qun v d dng vi cc lp CSS. Thuc tnh className V d on code sau s p t cc quy tc biu din ca lp declared cho mt nt: Code: hello.className='declared'; vi hello tham chiu ti mt nt DOM. Thuc tnh style V d, on m sau b sung cc thuc tnh style cho nt empty: Code: empty.style.border="solid green 2px"; empty.style.width="200px"; S dng thuc tnh innnerHTML Cc phn t DOM ca cc trnh duyt web u h tr mt thuc tnh gi l innerHTML, cho php cc ni dung kiu string tu c gn cho cc phn t theo thuc tnh ny, nh trong on m sau: Code: function addListItemUsingInnerHTML(el,text){ el.innerHTML+="<div class='programmed'>"+text+"</div>"; } Trn y ta va xt mt cch s lc v JavaScript, CSS, v DOM. Chng c tp hp trong mt cng ngh gi l Dynamic HTML (DHTML), v c th thy Ajax s dng rt nhiu k thut DHTML. Nh vy, cc bn nm c 2 cng ngh dng trong AJAX: CSS v DOM. Trong cc phn tip theo chng ta cng nhau tho lun v: XML v vic truyn d liu bt ng b (Gii thiu v XML v XSLT, XMLHttpRequest) v c l cng nn ni qua mt cht v JS (JavaScript). XML XML l g Nm 1969, IBM cho ra i ngn ng nh du u tin ca mnh vi tn gi Generalized Markup Language (GML). GML l mt ngn ng t c t s

dng cho vic nh du cu trc ca mt tp d liu nht nh ng thi GML c nh hng tr thnh mt meta language (siu ngn ng) ngha l ngn ng dng c t cho ngn ng khc. V sau GML pht trin thnh SGML (Standard Generalized Markup Language). Vo nm 1986, SGML c t chc ISO cng nhn l chun lu tr v chuyn i d liu. SGML c s dng xy dng nhng ti liu nh sch v, bo co, hay cm nang tham kho.Nhng ti liu ny s c chuyn thnh dng thc biu din c v sau gi chng ra thit b xut nh my in, mn hnh... Tuy nhin, tr ngi ln nht i vi ngi dng SGML l chng qu phc tp v tiu tn nhiu cng sc trong vic hin thc. V vy hu ht ngi dng c nhn v doanh nghip u khng th p ng nhng yu cu s dng cng ngh hu dng ny. Nm 1996, Word Wide Web Consortium (W3C) khi ng k hoch xy dng mt ngn ng c gi l XML (eXtensible Markup Language ngn ng nh du m rng) kt hp c sc mnh ca SGML v tnh ph dng ca HTML. S xut hin ca XML mang li cho ngi dng sc mnh ca SGML vi tn km t hn v khng phi i mt vi s phc tp ca SGML. Hn na vic vit parser (b phn tch t vng v c php) cho ti liu XML cng n gin hn. Ngoi ra, XML tng thch vi cc giao thc Internet v phn mm x l, chuyn i d liu. XML c xem l mt tp con ca SGML, v vy XML c c kh nng tng thch vi nhng h thng da trn SGML, gip nh pht trin vn c th duy tr c nhng h thng c xy dng trn nn tng SGML m khng phi tn km trong vic chuyn i. (on trn l copy n paste) Cc c im ca XML 1. 2. 3. 4. 5. 6. 7. XML tng thch vi SGML D dng vit c nhng chng trnh x l ti liu XML Ti liu XML d c v c tnh hp l cao XML c xy dng vi s gim thiu nhng thuc tnh tu chn XML d dng c s dng trn Internet XML h tr nhiu ng dng Khng t nng tnh hnh thc trong ni dung th nh du XML

Cu trc mt ti liu XML Ti liu XML ch cha ng d liu v cch lu tr d liu m khng h cp ti cch thc trnh by d liu. Mt ti liu XML s cha nhng c t v cu trc d liu. Mi cu trc gm nhiu phn t (element), mi thnh phn c bt u vi mt th bt u (Starttag) v kt thc vi mt th kt thc (Endtag).

Gia Starttag v Endtag l ni dung ca phn t ny. Ni dung c th bao gm d liu vn bn hay c th l mt phn t khc. Di y l mt file XML: <?xml version="1.0"?> <Catalog> <Product> <ProductID>F10</ProductID> <ProductName>Shimano Calcutta </ProductName> <ListPrice>47.76</ListPrice> </Product> <Product> <ProductID>F20</ProductID> <ProductName>Bantam Lexica</ProductName> <ListPrice>49.99</ListPrice> </Product> </Catalog> Mt ti liu HTML c th tn ti mt s th khng ng quy nh (trnh bin dch s b qua nhng th ny). Tuy nhin vi mt ti liu XML th iu ny khng th xy ra. Khi xy dng mt ti liu XML, n phi tun th theo mt s quy lut no . Nhng ti liu XML tun th ng nhng quy lut ny c gi l well-formed (tm dch l nh dng ng). Vi mt ti liu khng phi l well formed, Internet Explorer s thng bo li khi np ti liu ny. Mt ti liu XML wellformed cha chc l mt ti liu hp l. Mt ti liu XML c xem l hp l nu n m bo nhng quy tc c t trong ti liu Document Type Definition (DTD) hay gin (schema). Mt DTD hay schema s nh ngha mi th t cu trc d liu ti kiu d liu, nhng thuc tnh c yu cu, v nhng rng buc v thnh phn v thuc tnh c kt hp trong ti liu. Phng thc kim tra ti liu ny thng c s dng trong giao tip gia ng dng - ng dng, m bo d liu trao i hp l trnh dn ti nhng nh hng ca d liu khng hp l trn ton h thng. XSLT - (eXtensible Style Language Transforming) XSLT l mt phn ca XSL (eXtensible Style Language). XSL l mt ngn ng nn tng XML v ra i vi mc ch chuyn i mt ti liu XML thnh mt ti liu XML khc hay chuyn mt ti liu XML thnh nhng i tng c th th hin c. Internet Explorer c tch hp XSL transformer gip t ng chuyn i ti liu XML thnh ti liu HTML. s dng XSLT, chng ta phi xy dng ti liu XSL cha nhng template.

XML v vic truyn d liu bt ng b - XMLHttpRequest Cc ng dng web truyn thng u phi ti np ton b trang web khi gi yu cu n server, iu ny thng dn n mt s ngt qung cho cng vic ca ngi dng. V th yu cu t ra l lm cho cc request ca server khng ng b v ngi dng vn c th lm vic trong khi i tn hiu tr li t server. Gii php u tin gii quyt vn ny l dng IFrame. Cho n gn y, XMLHttpRequest c a ra nh l mt gii php mnh m v hiu qu. XmlDocument v XMLHttpRequest Cc i tng XmlDocument v XMLHttpRequest khng phi l cc chun DOM m rng ca trnh duyt web nhng vn c h tr bi rt nhiu trnh duyt. Cc i tng u da trn ActiveX hay cc i tng JavaScript. Cc trnh duyt khc u c thc thi cc i tng trn c cc chc nng tng ng v cc li gi hm API. Mt v d s dng XmlDocument: PHP Code: function getXMLDocument(){ var xDoc=null; if(document.implementation&&document.implementation. createDocument){ xDoc=document.implementation.createDocument("","",null); }else if (typeof ActiveXObject != "undefined"){ var msXmlAx==null; try{ msXmlAx=new ActiveXObject("Msxml2.DOMDocument"); }catch (e){ msXmlAx=new ActiveXObject("Msxml.DOMDocument"); } xDoc=msXmlAx; } if (xDoc==null || typeof xDoc.load=="undefined"){ xDoc=null; } return xDoc; } Hm va thc hin trn s tr v mt i tng XmlDocument vi cc hm API ging nhau trong cc trnh duyt hin nay. V d sau c chc nng tng t nhng dng cho i tng XMLHttpRequest. PHP Code:

function getXMLHTTPRequest() { var xRequest=null; if (window.XMLHttpRequest) { xRequest=new XMLHttpRequest(); }else if (typeof ActiveXObject != "undefined"){ xRequest=new ActiveXObject("Microsoft.XMLHTTP"); } return xRequest; } Gi mt yu cu n server Gi mt yu cu n server t mt i tng XMLHttpRequest rt d dng. Tt c nhng vic ta cn lm l truyn cho n a ch URL ca server. Ta s lm nh sau: PHP Code: function sendRequest(url,params,HttpMethod){ if (!HttpMethod){ HttpMethod="POST"; } var req=getXMLHTTPRequest(); if (req){ req.open(HttpMethod,url,true); req.setRequestHeader("Content-Type","application/ x-www-form urlencoded"); req.send(params); } } XMLHttpRequest h tr mt min rt rng cc li gi HTTP, gm cc tham s truy vn cho cc trang web ng. Dng cc hm callback gim st cc yu cu Cc hm callback n bt cc s kin trong cch tip cn lp trnh hng s kin trong hu ht cc b cng c UI hin nay nh nhn bn phm, click chut, v khng th bit c cc s kin no s xy ra, nn cc lp trnh vin phi n bt cc s kin c th xy ra . Khi to cc giao din ngi dng UI bng JavaScript, ta gn cc hm onkeypress, onmouseover, v t tn tng t cho cc thuc tnh tng ng cho cc i tng.

Khi lp trnh cho cc yu cu cho server, chng ta gp cc thuc tnh tng t onload v onreadystatechange. i tng XMLHttpRequest Gii thiu XMLHttpRequest By gi ta s xt vn trng tm ca Ajax: i tng XMLHttpRequest. XMLHttpRequest c a ra ban u trong Internet Explorer 5 nh l mt ActiveX component. N ch hot ng trong Internet Explorer iu ny lm cho s t cc nh pht trin lm vic vi XMLHttpRequest, cho ti khi n tr thnh mt chun khng chnh thc trong Mozilla 1.0 v Safari 1.2. Mt iu rt quan trng cn ch l XMLHttpRequest khng l chun ca W3C, mc d rt nhiu chc nng ca n c xut trong cc c t: DOM Level 3 Load and Save Specification. V XMLHttpRequest khng phi l mt chun, nn c nhng s khc bit nh trong cc trnh duyt v h tr i tng ny, tuy nhin hu ht cc phng thc v thuc tnh ca n u c h tr. Hin thi, Firefox, Safari, Opera, Konqueror, v Internet Explorer tt c u h tr XMLHttpRequest tng t nhau. Nu mt s lng ngi dng ng k vn truy cp website vi cc trnh duyt phin bn c, th phi cn nhc v s la chn cng ngh s ng dng. Trc tin cn to mt i tng XMLHttpRequest bng JavaScript trc khi s dng i tng ny gi request v x l cc response. XMLHttpRequest cha l chun ca W3C, v th phi dng JavaScript theo nhiu cch to mt th hin ca ca XMLHttpRequest. Internet Explorer thc thi XMLHttpRequest nh mt i tng ActiveX, v cc trnh duyt khc nh Firefox, Safari, v Opera thc thi n nh mt i tng JavaScript nguyn thu. V s khc nhau ny, on m JavaScript phi cha cc cu trc logic to mt th hin ca XMLHttpRequest dng k thut ActiveX hay i tng JavaScript nguyn thu. Rt may mn, trong trng hp ny khng cn phi vit m mt cch t m xc nh kiu trnh duyt v lm th no to mt th hin ca i tng XMLHttpRequest. Cng vic ch n gin l kim tra s h tr i tng ActiveX ca trnh duyt. Nu h tr ActiveX, th to mt i tng XMLHttpRequest dng ActiveX. Trng hp khc, to i tng ny bng k thut to i tng JavaScript nguyn thu. on m JavaScript sau d dng to ra cc th hin ca i tng XMLHttpRequest m khng quan tm ti trnh duyt. To mt th hin ca i tng XMLHttpRequest: PHP Code: var xmlHttp; function createXMLHttpRequest() { if (window.ActiveXObject) {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } Vic to mt i tng XMLHttpRequest kh l n gin. Trc tin, to mt bin ton cc (gi s c tn l) xmlHttp lu tr mt tham chiu n i tng. Phng thc createXMLHttpRequest thc hin vic to mt th hin ca XMLHttpRequest. on chng trnh ny cha mt cu lnh r nhnh n gin xc nh cch thc to mt i tng. Lnh gi window.ActiveXObject s tr v mt bin kiu object hay gi tr null, tng ng vi iu kin true hay false ca cu lnh if, y l du hiu ch ra cho ta bit trnh duyt h tr ActiveX control, l Internet Explorer. Nu th, XMLHttpRequest c to ra l mt th hin ca ActiveXObject, truyn mt ch bo kiu string xc nh loi i tng ActiveX cn to. Trong th hin ny, ta cung cp tham s Microsoft.XMLHTTP cho hm to, ch ra rng cn to mt th hin ca XMLHttpRequest. Nu lnh gi ti window.ActiveXObject khng c thc thi (iu kin false ca lnh if), lnh JavaScript s r nhnh tng ng vi lnh else, xc nh rng trnh duyt thc thi XMLHttpRequest di dng mt i tng JavaScript nguyn thu. Nu window.XMLHttpRequest tn ti, sau mt th hin ca XMLHttpRequest c to ra. Kiu d liu XMLHttpRequest ca JavaScript tng thch vi rt nhiu trnh duyt khc nhau, c th truy cp cc thuc tnh v phng thc ca mt th hin ca XMLHttpRequest m khng cn quan tm ti cch to cc th hin ny. iu ny lm cho vic pht trin cc ng dng n gin hn v lm cho JavaScript khng ph thuc vo trnh duyt c th. V d sau s ni r cch thc s dng XMLHttpRequest V d <script type="text/javascript"> var xmlhttp function loadXMLDoc(url) { // To ra i tng vi trnh duyt Firefox if (window.XMLHttpRequest)

{ xmlhttp=new XMLHttpRequest() xmlhttp.onreadystatechange=xmlhttpChange xmlhttp.open("GET",url,true) xmlhttp.send(null) } // To ra i tng vi IE else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") if (xmlhttp) { xmlhttp.onreadystatechange=xmlhttpChange xmlhttp.open("GET",url,true) xmlhttp.send() } } } function xmlhttpChange() { // if xmlhttp shows "loaded" if (xmlhttp.readyState==4) { // if "OK" if (xmlhttp.status==200) { // ...Mt vi on m vit y... } else {

alert("Problem retrieving XML data") } } } </script> Phng thc v thuc tnh ca XMLHttpRequest A-Cc phng thc ca XMLHttpRequest Phng thc Phng Thc abort() getAllResponseHeaders() getResponseHeader("headername") M t Hu b yu cu hin thi Tr li tp hp y ca nhng u mc HTTP nh mt chui Tr v gi tr u mc HTTP c ch r Headername

open("method","URL",async,"uname","pswd" Ch r phng thc (method),a ch ) trang x l d liu (URL) v cc tham s khc. Phng thc la chn c th l "GET", "POST", hoc "PUT" (S dng "GET" khi c mt yu cu d liu v s dng "POST" khi bn mun gi d liu i (trong trng hp c bit d liu cn gi i c chiu di ln hn 512 bytes.) URL c th l mt a ch y (http://www.ungdung.com/index.php )hoc tng i (thumuc/index.php). Tham s ASYNC ch r liu c phi x l thng tin ng b hay khng.Nu gi tr l True th qu trnh gi d liu s c gi i ngay sau lnh send() m khng cn i s p li ( mc d d liu ca ln x l

trc vn cha xong ). Gi tr l FALSE th qu trnh gi d liu s phi i s p li ca qu trnh x l trc mi gi d liu i. send(content) setRequestHeader("label","value") B-Thuc tnh ca XMLHttpRequest Thuc tnh Onreadystatechange readyState M t Mt b x l s kin cho s kin trng thi thay i Tr v trng thi ca i tng uninitialized (Khi to li) = loading (ang ti) 2 = loaded ( ti) 3 = interactive (Tng tc) 4 = complete (y Thng tin nhn v y ) Tr v mt chui vn bn Tr v kiu vn bn ca XML , d liu nhn v phi c phn tch di dng cu trc ca XML . Tr v trng thi di dng s (e.g. 404 for "Not Found" or 200 for "OK") Tr v trng thi di dng String (e.g. "Not Found" or "OK") 1 0 = Gi yu cu ti Server Thm mt cp nhn/gi tr ti u mc HTTP s c gi i.

responseText responseXML Status statusText

S tng tc ca XMLHttpResquest trong AJAX Ta xt mt v d tm hiu cc tng tc ca Ajax. Hnh sau cho thy m hnh tng tc chun trong mt ng dng Ajax.

Khng ging nh cc cch tip cn kiu request/response thng thng trong cc chun Web client, mt ng dng Ajax c nhng khc bit, sau y l m t qu trnh tng tc: 1. Mt event client-side gy ra mt s kin - Ajax event. Bt k mt tc ng no cng c th gy ra Ajax event, t mt s kin onchange n gin cho n mt s tc ng ca ngi dng. V d vi on m sau: PHP Code: <input type="text" id="email" name="email" onblur ="validateEmail()";> 2. Mt th hin ca XMLHttpRequest c to ra. Dng phng thc open(), to li gi hm - a ch URL c thit lp cng vi phng thc HTTP yu cu, thng thng l GET hay POST. Request c to ra qua vic gi phng thc send(). on m ngun sau th hin iu : Code: var xmlHttp; function validateEmail() { var email = document.getElementById("email"); var url = "validate?email=" + escape(email.value); if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); }

xmlHttp.open("GET", url); xmlHttp.onreadystatechange = callback; xmlHttp.send(null); } 3. Mt request c to v gi n server. C th l mt li gi ti mt servlet, mt CGI script, hay mt cng ngh pha server no tng t nh ASP.NET, JSP, hay PHP 4. Server x l cc yu cu, chng hn nh truy cp c s d liu hay mt tc v h thng no y. 5. Response c tr v cho trnh duyt. Trng Content-Type c thit lp dng text/xml; XMLHttpRequest ch c th x l kt qu dng text/html. Trong cc th hin phc tp hn, response kh rc ri v bao gm JavaScript, cc thao tc trn i tng DOM, hoc cc cng ngh lin quan khc. Ch l cng cn thit lp header v th trnh duyt s khng lu kt qu mt cch cc b. Ta s lm nh sau: Code: response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); 6. Trong v d sau, cu hnh XMLHttpRequest gi hm callback() khi kt qu x l c tr v. Hm ny kim tra thuc tnh readyState trn i tng XMLHttpRequest v sau xem xt m trng thi tr v t server. Mi th hon ton bnh thng, hm callback() c th lm nhiu vic trn pha client. Mt phng thc callback thng c dng sau: PHP function callback() { if (xmlHttp.readyState == 4) { if (xmlHttp.status == 200) { //do something interesting here } } } Code:

C mt s khc bit vi m hnh request/response thng thng nhng khng qu l lm i vi cc lp trnh vin Web. R rng, phi xem xt thm v vic to v thit lp mt i tng XMLHttpRequest v sau (hm) callback s kim tra cc trng thi. Thng th cc li gi chun ny c ng gi vo mt th vin dng trong ng dng, hay ni cch khc l dng mt th vin c sn thc thi Ajax cho ng dng Web (c rt nhiu th vin nh th, ta s xt trong

cc phn sau). Ajax l vn tuy cn mi m, nhng c mt lng ng k cc th vin v ng dng m ngun m c cng b. Hu ht cc framework v toolkit Ajax trn cc trang Web u dng cc k thut c bn v tru tng ho cc trnh duyt, v thm vo mt s component giao din ngi dng (UI). Mt s l cc framework thun client; cn li lm vic trn server. Nhiu framework trong s ny mi c bt u xy dng, nhng chng lin tc c cc phin bn v c thm cc th vin mi. Mt s gii php thc thi Ajax l cc th vin Ajax.NET, Atlas, libXmlRequest, RSLite, sarissa, JavaScript Object Notation (JSON), JSRS, Direct Web Remoting (DWR), v Ruby on Rails Bi sau chng ta s phn tch "Cc phng thc GET v POST". Cc phng thc GET v POST Trn l thuyt, s dng GET khi request khng thay i gi tr, tc l nhiu request s tr v cng kt qu. Trong thc t, nu phng thc tng ng server thay i trng thi theo mt vi cch, th iu ny khng cn ng na. iu ny c ngha, n l mt chun. C rt nhiu s khc bit vi chun trong iu kin kch thc ca phn m (payload) - trong nhiu trng hp, cc trnh duyt v server s gii hn di ca a ch URL s dng gi d liu ti server. Tm li, dng GET truy lc d liu t server; hay ni cch khc trnh c vic thay i trng thi trn vi li gi GET. Phng thc POST c dng khi mun thay i trng thi trn server. Khng ging nh GET, phi thit lp phn Content-Type header trn i tng XMLHttpRequest nh sau: PHP Code: xmlHttp.setRequestHeader("Content-Type","application/ x-www-form-urlencoded"); POST khng hn ch kch thc ca payload c gi ti server, v POST request khng cn bo m tnh khng i. Hu ht cc request c thit lp GET request; tuy nhin trng thi POST cng lun sn sng khi cn thit. Bi ny hi ngn, nhng hm nay l ch nht nn hc lin 2 bi, c bi 14 s c trong t pht na! Remote Scripting V c bn, remote scripting l mt loi li gi cc th tc t xa. S tng tc vi server vn nh cc ng dng Web thng thng, nhng khng ti np (refresh) ton b trang web. Ch vi AJAX, mi c th s dng cng ngh bt k pha server c th nhn cc request, x l chng v tr v kt qu. Vi mi cng ngh pha server, c mt s la chn cho pha client thc hin remote scripting. C th nhng vo cc on Flash, Java applet, hay cc ActiveX vo ng dng.

Thm ch cng c th dng mt s cng ngh nh XML-RPC, nhng s phc tp ca phng php ny lm gim tnh ph bin ca n. Cch thc thi c bn i vi remote scripting bao gm phi hp mt scripting vi mt IFRAME v server tr v cc on m JavaScript, cc on m ny s c chy trong trnh duyt. Microsoft c gii php ring v remote scripting, c gi l Microsoft Remote Scripting (MSRS), cho php gi cc server script ging nh l chng cc b. Mt Java applet c nhng vo trang web lm cho s lin lc vi server c d dng, mt trang asp c dng cha cc script pha server, mt file .html qun l bn pha client. C th dng cc gii php ca Microsoft vi Netscape v Internet Explorer 4.0 tr nn. Cc li gi hm ny c th ng b hay bt ng b. Tuy nhin gii php ny yu cu Java, c ngha l cn c s ci t thm vo, v n ph thuc vo trnh Internet Information Services (IIS), t lm gim s la chn cho pha server. V d v Remote Scripting so snh, chng ta cng xt mt v d v cng ngh tng t AJAX c thc thi nh th no dng IFRAME. V d sau ch ra vic dng IFRAME cho remote scripting. Trong v d ny c hai file iframe.html v server.html. Server.html gi lp mt response c tr v t server. iframe.html Code: <html> <head> <title>Example of remote scripting in an IFRAME</title> </head> <script type="text/javascript"> function handleResponse() { alert('this function is called from server.html'); } </script> <body> <h1>Remote Scripting with an IFRAME</h1> <iframe id="beforexhr" name="beforexhr" style="width:0px; height:0px; border: 0px" src="blank.html"></iframe> <a href="server.html" target="beforexhr">call the server </a>

</body> </html> server.html Code: <html> <head> <title>the server</title> </head> <script type="text/javascript"> window.parent.handleResponse(); </script> <body> </body> </html>

Gi cc request bng XMLHttpResquest: By gi ta s tho lun xem cch s dng i tng XMLHttpRequest. cc phn trc cp n cch to i tng ny, by gi ta s ch ra lm th no gi request ti server v x l cc response t server. Request n gin nht l mt request m khng cha mt thng tin no c, tc l cc form tham s truy vn hay form gi d liu l trng. Trong thc t th lun gi cc request mang

thng tin n Cc bc c bn gi request dng i tng XMLHttpRequest l:

server.

1- Dng mt tham chiu ti mt th hin ca XMLHttpRequest, c th bng cch to mi hay truy cp vo mt bin c th hin ca XMLHttpRequest. 2- Khai bo cho i tng XMLHttpRequest v hm s x l cc trng thi ca XMLHttpRequest. Ta hon thnh vic ny bng cch thit lp thuc tnh onreadystatechange ca i tng vi mt con tr v mt hm JavaScript. 3- Gn cc thuc tnh ca request. Phng thc open() ca i tng XMLHttpRequest c gn v request trng thi i. Phng thc open() c ba tham s: mt bin string ch bo (thng dng GET hay POST), mt bin string biu din a ch URL ca ti nguyn, mt bin boolean ch bo request s l bt ng b. 4- Gi request ti server. Phng thc send() s truyn request yu cu ti nguyn. Phng thc send() chp nhn mt tham s, thng thng l mt bin kiu string hay mt i tng DOM. Tham s ny c truyn ti a ch URL ch nh l mt phn ca request. Khi truyn tham s cho send(), phi m bo rng kiu phng thc c gn trong open() l POST. S dng null khi khng c d liu no c gi vi request. C th din gii cc bc trn nh sau: cn mt th hin ca i tng XMLHttpRequest, ch ra xem n cn lm g khi thay i trng thi, khi no th gi cc request v gi nh th no, v cui cng l nh hng XMLHttpRequest truyn request. Ta xt khi nim con tr hm, n cng gn ging con tr d liu, ngoi tr thay v tr vo d liu th n tr vo mt hm. Trong JavaScript, tt c cc hm u c nh a ch trong b nh v c th tham chiu bng tn hm. iu ny cho ta mt cch truyn tham s qua con tr hm rt linh ng v mm do hay lu tr mt con tr hm trong mt thuc tnh ca lp. i vi i tng XMLHttpRequest, thuc tnh onreadystatechange lu tr mt con tr ti hm callback. Hm callback c gi khi trng thi ni ti ca i tng XMLHttpRequest thay i. Khi mt li gi bt ng b c to ra, request c truyn i, v script tip tc x l ngay lp tc n s khng i request c p ng tip tc. Mi ln request c gi i, thuc tnh readyState ca i tng s thay i. V d v mt Request n gin

Trong v d ny, c mt trang HTML vi mt nt bm. Khi nhn vo nt bm, s khi to mt request bt ng b gi ti server. Server s p ng bng cch gi mt file text n gin. Response c hin th ni dung trn mt ca s alert. simpleRequest.html PHP Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Simple XMLHttpRequest</title> <script type="text/javascript"> var xmlHttp; function createXMLHttpRequest() { if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } function startRequest() { createXMLHttpRequest(); xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET", "simpleResponse.xml", true); xmlHttp.send(null); } function handleStateChange() { if(xmlHttp.readyState == 4) { if(xmlHttp.status == 200) { alert("The server replied with:" + xmlHttp.responseText); } } } </script> </head> <body> <form action="#"> <input type="button" value="Start Basic Asynchronous Request" onclick="st artRequest();"/> </form>

</body> </html> File response ca server, simpleResponse.xml, cha mt dng text duy nht. Khi nhn vo nt bm trn trang HTML s tr v mt hp alert vi ni dung ca file simpleResponse.xml. Request ti server c gi mt cch bt ng b, cho php trnh duyt tip tc p ng ngi dng trong khi ch i response ca server pha background (cn c gi l AjaxEngine). Nu mt thao tc bt ng b c chn v nu response ca server cn vi giy tr v, trnh duyt s khng th p ng ngi dng trong thi gian i. S bt ng b c th ci thin tnh trng ny bng cch trnh hin th khi trnh duyt b t lit v khng th p ng ngi dng. iu ny cho php ngi dng tip tc lm vic trong khi server tip tc lm vic vi request trc trong background. Kh nng lin lc vi server; khng c li ngt; vi lung lm vic ca ngi dng m ra rt nhiu k thut ci thin cho pha ngi dng. Mt ng dng, chng hn nh chng thc d liu nhp t ngi dng. Khi mt ngi dng in vo mt trng d liu trn form nhp, trnh duyt c th nh k lin tc gi cc form gi tr ti server kim chng m khng cn ngt qung qu trnh in d liu vo form nhp. Nu nhp d liu khng ng, ngi dng c th ngay lp tc c thng bo, trc khi form thc s c gi n server x l, iu ny tit kim c thi gian v gim qu trnh np d liu trn server, ni dung ca form s khng c np li sau mt form trnh khng thnh cng. Vn bo mt: Bt k mt cng ngh no da trn nn tng trnh duyt s khng trn vn nu b qua vn bo mt. i tng XMLHttpRequest c x l bo mt theo m hnh sandbox ca trnh duyt. Sc mnh ca phng php bo mt gii hn ny tu thuc vo tng trnh duyt. Internet Explorer hin th mt cnh bo tnh trng mt nguy c khng an ton c th tn ti v cho php ngi dng s la chn c tip tc vi request na hay khng. Trnh duyt Firefox s dng request li v hin th thng ip li trn JavaScript. DOM Level 3 v DOM. DOM Level 3 Cc gii php tho lun trn khng phi l cc chun. Mc d XMLHttpRequest c h tr rt rng, song vn c th thy mt s s khc bit vi tng trnh duyt. Nhiu ngi tin rng Ajax da vo s h tr ca W3C; tuy nhin thc t khng phi vy. W3C bt u ch trng vo iu ny v mt chun mi c cng b: DOM Level 3 Load and Save Specification. c t ny c thit k nh mt platform c lp vi cc ngn ng thay i ni dung mt ti liu DOM vi XML. Phin bn 1.0 c gii thiu vo thng 4/2004, nhng hin thi cha trnh duyt no thc hin theo n

DOM l mt c t ca W3C cho mt platform c lp vi ngn ng lp trnh truy cp v sa i ni dung v cu trc ca ti liu. Mt cch hiu khc, n l mt cch thng dng biu din v thao tc mt ti liu HTML hay XML. Mt iu quan trng cn lu l thit k ca DOM da trn c t ca Object Management Group, cho php s dng vi bt k ngn ng lp trnh no. Ban u DOM c thit k lm cho JavaScript linh ng vi cc trnh duyt, sau n pht trin ra khi gii hn ny. DOM l mt m hnh i tng trong ng cnh hng i tng. DOM xc nh cc i tng cn thit biu din v thay i ti liu, cc hnh vi v thuc tnh ca cc i tng ny, v mi lin h gia cc i tng. C th coi DOM nh mt cy biu din d liu v cu trc ca trang web, mc d thc t n c th khng theo cch nh vy. Simple Table PHP Code: <table> <tbody> <tr> <td>Foo</td> <td>Bar</td> </tr> </tbody> </table> C th biu din theo DOM ca bng trn. Li th ca c t DOM l n cung cp mt chun tng tc vi ti liu. Khng c DOM, khng th thc thi c Ajax. DOM khng ch cho php duyt v chnh sa ni dung, m cn lm cho trang web tr thnh cc trang ng. Giao thc HTTP: Trc ht chng ta cng tm hiu hot ng ca giao thc HTTP truyn thng - Hypertext Transfer Protocol, xem xt cc bn tin message ca giao thc ny. Thay v quan tm n cc bt hay byte, chng ta n cc t ng (thng bo) c xc nh trong c t HTTP v cc quy tc kt hp chng li vi nhau. Mt HTTP message bt u vi mt dng request hay status, tip theo c th l nhiu loi headers v phn message body. Cu trc ca HTTP Message HTTP l mt giao thc kiu client/server; client a ra cc request, v server s tr li cc request ny. Cu trc cc HTTP message v th cng thay i theo yu t ny. C mt nh dng cho HTTP request v cho cc response. HTTP Request

Mi request bt u vi mt Request-Line. Dng ny ch ra phng thc m client yu cu, ti nguyn, v phin bn ca HTTP m client c th h tr. Request-Line c th c tip sau mt hay nhiu header v mt message body.

Mt HTTP request bt u vi mt Request-Line v c th bao gm cc header v message body. Phn header c th m t qu vic truyn d liu, xc nh cc yu cu hay phn message body km theo. GET / HTTP/1.1 Accept: */* Accept-Language: en-us Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) Host: www.ft.com Connection: Keep-Alive Request-Line cha ba mc phn bit, l method, uri, v phin bn HTTP, mi mc c phn tch bi mt hay nhiu khong trng.

Mt HTTP Request-Line c mt phng thc, mt a ch nh danh ti nguyn (URI), v thng bo phin bn HTTP. Phng thc c xc nh trn dng u tin ca Request-Line. HTTP nh ngha tt c l 8 phng thc. Mt HTTP server ch c yu cu h tr cc phng thc GET v HEAD; nu chng h tr cc phng thc HTTP khc, s h tr phi c gn vi cc quy tc ca HTTP. c t HTTP cng c cc m rng cc phng thc khc c th c b sung trong tng lai.

Mc tip theo trong Request-Line l Request-uri. Mc ny cung cp a ch nh danh ti nguyn cho mt ti nguyn. V d, Request-uri l /, ch ra mt request cho ti nguyn gc. Cho cc request khng yu cu mt ti nguyn c th (nh l TRACE request hay trong mt s trng hp c OPTIONS request), client c th dng mt du * cho Request-uri. Mc cui cng trong Request-Line l phin bn HTTP. Nh trong v d, phin bn HTTP l 1.1 cha trong on text HTTP/1.1. Tip sau Request-Line, mt HTTP request c th bao gm mt hay nhiu dng message header. Mt message header c th cha cc loi general header, request header, hoc entity header. General header p dng trong truyn d liu; request header p dng cho cc request c th, v entity header p dng cho message body trong request. Mt HTTP request lun cha mt dng trng sau Request-Line v bt k header no. Nu request bao gm mt message body, phn body i sau mt dng trng. Dng trng - blank line rt quan trng v server xc nh c phn kt ca request, hoc phn kt ca header. Khng c dng trng, server nhn cc message s khng bit c cc header khc na c tip tc c truyn khng. HTTP Response HTTP Response kh ging vi HTTP Request. Du hiu khc bit duy nht l response bt u vi mt dng trng thi status so vi Request-Line. Status-Line, cng ging nh Request-Line, cha ba mc ngn cch bi cc khong trng.

Mt HTTP response bt u vi mt Status-Line v c th cha cc header v mt message body. Header c th m t qu trnh truyn d liu, xc nh response, hoc phn body km theo. Dng bt u vi phin bn cao nht ca HTTP m server h tr. HTTP/1.1 200 OK Date: Sun, 08 Oct 2000 18:46:12 GMT Server: Apache/1.3.6 (Unix) Keep-Alive: timeout=5, max=120 Connection: Keep-Alive Content-Type: text/html <html>...

HTTP Status-Line bt u vi ch bo HTTP, m trng thi, v mt on text m t response. Hai mc cn li trong Status-Line l Status-Code v Reason-Phrase. Status-Code l mt b ba k t ch bo kt qu ca request. Status-Code ph bin nht l 200. Gi tr ny thng bo yu cu ca client thnh cng. Header Field HTTP request v response c th c mt hay nhiu message header. Message header bt u vi tn trng v du (:). Trong mt s trng hp, ch c tn trng trong phn header. Trong hu ht cc trng hp khc header cha cc thm thng tin khc na, cc thng tin ny i sau du :. Mt message header kt thc cui dng, nhng nu mt client cn biu din nhiu hn mt dng th dng tip theo s bt u vi mt hay nhiu k t trng hay k t gch ngang (ascii character 8). V d sau l ca User-Agent header: GET / HTTP/1.1 Accept: */* Accept-Language: en-us Accept-Encoding: gzip, deflate User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) Host: www.ft.com Connection: Keep-Alive Nu mt message header cha mt chui gi tr phn tch bi du ,; ta c th tch ra thnh cc dng ring, nh v d sau tch cc gi tr ca Accept-Encoding: GET / HTTP/1.1 Accept: */* Accept-Language: en-us Accept-Encoding: gzip Accept-Encoding: deflate User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) Host: www.ft.com Connection: Keep-Alive Status code Mt phn rt quan trng ca HTTP response l m trng thi status code. M ny xc nh xem yu cu ca client c c thc hin thnh cng hay khng

v cung cp thm thng tin v request. Mi gi tr status code l mt s ba k t. Sau y l bng m trng thi HTTP. HTTP status code

Ph lc A : Hng dn s dng v ci t
Hng dn s dng v ci t CI T PHP V MYSQL s dng (APACHE WEB SERVER) Download Apache Web Server C th download Apache Web Server http://www.apache.org/dist/httpd/binaries/win32/ http://dev.mysql.com/doc/refman/4.1/en/index.html ch chn phin bn thch hp vi h iu hnh m ang dng. Ci t Apache 2.5.8 Cc bc chi tit ci t Apache c minh ho nh sau: Bc 1:

Sau khi download Apache, double click ln file .msi va download, mt hp thoi s hin ra nh hnh bn. Click vo nt Next sang bc 2.

Bc 2:

Chn phn "I accept " tip tc nhn Next mt ln na sang bc 3. Bc 3:

in localhost vo phn Server Name, in admin@localhost vo phn Aministrator's Email Address. Click Next sang bc tip theo.

C th chn v t Appser trn cng ca bn, sau nay ta s t c n ca mnh vo . y l vic cu hnh php Bc 4:

Chn Custom v nhn Next tip tc.

Bc 5:

t password cho phn SQL sau ny c th truy cp vo c s d liu thng qua User l root

Apache s bt u ci t cc file vo a cng ca. Ch c vic l...ngi ch.

Sau khi Apache ci t xong, c th s dng ngay bng cnh chn Fnish. Chy th Apache: Sau khi khi ng, chy Apache t Star Menu: Sau khi ng IE v truy cp vo a ch http://localhost, nu nhn c mn hnh n cho ca Apache th c ngha l ci t t hnh cng Apache. Cu hnh PHPMYADMIN t ca s ca IE chon ng dn sao: http://localhost/phpmyadmin

vi user l root v pasword l pas thit t trn.

y l ni c th to mi cng nh sa xa c s d liu. to CSDL bng cch t tn cho CSDL ti Create new database chn font utf8_unicode_ci hin th ting vit. Sau chn Create

sau chn import v chn Browse add CSDL c sn ca vo sau chn Go

Mt CSDL h tr y khoa c to ra.