ajax and php
DESCRIPTION
Ajax and PhpTRANSCRIPT
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 1/40
Copyright © 2006, Zend Technologies Inc.
Ajax and PHP John Coggeshall
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 2/40
Oct. 18 2005 # 2
Welcome!
• Who am I: John CoggeshallSr. Technical Consultant, Zend TechnologiesAuthor PHP 5 nleashedZend ducational Ad"isor# $oardS%ea&er on PHP'related to%ics (orld(ide)ee&
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 3/40
Oct. 18 2005 # 3
Wh# are (e here*
• We’re here to disc ss !J!"+ and PHP+ and -+ and /a"ascri%t+ and 0et(or&s
• In this three ho r t torial, I’ll #ee$plaining a n m#er o% !J!"&relatedconcepts
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 4/40
Oct. 18 2005 # 4
1air Warning
• I’ll 'arn yo right no' ( I 'or) %or Zend, not*etscape
2 am not a client'side de"elo%er2 do not &no( (hich 3ro(sers su%%ort (hich constructs o4
/a"ascri%t under (hich conditions using (hich technologies on(hich o%erating s#stem2 am a PHP de"elo%er res%onsi3le 4or scaling numerousmission'critical PHP sites and technologies2 do understand 2nternet architectures and ho( to scale themin %ractical en"ironments
2 do understand enough a3out A/A as a technolog# to s%ea&intelligentl#
+on’t e$pect a lot o% ashy !J!" demos here
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 5/40
Oct. 18 2005 # 5
The 3asics
• -o, 'hat does !J!" stand %or any'ay! s#nchronous Ja"ascri%t a nd " -
• The #asic idea: /a"ascri%t is the reigning cham%ion o4 the client side
• 2mage roll'o"ers• HT-• Client'side 4orm %rocessing
0ot all in4ormation and %rocesses can 3e gi"en to the
client• 2nsecure 6 ntrusted• Sim%le %rocessing a3ilit# restrictions
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 6/40
Oct. 18 2005 # 6
As#nchronous /a"ascri%t
• !J!" allo's s to ta)e ad/antage o% the ser/er%or in%ormation, 'hile lea/ing the 1I&relateditems to the client
• It’s not a ne' technology /ust has a neat acron#m no(
• o'’s it 'or) /a"ascri%t a%%lications %er4orm re7uests to the ser"erusing an agreed %rotocol
The ser"er res%onds in &ind (ith the re7uested in4ormationAll o4 this ta&es %lace (ithout reloading the %age
• !synchrono s o% the client /a"ascri%t then %rocesses the result and mani%ulates the%age
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 7/40Oct. 18 2005 # 7
on8t con4use technologies
• Is !J!" mail 3 oogle 4aps0o
• Is !J!" 5rototype or -cript.ac lo. s0o
• Is !J!" #y&on& ails0o
!J!" is simply the idea o% ena#ling yo r#ro'ser to comm nicate asynchrono sly'ith the ser/er to pro/ide a more richser 7We# 2.08 e$perience.
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 8/40Oct. 18 2005 # 8
2m%lementing A/A
• -tep 9: pen a asynchrono s connection%rom the client to the ser/er
• -tep 2: 5er%orm a re; est against theser/er sing an agreed pon protocol
• -tep <: 5rocess the res lts /ia Ja/ascriptand manip late the client 'itho tca sing a % ll re%resh o% the page
<SCRIPT language="JavaScript"><!--pic1= new Image(100 #$pic1%&rc="'ttp ))e*ample%c+m)getRan,+mImage%p'p"$
))--><)SCRIPT>
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 9/40Oct. 18 2005 # 9
9Traditional: A/A
• +espite the misconceptions on 'hat e$actly!J!" is, it does ha/e a traditional approach
- Htt%;e7uest o3ject• A"aila3le in most modern 3ro(sers• 2dentical in conce%t to the 2mage o3ject
Allo(s #ou to retrie"e data 4rom the ser"er (ithout%er4orming an entirel# ne( re7uest
• e; ests are generally made in con= nction'ith a partic lar Ja/ascript e/ent
i.e. on$lur o4 a <i%'code =eld (hich automaticall# =ndsout the cit# 6 state
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 10/40Oct. 18 2005 # 10
9Traditional: A/A
• )ay, so here 'e go:>in%ut t#%e?9text: si<e?95: on$lur?9u%dateCit#State@ :B
• *o' all 'e need to do is implement a =a/ascriptpdateCity-tate>? % nction that creates an
"4@ ttp e; est o#=ect• Then 'e ta)e that o#=ect and re; est a 5 5 page
http:33'''.e$ample.com3getCity-tate.php AipB929
• Dparse the res lt
• D pdate the city and state inp t Eelds to re ectthe ne' in%ormationF
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 11/40Oct. 18 2005 # 11
$ro(ser Wars ;e"isited
• !h, i% only it 'ere that simplen4ortunatel#, - Htt%;e7uest is im%lemented indi erent (a#s on each 3ro(ser;e7uires lots o4 /a"ascri%t 3lac&'magic that 2 don8t
&no( to ensure #ou8re creating the %ro%er o3ject the%ro%er (a#
• 4y sol tion: oogle This %ro3lem has 3een sol"ed a million times o"er so2 (on8t re'ex%lain the (heel here
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 12/40Oct. 18 2005 # 12
sta3lishing a Protocol
• *o' that yo ’/e made a re; est #ac) tothe 'e# ser/er >in this case, sing 5 5 andTT5 GT? time to deal 'ith the response
This is (here things reall# go amuc&
• There is no standard !J!" protocol, thedata can #e anythingComma se%arated =eldsSeriali<ed /a"ascri%tCustom -
SDAP; ncoded =eldsEF 3#tes o4 data, each 3#te re%resenting a command
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 13/40Oct. 18 2005 # 13
sta3lishing a Protocol
• While there are no standards per&se,there are common techni; es
1uture "ersions o4 PHP (ill su%%ort /SD0 encoding 3#de4ault
Allo(s #ou to %ass com%lex data t#%es 3ac& and4orth 3et(een PHP and /a"ascri%t 4airl# easil#
Gou can do(nload /SD0 encoding su%%ort 4rom P C• htt% 66%ecl.%h%.net6%ac&age6json• IjsonJenc ? jsonJencode@arra#@K,E,L M• IjsonJdecJ"ar ? jsonJdecode@NO 9a3c: KE 8 M• ja"ascri%t e"al@NO 9a3c: KE 8 M 66 return 4oo.a3c in /S
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 14/40Oct. 18 2005 # 14
A/A (ithout mlHtt%;e7uest
• *o' that yo ha/e the #asic =ist, the clea/er amongyo m st realiAe that "ml ttp e; est isn’tnecessary
• With some cra%ty T4@ yo can do yo r !J!" re; estsing 7standard8 #ro'ser %acilities
Ste% K se /a"ascri%t to create a ne( >SC;2PTB tag in thedocumentSet the source o4 this scri%t tag d#namicall# to our PHP 3ac&end; and %ro"ide the 9out%ut element: 2 (e are interested inmani%ulatingHa"e our 3ac&end (ritten in PHP %rocess the re7uest and return
/a"ascri%t mani%ulating that 2 as (e sa( =t.
• http:33'''.phpit.net3article3a=a$&php&'itho t&$mlhttpre; est323
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 15/40Oct. 18 2005 # 15
2 said it (as a s#nchronous
• egardless o% the approach yo se togenerate an !J!" re; est, al'aysremem#er that it is an !-H*C * * 1-re; est.
Per4orming a 3ehind the scenes s#nchronous re7ueststands a "er# good chance o4 loc&ing u% 2"er# second the ser"er ta&es the res%ond to theclient in a s#nchronous re7uest is a second the#ro'ser is not responding to inp tad.. adD !+
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 16/40Oct. 18 2005 # 16
HTTP ) T "s. PDST
• This one personally really r)es me a#o t'e# de/elopers
) T is 4or ) TT20) dataPDST is 4or PDST20) data
• -ending a GT re; est sho ld ne/er ca sean pdate on the ser/er
;eason K ) T re7uests should 3e 3oo&mar&'a3le;eason E ) T re7uests should 3e cache'a3le
• I% yo se !J!" %or anything other thenretrie/ing data then se TT5 5 -T %orthose actions
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 17/40
Copyright © 2006, Zend Technologies Inc.
Wh# 2 am scared o4 A/A
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 18/40Oct. 18 2005 # 18
Dne o4 the 3iggest %ro3lems (ithA/A
• @et’s imagine that each re; est sent o/erthe 'ire is li)e a car dri/ing %rom point !>the client? to point >the ser/er?
• oads are *et'or)s
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 19/40Oct. 18 2005 # 19
Dne o4 the 3iggest %ro3lems (ithA/A
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 20/40Oct. 18 2005 # 20
Dne o4 the 3iggest %ro3lems (ithA/A
• -imple re; ests seem to 'or) = st EneD
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 21/40Oct. 18 2005 # 21
Dne o4 the 3iggest %ro3lems (ithA/A
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 22/40Oct. 18 2005 # 22
Dne o4 the 3iggest %ro3lems (ithA/A
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 23/40Oct. 18 2005 # 23
Dne o4 the 3iggest %ro3lems (ithA/A
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 24/40Oct. 18 2005 # 24
Dne o4 the 3iggest %ro3lems (ithA/A
• The pro#lem 'ith !J!" has to do 'ithm ltiple dependent asynchrono sre; ests
Gou can8t rel# on an# order o4 o%erations in classicalA/A models
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 25/40Oct. 18 2005 # 25
Dne o4 the 3iggest %ro3lems (ithA/A
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 26/40Oct. 18 2005 # 26
Dne o4 the 3iggest %ro3lems (ithA/A
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 27/40Oct. 18 2005 # 27
Dne o4 the 3iggest %ro3lems (ithA/A
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 28/40Oct. 18 2005 # 28
Dne o4 the 3iggest %ro3lems (ithA/A
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 29/40
Oct. 18 2005 # 29
Some re7uests (ill ha%%en 4aster
• When 'or)ing 'ith !J!", al'ays )no'yo cannot rely on one re; est Enishing#e%ore the ne$t is triggered
• e; ests can ta)e di erent lengths o%time #ased on a h ge array o% %actors
Ser"er load and 0et(or& load come to mind
• Can really mess p yo r application• ad ne's: *one o% the c rrent !J!"
tool)its acco nt %or this latency
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 30/40
Oct. 18 2005 # 30
e"elo%ing (ith atenc# in mind
• ! n m#er o% tools e$ist %or de/eloping!J!" applications 'ith latency in mind
A/A Prox# is a good exam%le• htt% 66ajax3log.com6archi"es6EFF56FQ6FQ6ajax'%rox#'FE
• Allo(s #ou to simulate latenc# in #our re7uests Gou can use it in conjunction (ith 9S(itchProx#: to%oint #our 3ro(ser at a di erent %rox# ser"er to useit
• htt% 66(((.roundt(o.com6%roduct6s(itch%rox#
• *ot a tr e sol tion, # t at least let’s yotest %or the pro#lem.
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 31/40
Oct. 18 2005 # 31
A/A ;ede=ning the notion o4 state*
• *o' that 'e are tal)ing a#o t !J!"intelligently, let’s tal) a#o t a /eryimportant aspect to the modern 'e#application: sessions
Sessions allo( current (e3 a%%lications to maintainstate across stateless HTTP re7uests
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 32/40
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 33/40
Oct. 18 2005 # 33
;e7uests %er second @Traditional
• ther then act ally 'or)ing, scaling a'e# application is the most importantarchitect ral consideration
@accurate ;e7uests %er second is &e# metric
• Consider 'hat happens d ring a singleser/er3single client e$change
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 34/40
Oct. 18 2005 # 34
;e7uests %er second @Traditional
• -er/ers are limited to a ma$im mre; ests per second #y n mero s %actors
• To scale:-a&e the maximum sustaina3le ;PS num3er as highas %ossi3le
• 1aster scri%t execution times• 1aster data3ase access
-a&e the most o4 e"er# re7uest• A"oid costl# unnecessar# handsha&es• 2ntelligentl# segment content
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 35/40
Oct. 18 2005 # 35
;e7uests %er second @Traditional
• Common scaling tric): static content%armsD 'load non'logic'3ased content ser"ing tolight(eight and 4ast HTTP ser"ers
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 36/40
Oct. 18 2005 # 36
;e7uests %er second @A/A
• @oo)ing at the !J!" philosophy it’s cleara di erent re; est pattern e$ists
;elati"el# hea"# and common load s%i&esRer# 4re7uent and relati"el# 7uic& 4ollo('u% re7uests
• While some tric)s can #e #orro'ed %romthe old models, clearly a ne' pattern o%scaling m st #e introd ced
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 37/40
Oct. 18 2005 # 37
D%timi<ing A/A %ages
• -ingle&ser/e client li#rariesse tools to com3ine multi%le /a"aScri%t6CSS =lesinto a single giant =le to reduce the load on theser"er to a single re7uest to load the a%%lication
logic• Can 3e cached on the client
• !/oid Erst&e$ec tion spi)esesign #our a%%lications u%on initial execution to%er4orm a single A/A re7uest to e ecti"el# %o%ulatethe entire %age
• ;educes strain on 3oth the %i%eline and on #our3ac&end data3ase ser"ers
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 38/40
Oct. 18 2005 # 38
A thought
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 39/40
Copyright © 2006, Zend Technologies Inc.
The 1uture o4 A/A *
7/21/2019 Ajax and Php
http://slidepdf.com/reader/full/ajax-and-php-56da2413d7716 40/40
Than& #ou!K estions