130 51
Russian Pages 1073 Year 2016
iRMHTPHH KoTepoa, Mropb CHMARHOB
HOBOBB0,Q0HIASI PHP 7 06b0KTHO-OplA0HTIAposaHHOe nporpaMM111posaH111e KoMnoHeHTbl PHP 111 Composer CTaH,QapTbl PSR PHP-FPM M nginx L-1cxo.QHble KOAbl Ha GitHub
AMMTpMM Korepoe 1!1ropb CMMARHOB
CaHKT-nerep6ypr
« 6XB-nerep6ypr»
2016
YW 66K
004.438 PHP 32.973.26-018.I K73
K73
KoTepoe, .n;. B. PHP 7 I ,n;. B. Korepos, 11. B. CHM.!UIHOB. - CII6.: EXB-IleTep6ypr, 2016. 1088 C.:
HJJ.
-(B IlO.!lJIHHHHKe)
ISBN 978-5-9775-3725-4 PaccMOTpeH&I ocHOBbl JIJblKa PHP H ero pa6o11ero OKpYJKeHHJI s Windows, Mac OS X H Linux. OTpIDKeHbl pa,nHK8.II&H&1e HJMeHeHHJI s JIJI:.IKe PHP, npoHJowe.11wHe c MOMeHTa s&1xo.11a npe.11&1.11ymero H3.llaHHJI: TpeITTbl, npOCTpaHCTBO HMeH, aHOHHMHble «l>YHKUHH, JaMblKaHHJI, 3JJeMeHTI:.I CTporoA THnHJaUHH, reHeparop&1, BCTpOeHH&IH Web-cepsep H MHOrHe .11pyrHe BOJMOJKHOCTH. IlpHse.11etto onHcaHHe CHHTaKCHca PHP 7, a TaIOKe cl>YHKUHA .l1JlJI pa6or&1 c MaCCHBaMH, ct>aH.naMH, CYE,n: MySQL, memcached, perynJipH&IMH B&IpIDKeHHJIMH, rpacl>H11eCKHMH npnMHTifBaMH, no11ToA, ceccHJIMH n T. .11. Oco6oe BHHMaHHe y.11eJJeHo pa6o11eMY oKpYJKettmo: c6opKe PHP-FPM H Web-cepsepa nginx, CYE,n: MySQL, npOTOKOJJY SSH, BHPTYaJI&H&IM MawHHaM VirtualBox H MeHe.ll)Kepy BHPTYaJI&H&IX MaIDHH Vagrant. PaccMoTpeHI:.I cospeMeHHI:.Ie no.11Xo.11&1 K Web-paJpa6oTKe, cHCTeMa KOHTpOJlll sepcHA Git, GitHub H .11pyrHe 6ecnJiaTHble Git-xoCTHHrH, HoBaJI CHCTCMa pacnpOCTpaHeHHJI nporpaMMHblX 6H6JJHOTeK H HX pa3pa60TKH, c6opKa Web-npHJJoJKeHHA MeHe.ll)KepoM Composer, CTaH.llapTbl PSR H .11pyrHe HHCTpYMCHT&I H npHeMI:.I pa6or&1 cospeMeHHoro PHP-coo6meCTsa. B TpeTbeM H3.118HHH .1106aBJieHbl 24 HOBble fJlaBbl, OCTaJibH&Ie rJJaBbl o6HOBJieHbl HJIH nepepa60TaH&1. Ha caATe HJ.llaTeJJbCTBa H8XO.l1JITCJI HCXOllHbie KO.llbl scex JJHCTHHrOB. /(11R
Web-npozpaMMucmoe YD.K 004.438 PHP 66K 32.973.26-018.1
rpynna noaroTOBKH HlllBHHH: r.1asHL1A pe.aairrop 3aM. rnasHoro peaairropa 3as. peJiaKUHeA Pellairrop KoMnblOTepHIUI eepcnca KoppeKTOp ,UnJaAH cepHH Olj>opt.1.1eHHe o6JJOlKKH
EKamepw1a KoHOyKOtJa Et12eHuii Pb16aK06 EKamepuHa Kana,11>1zuHa AHHa Ky11>1.1uHa o,1bZU CepzueHKO 3uHauOa Jlwumpuesa J1HHb1 Ta'luHoii MapuHbl J!a,w6uesoii
noAnMcaHO B neHR KoTeposa .......................................................................................... 34 6naro.uapHOCTH OT liropJI CHMMHOBa ............................................................................................35
Ilpe)lHCJIOBHe K nepeoMy Hl.llBHHJO ............................................................................... 39
11ACTb I. OCHOBbl WEB-IlPOrP AMMHPOBAHIDI .......................................... 41
r JIBBB 1. IlpHHQHDhl pa60Thl HeTepeeTa .................................................................... 43 CTpoTOKOJibI nepe.uaqn .uaHHbIX ........................................................................................................43 CeMeAcTBo TCP/IP ..........................................................................................................................45 A..upecaunJI B CeTn ...........................................................................................................................46 IP-a.upec ...................................................................................................................................46 Bepc"" npoTOKoJia IP ..............................................................................................................47 JJ:oMeHHoe HMJI .........................................................................................................................48 flopT ........................................................................................................................................50 YcTaHOBKa coe.unHeHHR ..................................................................................................51 06MeH .uaHHbIMH .............................................................................................................52
4
OznaeneHue
TepMHHonor1U1 ................................................................................................................................52 CepBep .....................................................................................................................................52 YleJI .........................................................................................................................................53 IlopT ........................................................................................................................................53 CereBoit .neMoH, cepBHC, CJl)')l(fia .............................................................................................53 XocT .........................................................................................................................................54 BupzyanhHLiii xocT ..................................................................................................................54 IlpoBaH.uep ...............................................................................................................................55 XocTHHr-npoBai\.nep (xocTep) .................................................................................................55 XocTHHr ...................................................................................................................................55 BupzyanhHhlii cepBep ..............................................................................................................55 CattT .........................................................................................................................................56 HTML-.noK)'MeHT.....................................................................................................................56 CTPaHHUa (HJIH HTML-cTPaHHUa) .......................................................................................... 56 CKpum, cueHapun ...................................................................................................................56 Web-nporpaMMHpOBaHHe ........................................................................................................56 83aHMOCBJl3h TepMHHOB ..................................................................................................................57 World Wide Web H URL .................................................................................................................57 IlpOTOKOJl .................................................................................................................................58 l1MH xocra ................................................................................................................................58 IlopT ........................................................................................................................................58 Ilyrh K CTPaHHUe ......................................................................................................................59 PeJK>Me ............................................................................................................................................59
r Jl8B8 2. HeTepcl>eiic CGI B npOTOKOJI HTTP ............................................................. 60 qTo raKoe CGI? ...............................................................................................................................60 CeKpeThl URL ..................................................................................................................................61 3aroJIOBKH Janpoca u MeTo.n GET ................................................................................................... 62
GET.......................................................................................................................................... 63 POST........................................................................................................................................ 64 Content-Type ........................................................................................................................... 64 Host.......................................................................................................................................... 64 User-Agent ............................................................................................................................... 65 Referer ······························································································:······································65 Content-length ......................................................................................................................... 65 Cookie ...................................................................................................................................... 66 Accept ...................................................................................................................................... 66 3M)'JUIUHJI 6paYJepa 11epe3 telnet. ............................................................................................66 Mero.n POST ...................................................................................................................................67 URL-Ko.nupoBaHHe ..........................................................................................................................68 qTo TaKoe $opMhI H .[{JUI 11ero OHH H)')KHhI? ....................................................................................68 Ilepe.na11a napaMeTPOB "BPY'lffY10" ..........................................................................................69 HcnoJihlOBaHHe $opMhl ...........................................................................................................69 A6coJIK>THhlii H OTHOCHTeJlhHhdt nyru K cueHapmo ................................................................ 70 MeTo.n POST H $opMhl ............................................................................................................71 PeJK>Me ............................................................................................................................................71
OenaaneHue
rJJaea 3. CGI
5 HlHyYpH •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 72
.H3b!K C .............................................................................................................................................72 Pa6ora c HCXOD.HbIMH reKcraMH Ha C ...................................................................................... 73 KoMnHJU11UU1 nporpaMM ..........................................................................................................73 Ilepe.11.aqa .11.oK)'MeHTa nonbJoaareJDO ..............................................................................................74 3aronoBKH OTBeTa ....................................................................................................................74 3aronoBOK KO.II.a OTBeTa ..................................................................................................74 "IloMeJibIBaHHe" JaronoBKa oraera ...............................................................................75
Content-type ................................................................................................................... 75 Pragma ........................................................................................................................... 75 Location .......................................................................................................................... 75 Set-cookie ....................................................................................................................... 76 Date ................................................................................................................................ 76 Server .............................................................................................................................. 76 IlpHMepb1 CGI-cueHap11ea Ha C ..............................................................................................76 Bb!B0.11. 6ttHapHoro al\Jia ...............................................................................................77 Ilepe.11.aqa HHopMaUHH CG 1-cueHapHlO ......................................................................................... 78 IlepeMeHHh1e oKp)')KeHIUI ........................................................................................................78 nepe.11.aqa napaMerpOB MeT0.11.0M GET. .................................................................................... 80 Ilepe.11.a'la napaMerpoB Mero.11.oM POST................................................................................... 81 PacumpoBKa URL-K0.11.HpOBaHHbIX .11.aHHbIX .......................................................................... 83 OpMbl. ............................................................................................................................................86 Ter - paJnH'IHhie noJUI aso.11.a ................................................................................. 87 TeKcroaoe none (text) ..................................................................................................... 87 Ilone BB0.11.a napOJUI (password) ...................................................................................... 87 CKpbnoe reKcroaoe none (hidden) ................................................................................. 88 HeJaBHCHMbIB nepeKJIJOqarenb (checkbox) .................................................................... 89 3aBHCHMbIB nepeKJUO'!arenb (radio) ............................................................................... 89 KHonKa ornpaBKH opMbI (submit) ................................................................................ 90 KHonKa c6poca opMbI (reset) ....................................................................................... 90 PHC)'HOK )lJUI ornpaBKH opMbI (image) ......................................................................... 90 Ter - MHorocrpoqHoe none BBo.11.a reKcra ......................................................... 90 Ter - CnHCOK ..........................................................................................................91 CnttCKH MHmKeCTBeHHoro Bb16opa (multiple) ................................................................. 92 :-i TML-cymHocrH ...........................................................................................................................•92 33.rpy"JKa aBJIOB .............................................................................................................................93 opMaT .11.aHHhlX ......................................................................................................................94 Ter JarpyJKH aBJla (file) ........................................................................................................ 95 -'. :-o raKoe cookies H "c qeM HX e.11JIT"? ............................................................................................96 Y craHOBKa cookie ....................................................................................................................98 IlonyqeHHe cookies H3 6payJepa ........................................................................................... 100 rlpHMep nporpaMMbl )lJUI pa6orb1 c cookies .......................................................................... 100 !_ :-1-.~HTHHKaUIUI .......................................................................................................................... 101 =-:310\le ..............................................................•...............•...................................................•....... 103
r .1aea 4. BCTpoeHHLiii cepeep PHP ............................................................................ 104 · - :-3.HOBKa PHP B Windows .......................................................................................................... 104 rlepeMeHHM OKp}')KeHIUI PATH ............................................................................................ 105
OanaeneHue
6
Y cTaHOBKa PHP B Mac OS X ........................................................................................................ 107 YcraHOBKa PHP B Linux (Ubuntu) ................................................................................................ 109 3anycK BC'rpoeHHoro cepsepa ........................................................................................................ 109 cI>attrr hosts ..................................................................................................................................... 110 BeruaHue BOBHe ............................................................................................................................. 110 KoH4nrrypupoeaHHe PHP .............................................................................................................. 111 Pe3toMe .......................................................................................................................................... 111 ~CTL
II. OCHOBhl .B3L.IKA PHP ......................................................................... 113
rJiaea 5. XapaK1'epRCTHK8 RlhlK8 PHP ...................................................................... 115 11CTOPHJI PHP ................................................................................................................................ 115 qyo HOBoro B PHP 7? .................................................................................................................... 119 TipHMep PHP-nporpaMMbl ............................................................................................................. 120 l1cnoJI1>3oBaHHe PHP B Web .......................................................................................................... 124 Pe3toMe .......................................................................................................................................... 126 rJl&BB
6. IlepeMeBBLle, KOBCTBBTbl, TBDbl Jl,BBBbIX .................................................. 127
TiepeMeHHble .................................................................................................................................. 127 KonHpoBaHHe nepeMeHHJ>IX ................................................................................................... 128 THn1>1 nepeMeHHblx .................... :........................................................................................... 128 integer (uenoe qucno) ................................................................................................... 128 double (semecmeHHoe 'IHCJio) ..................................................................................... 129 string (cypoKa TeKcra) .................................................................................................. 130 array (accounaTHBHblA MaccHB) ................................................................................... 130 object (ccblJIKa Ha o6'beKT) ............................................................................................ l 31 resource (pecypc) ......................................................................................................... 131 boolean (JIOJ'HqeCKHA THn) ........................................................................................... 131 null (cneunaJil>Hoe 3Ha'leHHe) ....................................................................................... 132 callable (cp}'HKUHJI o6paTHoro BhI30Ba) ........................................................................ 132 ,[(eACTBIUI c nepeMCHHblMH .................................................................................................... 132 TipHCBOeHHe 3HaqeHHJI .................................................................................................. 132 TiposepKa CYilleCTBOBaHHJI ........................................................................................... 132 Y HH'ITO)f(CHHe ............................................................................................................... 133 OnpeneneHHe THna nepeMeHHOA ........................................................................................... 133 y CTaHOBKa THna nepeMeHHoA................................................................................................ 134 OnepaTop npucsaHBaHHJI .............................................................................................................. 136 CcblJloqHJ>1e nepeMeHHble .............................................................................................................. 136 )l(ecTKHe CChIJIKH ................................................................................................................... 136 "C6op M)'COpa" ............................................................................................................. 137 CHMBOJIH'leCKHe ccblJIKH ....................................................................................................... 138 CcbIJIKH Ha o6'beKTbl .............................................................................................................. 138 HeKoTopble ycnoBHble 060JHa11eHHJ1 ............................................................................................. 139 KoHCTaHTbl .................................................................................................................................... 141 TipenonpeneneHHble KOHCTaHTbl ............................................................................................ 142 OnpeneneHHe KoHcTaHT ......................................................................................................... 143 TipoBepKa CYilleCTBOBaHHJI KOHCTaHTbl .................................................................................. 143 KoHCTaHTbl c JlHH!lMH'leCKHMH HMCH!lMH .............................................................................. 143 OTnano'IHble cp}'HKUUH ................................................................................................................. 144 Pe3toMe .......................................................................................................................................... 146
OanaeneHue
7
rJJaea 7. BblpB*eHHR H onepaQBB PllP ..................................................................... 147 BbipIDKeHHR ................................................................................................................................... 147 JlorH11eCKHe BblpIDKeHHJl ........................................................................................................ 148 CTJlOKOBble BbipIDKeHHR ......................................................................................................... 148 C'TpoKa B anoc'Tpoax ................................................................................................... 149 C'TpoKa B KaBbl'IKax ...................................................................................................... 149 Here-.uoK)'MeHT ............................................................................................................. 150 Now-.uoK)'MeHT ............................................................................................................. 151 BbIJOB BHewHeA nporpaMMbl ....................................................................................... 151 Onepauuu ...................................................................................................................................... 151 ApuMeTH'JecKHe onepauuu ................................................................................................. 151 CTJlOKOBbie onepauuu ............................................................................................................ 152 Onepauuu npucsausaHHR ...................................................................................................... 152 Onepauuu HHKpeMeHTa u .ueKpeMeHTa .................................................................................. 153 liHTOBble onepauuu ................................................................................................................ 153 Onepauuu cpasHeHHR ............................................................................................................ 15 8 Oco6eHHOCTH onepaTopos == u != ............................................................................. 159 CpasHeHue CJIO)l(Hb!X nepeMeHHbIX .............................................................................. 160 OnepllUIUI 3KBHBaJieHTHocrn ........................................................................................ 160 OnepaTop < => ............................................................................................................. 162 JlorH'leCKHe onepauuu .......................................................................................................... 162 OnepaIUUI OTKJTIOqeHHJI npe.uynpe)l(,lleHHA ............................................................................ 163 Oco6eHHoCTH onepaTopa @ ......................................................................................... 164 ITpoTHBonoKa:JaHIDI K ucnoJibJOBaHmo ........................................................................ 165 YcJIOBHbie onepauuu ............................................................................................................. 165 PeJK>Me .......................................................................................................................................... 167
rJiaea 8. PafioTa c JlBHBhlMH topMbl .......................................................................... 168 ITepe.uaqa .uaHHblX KOMaH.llHOA CTJ'OKH ......................................................................................... 168 OpMbI ........................................................................................................................................... 170 TpaHCJUIUHH noneA opMbI .................................................................................................... 171 TpaHCJUIIUUI nepeMeHHbIX OKp}')keHHR .................................................................................. 173 TpaHCJUIIUUI cookies .............................................................................................................. 173 06pa60TKa CTIHCKOB ...................................................................................................................... 174 06pa60TKa MaCCHBOB .................................................................................................................... 175 LJ;uarHOCTHKa ................................................................................................................................. 176 f10pH,llOK 'TpaHCJUIUHH nepeMeHHbIX .............................................................................................. 177 Oco6eHHoCTH JIIDKKOB checkbox ................................................................................................. 177 PeJK>Me .......................................................................................................................................... 179
r Jl8BB 9. KoecTpyJCQHH RlblKB ••..••••••••••••••••••••••••••••••••.•.••••••••••.•.•.•...•••••...•.••••....•..••••• 180 HHC'Tp)'KIUUI if-else .............................................................................. .......................................... 180 HcnoJibJOBaHHe aJibTepHaTHBHoro cHHTaKcuca ............................................................................ 181 UHKJI c npe.uycnosueM while ......................................................................................................... 182 UHKJI c nocrycnosueM do-while .................................................................................................... 183 YHHBepcaJibHblA UHKJifor ............................................................................................................. 183 liHCTJl)'KUHH break H continue ....................................................................................................... 184 He'Tpa.umrnoHHoe ucnoJihJOBaHue do-while u break ............................................................. 185
OenaeneHue
8
IJ;HKJiforeach ................................................................................................................................. 187 KoHCTpYJCIUUI switch-case ............................................................................................................. 188 11HCTpYKUHH goto .......................................................................................................................... 188 11HCTpYJCll,HH require H include ...................................................................................................... 189 11HCTpYJCll,HH 0.IUIOKp3THOro BKJilOqeHHJl ....................................................................................... 190 CyYb npo6neMbI ..................................................................................................................... 191 PeweHHe: require_ once ......................................................................................................... 192 ~yrne HHCTp)'KUHH ...................................................................................................................... 193 Pe3IDMe .......................................................................................................................................... 193 r JI&Ba
10. Acco .. eaTHBHble M8CCHBbl ......................................................................... 194
C03.1laHHe MaccHBa "Ha nezy". ABToMaccHBbI ............................................................................... 195 KoHcTpyKUIUI /istQ ......................................................................................................................... 196 CrrucKH H acco!lHaTHBHble MaccHBbI: IIyYaHHUa? .......................................................................... 197 KoHCTpYJCIUUI arrayO H MHOroMepHble MaCCHBbl .......................................................................... 197 MaccHBbI-KoHcTaHTbI .................................................................................................................... 199 Onepall,HH HM MaccHBaMH ............................................................................................................ 199 .LJ:ocyYII no KJI10qy .................................................................................................................. 199 yHKIUUI countO .................................................................................................................... 199 CnHJ1HHe MaccHBos ................................................................................................................200 CJIHJIHHe crrHCKOB .........................................................................................................200 06HoBJieHHe 3JieMeHTOB ...............................................................................................200 KocseHHh!A rrepe6op JJieMeHTos MaccHBa .............................................................................201 ilepe6op cIIHcKa ...........................................................................................................201 ilepe6op 3CCOilH3THBHOfO M3CCHB3 ............................................................................. 202 He.uocrnTKH KocseHHoro nepe6opa .............................................................................. 203 BJioJl(eHHh!e IlHKJlbl ..............................................................................................203 HynesoA KJIK)q .....................................................................................................203 ilpllMOA nepe6op MaCCHBa .....................................................................................................204 CTapb1A cnoco6 nepe6opa ............................................................................................204 ITepe6op IlHKJIOMforeach ............................................................................................. 204 CcbIJIOqHblA CHHTaKcHcforeach ................................................................................... 204 CrrHCKH H CTpOKH ...........................................................................................................................205 CepHaJIH3aIUUI ...............................................................................................................................206 YnaKosKa ...............................................................................................................................207 PacnaKOBKa ............................................................................................................................207 Pe310Me ..........................................................................................................................................208 rJiaBa
11. cJ>yeK.. HH
H 06JI&CTH BH.Z.BMOCTH ............................................................... 209
ITpHMep cpyHKUHH ..........................................................................................................................209 06IUHA CHHT3KCHC onpeneJieHJUI «PYffKll,HH ................................................................................... 211 11HCTpYJCIUUI return ................................................................................................................ 211 06bl!BJieHHe H Bbl30B «liYHKilffH ............................................................................................. 213 napaMeTpbl no YMOJiqaHHI0 ...................................................................................................213 Tiepe.uaqa napaMeTpOB no ccblJIKe .........................................................................................214 ilepeMeHHOe 'l:HCJIO napaMeTpOB ...........................................................................................215 TH1Ib1 apryMeHTos u so3spamaeMoro 3HaqeHHJI ................................................................... 217 JloKaJibHble nepeMeHHbie ...............................................................................................................218
02naeneHue
9
fJI06aJibHl>Ie nepeMeHHh!e .............................................................................................................219 MaccHB $GLOBALS .............................................................................................................. 220 CaMoBJIO:llCeHHocTb .......................................................................................................221 KaK pa6oraeT HHCTPYJCUIUI global .... ..................................................................................... 221 CTaTH'IeCKHe nepeMeHHb1e ............................................................................................................222 PeK)'PCIUI .......................................................................................................................................223 aKTOpHaJI .............................................................................................................................223 flpHMep QlyHKUHH: dumperO ................................................................................................. 223 BJio:llCeHHb1e QlyHKUHll ...................................................................................................................225 YcJIOBHo onpe.n:eJU1eMbie QlyHKUHH ................................................................................................226 3M)'JUIIJ.lUI QlyHKUHH virtualO ................................................................................................ 227 flepe.n:aqa Q>yHKrurl\ no cchIJIKe .....................................................................................................228 Hcnorrb30BaHHe call_ userJuncO .......................................................................................... 228 HcnoRb30BaHHe call_ userJune_ arrayO ............................................................................... 229 AHOHHMHl>Ie QlyHKUHH ...........................................................................................................229 3aMbIKaHHH ............................................................................................................................230 Bo3spaT QlyHKUHett CCbIJIKH ...........................................................................................................232 TexHorronui: OTJIO:llCeHHoro KOrIHpOBaHIUI .....................................................................................233 HecKOJibKO coseros no Hcnorrb30BaHHIO Q>)'HKUHl\ .......................................................................235 Pe3IOMe ..........................................................................................................................................236
r JIBB8 12. reeep&TOpLI ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 237 0TJIO:llCeHHl>Ie BbltJHCJieHIUI ............................................................................................................237 MaHHnyJUIIJ.lUI MaccHBaMH .............................................................................................................240 Ll:errerttpoBaHHe reHepaTopoB ........................................................................................................242 3KOHOMH.ll pecypcoB ......................................................................................................................243 HcnoJib30BaHHe KJIIOtJett ................................................................................................................244 Hcnorrh30BaHHe CCbIJIKH ....._...........................................................................................................244 Ce.11Jb reHepaTopoB c o6'beKTaMH ..................................................................................................245 Pe310Me ..........................................................................................................................................247
'IACTL ill. CTAJmAPTllLIE cr-YllKIJ;HH PHP ................................................... 249
r JIBB8 13. CTpOKOBble fYRKQBH ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 251 Ko.n:ttpoBKH ....................................................................................................................................251 UTF-8 H PHP .................................................................................................................................255 KoHKaTeHaUH.11 crpoK .....................................................................................................................258 0 cpaBHeHHH crpoK .......................................................................................................................258 Oco6eHHOCTH strposO ............................................................................................................ 259 OrpeJaHHe npo6erros .....................................................................................................................260 6aJOBbie QlyHKUHH .........................................................................................................................261 Pa6oTa c no.n:crpoKaMH ..................................................................................................................262 3aMeHa ...................................................................................................................................262 ITo.n:cTaHOBKa .........................................................................................................................263 npeo6paJOBaHHH CHMBOJIOB ..........................................................................................................267 113MeHeHHe perttcrpa .....................................................................................................................269 YcTaHOBKa JIOKaJIH (rroKaJibHhIX HacrpoeK) ................................................................................... 270 yHKUHH Q>opMaTHl>IX npeo6pa.30BaHHtt .......................................................................................271 ct>opMaTHpOBaHHe TeKCTa ..............................................................................................................273
OenaeneHue
10
Pa6ora c 6HHapHbIMH aaHHLIMH ....................................................................................................274 X3m-$yHKUHH ...............................................................................................................................276 C6poc 6y$epa BbIBOJia ..................................................................................................................278 Pe3JOMe ..........................................................................................................................................278 rJJaea
14. PafiOT8 c
M&CCHB&MH ................................................................................... 279
JleKCHKOrpa$H'l.eCKaJI H 'IHCJIOBaJI copntpOBKH ............................................................................279 CoprupoaKa npoH3BOJibHhIX MaccHBoB ......................................................................................... 280 CoprupoBKa no 3HatJeHJUIM ...................................................................................................280 CoprupoaKa no KJIIO'laM ........................................................................................................280 IloJih30BaTeJibCKM coprupoaKa no KJIIO'laM .........................................................................281 IloJib30BaTeJibCKaJI copntpOBKa no 3HatJeHIDIM .....................................................................282 TiepeaopatJHBaHHe MaccHBa ...................................................................................................282 "EcrecTBeHHrur" coprupoaKa .................................................................................................283 CoprupoaKa cnucKoB ....................................................................................................................284 CoprupoaKa cnucKa ...............................................................................................................284 IlOJib30B3TeJibCKaJI copntpoBKa cnHCKa ................................................................................ 285 CopntpoBKa MHoroMepHblx MaccHBOB ..................................................................................285 TiepeMemHBaHHe cnucKa .......................................................................................................288 KJnoqn u 3Ha'leHlUI ........................................................................................................................288 CJIH11HHe MaccHBOB ........................................................................................................................289 Pa6ora c noJJ:MaccHBaMH ...............................................................................................................290 Pa6ora co creKoM u oqepe.zu,10 ......................................................................................................291 TiepeMeHHbie H MaCCHBbI ...............................................................................................................292 IlpHMeHeHHe a ma6Jiottax ......................................................................................................293 CoJaattne JIHanaJotta qeceJI ..........................................................................................................294 Pa6ora c MHOJKecTBaMH .................................................................................................................295 Tiepece'leHHe .........................................................................................................................295 Pa3HOCTb ................................................................................................................................295 06beJIHHeHHe ...............•........................................................................................................295 JSON-$opMaT................................................................................................................................296 Pe310Me ..........................................................................................................................................301 rJtaea 15. MaTeMBT&qecKHe 4tYBKQBB ....................................................................... 302 BcrpoeHHbie KOHCTaHThI ................................................................................................................302 yHKUHH OKpyrJieHHJI ...................................................................................................................303 CJIY11aAHble 'IHCJia .........................................................................................................................305 TiepeBOJl B pa3JIH'IHble CHCTeMbI C'IHCJieHlUI ................................................................................. 308 MHHHMYM H MaKCHMyM .................................................................................................................308 He-'IHCJia .......................................................................................................................................309 CreneHHble $yHKUHH .....................................................................................................................310 TpurottoMerpH1! .............................................................................................................................310 Pe310Me ..........................................................................................................................................312
rJiaea 16. PafioTa c
tall:JJ&MH H KBTBJJOr&MB ............................................................. 313
0 TeKCTOBbIX H 6ettapHbIX 1e H o6paTHbie cnewu ..................................................................................................317 EeJbIMJIHHbie speMeHH1>1e cl>a1tJn.1 .......................................................................................... 318 3axpbITHe ct>attrra ............................................................................................................................3 19 qTCHHe H JaIIHCb ............................................................................................................................319 bJIO'IHble 'ITeHHehaIIHCb ........................................................................................................320 IlOCTPO'IHble 'ITeHHe/JaIIHCb ..................................................................................................320 qTeHHe CSV-ct>attrra ...............................................................................................................321 IlOJIO'llCCHHe )'Ka33Te.IDI TCK)1lleR Il03HUHH .................................................................................... 322 Pa6oTa c nyTHMH ............................................................................................................................323 MaHHJIYJIHpOBaHHe 11eJibIMH cl>attrraMH .......................................................................................... 325 qTeHHe H JaIIHCb 11enoro ct>attrra ............................................................................................325 qTeHHe INI-ct>attrra .................................................................................................................326 .Z:U,yrue cl>YHKUHH ...........................................................................................................................328 bJIOKHpOBaHHe ct>attrra ....................................................................................................................329 PeKoMeH,!laTeJibHM H 'llCecTKrui: 6noKHpOBKH .......................................................................... 329 yHKuHRf/ockO ..................................................................................................................... 329 THnbI 6JIOKHpOBOK .................................................................................................................330 11CKJl10'IHTCJlbH3'1 6JIOKHpOBK3 ..................................................................................... 330 "He y6Htt!" ...........................................................................................................331 "Ilocal{H ii:epeso" .................................................................................................331 "CJiel{H 33 co6oR, 6yJJ:b OCTOpO'llCeH" .................................................................... 332 BhlBOJJ:hl ................................................................................................................333 PaJJJ:eJUleMrui: 6JIOKHpOBKa .............................................................................................333 BhlBOJJ:bl ................................................................................................................33 5 bJIOKHpOBKH c 3anpeTOM 11 IlO.!lBHCaHHJI" ................................................................................ 335 IlpHMep C'ICT'IHKa ..................................................................................................................335 Pa6oTa c KaTaJioraMH .....................................................................................................................336 MaffHilYJIHpOBaHHe KaTanoraMH ............................................................................................336 Pa6oTa c JaIIHC.llMH ................................................................................................................337 IlpHMep: ne1Tb HJIH He 61>1T1>? .......................................................................................................................412 CCbIJIKH ..........................................................................................................................................412 PeJIOMe ..........................................................................................................................................412
OenaeneHue
14 rJlaea
21. Pa3Hble tYHKQBH
••••••••••••••••••.••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 413
11.ttnKaUIDI n HacnellOBaHlfe .......................................................................................... 521 l>a30BbIH KJiacc Exception ............................................................................................. 522 11cnoJib3oeaHne HHrepQ>ettcoe ...................................................................................... 523 11cKJIJO'leHRll e PHP 7 ................................................................................................... 526 bJIOKR-RHaJIH3aTOpbl ...........................................................................................................527 nepeXBaT BCeX HCKJDOqeHfftt .....................................: .................................................. 528 TpaHcQ>opMaUIDI own6oK ..............................................................................................................529 Cepbe3HOCTb "Hecepbe3HbIX" ornn6oK ................................................................................... 530 npeo6pa30BaHRe OIIIH60K B HCKJIJO'leHlfll ............................................................................. 531 npRMep .........................................................................................................................531 Koll 6n6nnoTeKn PHP_Exceptionizer .......................................................................... 532 11epapxIDI HCKJDOqeHRH ................................................................................................535 IUlbTJ)aUIDI no TRnaM omn6oK ................................................................................... 536 PeJIOMe ..........................................................................................................................................537
'IACTh V. IIPE)J;OIIPE.IJ;EJIEHllhlE KJIACChl PHP .......................................... 539 r JJaBa
27. Ilpe,llonpe,lleJJeHHble KJl8CCbl PHP ............................................................. 541
Knacc Directory ............................................................................................................................. 541 Knacc Generator ............................................................................................................................ 544 Knacc Closure ................................................................................................................................ 545 Knacc Int/Char .............................................................................................................................. 548 Pe3JOMe ..........................................................................................................................................549 rJJaea
28. KaJieH,llapHble
KJl8CCbl
PHP ....................................................................... 550
Knacc DateTime ............................................................................................................................ 550 Knacc DateTimeZone .................................................................................................................... 551 Knacc Datelnterval ........................................................................................................................ 552
OanaeneHue
17
K.nacc DatePeriod. ......................................................................................................................... 554 PeJJOMe ..........................................................................................................................................555
rJJaea 29. HTepaTOpbI ................................................................................................... 556 CraH.uapTHoe noee.ueHtte foreach .................................................................................................. 556 Onpe.ueJieHtte co6crneHHoro HTeparnpa ....................................................................................... 557 KaK PHP 06pa6an.1eaeT HTepaTOpbI .............................................................................................. 560 MHO)f(eCTBeHHbie HTepaTopbI ........................................................................................................ 561 Bttpl)'aJibHbie MaccHBbI ................................................................................................................. 561 6116JI110TeKa SPL ............................................................................................................................ 563 K.nacc Directorylterator ........................................................................................................ 563 K.nacc Filter Iterator ............................................................................................................... 565 K.nacc Limitlterator ..... ........................................................................................................... 566 PeK)'PCHBHbie HTepaTopbl ...................................................................................................... 566 PeJJOMe ..........................................................................................................................................567
rJ1aBa 30. 0TpalKeHHH .................................................................................................. 568 HeRBHblil .uocryn K KJiaccaM "Mern.uaM ........................................................................................ 568 HeRBHblil BbIJOB MeTo.ua ........................................................................................................ 568 HeRBHbIH cnttcoK apI)'MeHTOB ...............................................................................................569 l1HCTaHl.lHpoeaHHe KJiaCCOB ................................................................................................... 570 ttcnOJibJOBaHHe HeRBHbIX apI)'MeHTOB ······································ ............. ··············· ................ 570 AnnapaT oTJ)a*eHl1H .....................................................................................................................571 yHKl.lAA: RejlectionFunction ............................................................................................... 572 TiapaMeTJ) QlYffKl.lHH: ReflectionParameter ............................................................................ 574 K.nacc: ReflectionC/ass .......................................................................................................... 574 HacJie.uoeaHHe" OTJ)a*eHHR ........................................................................................ 577 Ceoflcrno KJiacca: RejlectionProperty ................................................................................... 579 MeTO.U KJiacca: ReflectionMethod .......................................................................................... 580 6tt6JIHOTeKa pacwttpeHttR: ReflectionExtension .................................................................... 580 PaJJIH'IHbie YTHJIHTbI: Rejlection ............................................................................................ 581 l1cKJI10'leH11e: RejlectionException ........................................................................................ 581 HepapxHR ...............................................................................................................................582 PeJJOMe ..........................................................................................................................................582
qACTb VI. P Al>OT A C CE Thi() B PHP .................................................................. 583 rJJaea 31. Pa6oTa c HTTP H WWW ........................................................................... 585 3aroJIOBKH OTBeTa .............. ········································ ··············· ..................................................... 585 Bhrno.u JaroJioeKa OTBeTa .......................................................................................................585 Tipo6JieMbI c JaroJioeKaMH ........................................................................................... 585 3anpeT K3wttpoeattHJI ............................................................................................................586 TioJIY'leHHe Bbrne.ueHHbIX Jaro110BKOB ................................................................................... 587 CToJIY'leHHe JaroJIOBKOB Janpoca ................................................................................................... 588 Pa6ora c cookies ............................................................................................................................588 HeMHoro Teoptttt .................................................................................................................... 588 YcraHOBKa cookie ..................................................................................................................589 MaccHBbl " cookie ................................................................................................................. 590 TioJIY'leHHe cookie ................................................................................................................. 591
18
OanaeneHue
Pa36op URL .................................................................................................................................. 591 Pa36!.1em1e H "cKJieHBaHHe" QUERY_STRING ...................................................................... 591 Pa36HeHHe H "cKJieHBaHHe" URL .......................................................................................... 593 flpHMep ..................................................................................................................................594 PeJJOMe ..........................................................................................................................................595
r Jl&Ba 32. CeTeBble tYHKQHH ...................................................................................... 596 aiUIOBbie YHKUHH H IlOTOKH ......................................................................................................596 ITpo6neMbl 6eJonacttocTH ......................................................................................................597 )J;pyrHe cxeMbl .......................................................................................................................598 KoHTeKCT noToKa ...................................................................................................................598 Pa6oTa c coKeTaMH ........................................................................................................................601 "3My1unuu1" 6paYJepa ...........................................................................................................601 He6n0Kttpyiomee tITeHHe .......................................................................................................602 yttKUHH .llJUI pa60T1>1 c DNS ........................................................................................................ 603 ITpeo6pa3oBaHHe IP-a.upeca B .llOMettttoe HMJI H ttao6opoT ...................................................603 Pe3JOMe ..........................................................................................................................................604
rJIBBB 33. IloCblJIKS DHCeM 11.epe1 PHP ....................................................................... 605 opMaT )JieKTPOHHOfO IlHCbMa .....................................................................................................605 0TnpaBKa IlHCbMa ..........................................................................................................................606 flo'ITOBbie rna6JIOHbI ......................................................................................................................607 PacmeilJieHHe JaroJIOBKOB .....................................................................................................608 AttaJIH3 JaronoBKOB ...............................................................................................................609 Ko.!IHpoaKa UTF-8 ......................................................................................................................... 61 l 3aronoBOK Content-type H KO.!IHpOBKa ...................................................................................611 Ko.!IHPOBKa JaroJIOBKOB .........................................................................................................611 Ko.!IHpoaaHHe Tena nHCbMa ....................................................................................................613 AKTHBHbie rna6JIOHbI .....................................................................................................................613 OmpaaKa nuceM c BJJO)KeHHeM .....................................................................................................616 0TnpaBKa nuceM co BCTPOeHHbIMH HJo6pa)KeHHJIMH ....................................................................619 Pe3JOMe ..........................................................................................................................................621
rJiaea 34. YnpaBJJeeee ceCCHHMH ............................................................................... 622 llTo TaKoe ceccHJl? .........................................................................................................................623 3aqeM tty)f(Hbl ceCCHH? ..................................................................................................................623 MexaHHJM pa6oTbI cecc~ ............................................................................................................624 11HHUHaJIUJaUHJI ceccHH ................................................................................................................625 flpHMep HCilOJlbJOBaHHJI ceccHH ....................................................................................................625 YHll'ITO)KeHHe ceccHH ....................................................................................................................626 11.lleHTHlfiHKaTop ceccHH H HMJI rpynilbI ........................................................................................ 627 l1MJ1 rpyllllb1 ceccHfi ..............................................................................................................627 11.lleHTHHKaTop ceccHH ........................................................................................................628 ITYTb K apeMeHHOMY KaTanory ............................................................................................... 629 CTOHT JJH HJMeHJITb rpynny cecc~? .....................................................................................629 YcTaHOBKa 06pa60T11HKOB ceccHH .................................................................................................630 063op 06pa60T11HKOB ............................................................................................................630 PerncTPaUHJI 06pa60T11HKOB ..................................................................................................631 flpHMep: nepeonpe.lleJJettue 06pa60T11HKOB ..........................................................................632 PeJJOMe ..........................................................................................................................................634
OenaeneHue ~CTb
19
VII. PACillHPEHlfj{ PHP ........................................................................... 635
r Jiaea 35. PacmepeHHH PHP ....................................................................................... 637 TioJIKJIJOqeHHe paclllHpemrn ..........................................................................................................63 7 KoHopMaTe RGB ................................................................................................723 CoJ.aattHe HOBoro uaeTa ........................................................................................................723 TeKcToeoe npe.ucTaanetttte uaera .......................................................................................... 723 CToJl)"leHHe 6mt)f(ail:rnero B naJIHTPe uaeTa ........................................................................... 724 31j>lj>eKT npo3pa'IHOCTH ..........................................................................................................725 noJl)"leHHe RGB-cocTaBJillIOlllHX .......................................................................................... 725 J1cnonhJoaaHHe nonynpOJpa'IHhIX uaerne ............................................................................ 726 fpalj>tt'leCKHe npHMHTI1Bbl .............................................................................................................727 KonHpoeaHHe HJo6pa)f(ettHil: ................................................................................................. 727 CTpllMOyrOJibHHKH ........ ································································ ·········································· 728 Bn16op nepa ...........................................................................................................................729 JlHHHH ....................................................................................................................................730 Ayra ceKTopa .........................................................................................................................730 3aKpaCKa npoH3BOJlbHOH o6JlaCTH .........................................................................................730 3aKpaCKa TeKCTYJ>OH ··············································································································731 MHoroyroJihHHKH ..................................................................................................................731 Pa6oTa c nttKCeJlaMH .............................................................................................................. 732 Pa6ora c lj>ttKCHpOBaHHhIMH rnpttlj>TaMH ....................................................................................... 732 3arp)'3Ka rnpHlj>Ta ..................................................................................................................733 CTapaMeTPbl rnpttlj>Ta ..............................................................................................................733 BhIBO.!l CTPOKH .......................................................................................................................734 Pa6ora co rnpttlj>TaMH TrueType ...................................................................................................734 BhIBO.!l CTPOKH .......................................................................................................................734
OenaeneHue
21
Ilpo6JJeMbl c pyccKHMH 6yKBaMH .......................................................................................... 735 Orrpe.11.eneHHe rpaHHU CTJ>OKH ................................................................................................735 KoppeKUIDI YHKmrn imageTtjBBox() ................................................................................... 735 flpHMep ..................................................................................................................................737 Pe3JOMe ..........................................................................................................................................739
rJiaea 39. Pa6oTa c ceTblO ............................................................................................ 740 Ilo.!IKJJJOlfeHHe pacunipeHHl\ ..........................................................................................................740 IlOJI)'lfeHHe TOlfHOro epeMeHH ....................................................................................................... 745 OTrrpaeKa .11.aHHb1x MeTo.aoM POST ............................................................................................... 745 Ilepe.11.aqa nOJJb30BaTeJJbCKoro areHTa ........................................................................................... 74 7 Pe3JOMe ..........................................................................................................................................748
rJiaea 40. Cepeep memcached ..................................................................................... 749 Hac-rpoiiKa cepeepa memcached ...................................................................................................749 XpaHeHHe ceccttll: e memcached .................................................................................................... 750 Y cTaHoeKa coenHHeHHJI c cepeepoM ............................................................................................. 7 51 IloMemeHHe naHHhJX e memcached ............................................................................................... 752 06pa6oTKa OllJH60K ....................................................................................................................... 753 3aMeHa naHHD1x e memcached ....................................................................................................... 7 54 113BJielfeHHe naHHbJX H3 memcached ............................................................................................. 756 YnaneHHe naHHD1x H3 memcached .................................................................................................758 YcTaHOBKa epeMeHH JKH3HH ........................................................................................................... 758 Pa6oTa c HeCKOJJbKHMH cepeepaMH ............................................................................................... 758 Pe3JOMe ..........................................................................................................................................763
1IACTb VIII. 6116JIHOTEKH ................................................................................... 765 rJiaea 41. KoMDOHeHTbl ............................................................................................... 767 Composer: yrrpaeneHHe KOMITOHeHTaMH ........................................................................................ 767 Y cTaHOBKa Composer ....................................................................................................................768 YcraHOBKa e Windows ..........................................................................................................768 YcTaHOBKa B Mac OS 770 YcTaHOBKa e Ubuntu .............................................................................................................. 770 fne HCKaTb KOMITOHeHTbl? .............................................................................................................770 y CTaHOBKa KOMTIOHeHTa ................ ················ ................................................................................ 770 11cnOJJb30BaHHe KOMITOHeHTa ..................... ········ ................................ ··················· ........................ 773 IloJJe3Hble KOMITOHeHTbl .................. ····················· ........................... ··················· ........................... 773 KoMnoHeHT psySH. l1HTepaKTHBm.1fi OTJJa.!llfHK .................................................................... 773 KoMnoHeHT phinx. MttrpaI.OIH ............................................................................................... 775 11HHUHaJJH3alUUI KOMITOHeHTa ................................................ ····················· ·················· 776 ITonroTOBKa MHrpaUHH ................................................................................................. 776 Bb1no11HeHHe Mttrpauttfi ................................................................................................ 779 0TKaT MHrpauttl\ ........................................................................................................... 780 OrrepaUHH co CTon6uaMH ..............................................................................................781 ITonroToeKa TeCTOBbIX .11.aHHDIX .................................................................................... 782 Pe3JOMe ..........................................................................................................................................784
x.........................................................................................................
22
OenaeneHue
r Jiaea 42. CT&B.llBPTbl PSR .......................................................................................... 785 PSR-craH.!1apTu1 .............................................................................................................................785 PSR-1. OcHOBHon craH)].apr KO.LlffPOBaHIDI ................................................................................... 786 PHP-ren1 ...............................................................................................................................786 Ko.!1HpOBKa UTF-8 .................................................................................................................786 Pa3.!1eJleHife o6bJIBJleHHH H BbinOJIHeHIDI nencTBHA .............................................................. 787 TipoC'rpaHCTBO HMeH ..............................................................................................................788 HMeHOBaHHe KJiaCCOB, MeTO.!lOB H KOHCTaHT KJiaCCOB ........................................................... 788 PSR-2. PyKoaoncrao no CTHJDO KO.!la ............................................................................................789 Co6JI10.!IeHHe PSR-1 ..............................................................................................................789 Orcryrn.1 ................................................................................................................................789 aH.rrbt ...................................................................................................................................790 CrpOKH ...................................................................................................................................790 Klno'leBbie CJIOBa ...................................................................................................................790 TipocrpaHcTBa HMeH ..............................................................................................................791 K.naccht. ..................................................................................................................................791 Mer0.!1bI .................................................................................................................................792 Y npaBJIJllOIIlHe crpyiyHKUHH nepeXBaTa ......................................................................................................................882 CreK 6y$epos ................................................................................................................................883 He.uocraTKH "pyqttoro" nepexsaTa ................................................................................................884 11cnOJih30BaHHe o6beKTOB H .uecrpyKTOpOB .......................................................................... 885 Knacc ll.JUI nepexsara Bhixo.uttoro nornKa ............................................................................. 886 He.uocTaTKH KJiacca ................................................................................................................889
OanaeneHue
24
Ilpo6JJeMbl c OTJJa,ll.KOR .................................................................................................................. 889 06pa60Ttu1m 6yepoe .................................................................................................................890 GZip-c)KaTHe ..........................................................................................................................891 fleqaTb JeKTHBHOCTH C)KaTHH ........................................................................................... 892 Pe310Me .........................................................:................................................................................ 894 rJlaea
50. Ko.11.
H ma6JIOH CTpaHHl{bl ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 895
IlepebIH cnoco6: "eKpailJlem1e" HTML e KOll .............................................................................. 895 Brnpofi cnoco6: ecTaeKa KOlla e wa6JJOH ......................................................................................897 Tpenrn cnoco6: Model-View-Controller ................................................................................. 898 Wa6JJoH (View) .....................................................................................................................899 KoHrpoJJJJep (Controller) .......................................................................................................899 MolleJJb (Model) ....................................................................................................................901 83aHMOlleHCTBHe JJJeMeHTOB .................................................................................................902 AKTHBHbte H naccHBHbie wa6JJOHbI ........................................................................................903 AKTHBHhie rna6JJOHbI .....................................................................................................903 IlaCCHBHbie wa6JJOHbl ...................................................................................................904 HellOCTaTKH MVC .................................................................................................................906 4eTeepTbIH cnoco6: KOMilOHeHTHhlfi IlO.!lXOJl ................................................................................ 908 liJJOlfHKypnan1>Hb1e cl>aA.111>1 ................................................................................................................... 103 1 MecTononolKeHHJI ........................................................................................................................ 1034 Pe3ioMe ........................................................................................................................................ 103 7
r JJaea 56. PHP-FPM ................................................................................................... 1038 y CTaHOBKa ................................................................................................................................... 1038 YnpasneHHe cepsepoM ................................................................................................................ 1038 KoH$urypauuoHHble cl>aA.nbl ....................................................................................................... 1039 IlO.llKJIIOqeHHe K Web-cepeepy nginx .......................................................................................... 1042 Pe310Me ........................................................................................................................................ 1044
OenaeneHue
27
rJlaBa 57. A.z:IMRBBCTpHpoeaeae MySQL................................................................. 1045 Y craHoeKa ................................................................................................................................... I 045 YnpaBJ'leHHe cepeepoM .................................................................•.............................................. 1046 KoHcj>HI)'PaUHoHHbIA cj>aA.n cepeepa ............................................................................................ 1047 B1a1.neneHHe naMnH MySQL ....................................................................................................... 1050 IlOJiblOBaTeJibCKHA KOHcj>HI)'PBUHOHHbIA cj>aA.n ........................................................................... 1054 C03.!lBHHe MySQL-noJib30BaTeJieA.............................................................................................. I 055 Y.naJieHHblA .nocyYn K MySQL ............................................................................................. 1055 IlpHBHJierHH.•...•....•..•....•..•................................................................................................... I 056 BoccTaHOBJieHHe yYepJ1HHoro napoJIJ1 ................................................................................. I 059 ITepeHoc 683 .!lBHHblX c o.nHoro cepeepa Ha .npyroA .................................................................... 1060 KonnpoeaHHe 6HHapHbIX cj>aA.nos ........................................................................................ I 060 Co3.!laHHe SQL-.naMna ......................................................................................................... 1061 Pe3JOMe ........................................................................................................................................ 1062
Dpanoac:eeae. HTTP-KOJlLI ••••••••••••••••••.•.•••••••••••••••••••••••••••••••••••••••••••••••.••..••••••••••••••• 1063 Ilpe)lMeTHLJI y1e:aiaTeJ11. •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• 1069
npeAMCnOBMe
C MOMetrra nepeoro H3.lJ.aHIDI KHHrH npoUIJio 6oJlhme IO ner. Hmepuer npeepanmcJI H3 nrpyunrn B pa6oq)10 cpe.zzy, B qacn, noece.n.HeBHOii HHtPPaCTJlYJCTYPbI, uapR.zzy c .n.opora.MH, :meKTpoCeTIIMH H eo.n.onpoBOJJ.OM. npoepbl. OJJ.Ho HJ ttan6onee nepcneICTHBH&IX HanpaeneHHA, KOTOpbie MO:llCer BbIDpan. nporpaMMHCT .llJUI DpHJIO)l(eHHJI CBOHX ycHJIHA. PeeomoQHOHHble H3MeHeHHJI B crrpacJJH, ,IJ.eHbrn, BHHMaHHe rapaHTHpoBaHbl. 11HTepHer npo.n.o.JDKaeT pa3BHBaTbCJI H nepecTpaHBaTLCJI. focy.u.apcTBa, CMM, o6Ulecnm B I.leJJOM HIJlYT cnoco6&1 cymecTeoeaTb B HoeoA cpe.u.e H HoeoA peanLHOCTH. MruuiHOHbl mo.u.eA ocBaHBaJOT HOBLIR MHp. IloKa 3TOT npoQecc H.IJ.eT, a npo.u.;urrc.11 OH ,IJ.eCJITHJieTHJI, Web-pa3pa60T'IHK ecer.n.a mdl,ner TO'IKY npHJio:llCeHHJI ceoeM)' ramurry .
.Qnfl
KOro HanMCaHa 3Ta KHMra EcnH MOlKewi. He nHcaTi. -
He nHWH.
A. fl. l/ex06
KHHra, KOTOp)'lO Bbl .n.ep:>KHTe B pyxax, JIBJYleTCJI B HeKoTOpOM po.n.e )"le6HUKO,'\I no WebnporpaMMHpOBaHHIO Ha PHP. ML1 c.n.enanH nonLITKy Hanncan. ee Tax, qro6LI .IJ.ll)l(e nnoxo no.n.roroBJJeHHLIA qffTaTeJIL, HHKor.u.a He pa6oTaBllJHR B Web H BJJa,IJ.eJOl.llHA JJHlllb OCHOBaMH nporpaMMHpOBaHHJI Ha O,llHOM H3 anropHTMHqecKHX J13blKOB, CMOr 00.II)"IHTb 6oJibllJHHCTBO Heo6XO.IJ.HMLIX 3HaHHA H B MHHHMaJibHble cpoKH HaqaTb npott>eccHOHanbH)'lO pa6ory B Web.
npeoucnoeue
30
3aMeTbTe eme pa.3: Mbl npe,D.nonaraeM, 'ITO Bbl y.»ce 311QKOJl4bl c OCHOBHblMH OOHJITIUIMH nporpaMMHpOBaHHJI H He 6y.ueTe, no KpaAHeH Mepe, nyTaTbCJI B UHICJtax, ycJtOBHblX onepaTOpax, no.unporpaMMax HT. .u. nporpaMMHposaHHe KaK nucoeoe soo6me cna6o CBJIJaHO c KOHKpeTHblM Jl3bIKOM; Hay•rnBUIHCb nHcaTb Ha HeCKOJlbKHX HJIH .UIDKe Ha O.D.HOM-e.UHHCTBeHHOM Jl3blKe, Bbl B .D.aJibHeArneM JiefKO OCBOHTe see OCTaJlbHbie. 3AMEl/AHHE
Bnpo1.1eM, AaJ1Ce ecm1 Bbl 3HaeTe TOnbKO HTML .-. JaH..iMaeTeco eepCTKOlil, To MO>KeTe nonpo6oeaTb oceoITTb aJbl PHP no 3Tolil KHMre. Ho TOT ¢>aKT, 4TO HTML Aem1eTCA R3blKOM pa3MemKu, a He A3b1KOM nporpaMMMpoeaHMA ... He COAep>KMT MHO!l1X nporpaMMMCTCKMX MAelii, CMnbHO ocno>KHMT eaM 3SA84y.
KHHra TaK:lKe 6y.D.eT none3Ha H Y:lKe ycnesrneMy nopa6oTaTb c PHP npoKHO 6eCKOHe'IHO .n:onro.
lf1CXOAHbl9 KOAbl Y.UHBHTeJibHOe Ha6JIIO.UeHHe: CTOHT TOJibKO ,D.aTb 8 KHHre a,npec 3JieKTpoHHOR no'ITbl aBTOpoB, KaK T)'T )l(e Ha Hero Ha'IHHaJOT OpHXO.ZJ:HTb CaMbie pa3Hble nHCbMa OT 'IHTaTeJieH. npe.ZJ:bI)J;yJnaJI eepCHJI KHMTH He 6bIJia 8 3TOM OTHOIIJeHHH HCKJIIO'IeHHeM. OcHOBHYJO Macey nHceM COCTaBJUIJIH npocb6bI BblCJiaTb HCXO,ll.Hble TeKCTbl JlHCTHHfOB. Ilo Mepe CBOHX CHJI Mbl cTapanncb y.n:osneTBopJ1Tb 3TH 3anpocb1, O.D.HaKO, KOHe'IHO, ceAqac TaK npo,D.OJI)l(aTbCJI Y:lKe He MO)l(eT. HMeHHO no 3TOH npH'IHHe a6comomHO .uocrynHbl .D.JUI 3arpYJKH c GitHub.
6Ce
HCXO,ll.Hble KO.Ubl npuee.ueHHbIX JIHCTHHfOB Tenepb
bttps://gitbub.com/igorsimdyanov/pbp7 CucTeMa KOHTPOJIJI eepcun git H cepsHc GitHub no.D.po6Ho ocsemaioTCJI B 211a6e 54. Ecnu Bbl o6Hapy)l(HTe HeTO'IHOCTb mm oneqaTKy, )l(,ll.eM BaIIJHX peKBeCTOB. Bonpocbl H npe.ZJ:JlO:lKeHHJI MO:lKHO pa.3MemaTh B pa3.D.ene Issues.
npeauc/108ue
31
TpeTbe MJAaHMe Bbl .nep)f(}ffe B pyicax YpeTbe H3)laJIHe ICHHrH "PHP 7". CTepsoe H3.llaHHe KHHrH IlOJU'(}TaBJIHBaJlOCb 12 JieT H~ .IUlJI PHP 5. PeJIH3 sepCHH PHP 6 True HHKor.na He yelmeJI ceer, o qeM no.npo6Hee MO)l(HO 6y.ner YJHaTb e 2JIQ6e 5. PeeomoU,HOHHhle HJMeHeHIDI e PHP H B Web-nporpaMMHpoeanHH noYpe6oea.rrn Kap,llHHaJibHoR nepepa60TICH KHHnt. Mhl HanHCaJIH 24 HOBble rJiaBbl, )lOilOJIHHTeJlbHO 9 rnae no.neeprJIHCb cym,ecTBeHHoA nepepa6oTICe. OcTaBIDHecR rnaeb1 6b1JIH n..u:aTeJibHO npopa6oTaHbI H OOHOBJieHbl. nepeqHCJUITb no nyHKTaM H3MeHeHHJI He npe)lCTaBJISleTCSI B03MO)f(Hb1M, H3-3a HX OO'beMa. Yea)f(aeMble 'IHTarenu! ECJIH Bbl XOTHTe c.nenaTb cne,zzyiomee HJ)laHHe 3ToR KHHrH nyqIIJe, noxcanyj{crn, ny6nnicyitre csou JaMeqanHJI no Mpecy:
bttps://gitbub.com/igorsimdyanov/pbp7/issues CTpaICTHKa nOKaJaJla 3$41eKTHBHOCTb TaKoro no)lXo.na.
06~aR CTPYKTYPa KHMrM KHHra cocroHT HJ I 0 qac-reR u 57 rnae. Henocpe.ncTBeHHoe onucanne R3bIKa PHP Ha'IHHaeTCR c 11acmu II. 3TO ofu.RCHReTCR Heo6xo.nnMOCTblO npe)l()le yJHaTb Koe-qTO o CGI (Common Gateway Interface, o6uudt wmoJoeoR HHTepeRc)- 11acmb I. B 11acmu III p83o6panb1 HaH6onee noneJHbie CTaH.napnibie «l>YHKIJ.HH R3bIKa. Cfacmb IV noceRmeHa 061,eKTHOopueHTHpoBaHHbIM BOJMO)l(HOCTIIM PHP, a 11acmb V npe.nonpe.neneHHbIM KJiaccaM. Cfacmb VJ ocseJ.QaeT ceresi.1e B03MO)l(HOCTH PHP, a 11acmb VII 3HaKOMHT c MO.IzyJibHOH CTPYKTYJ>OR HHrepnperaropa u pacwupeHHRMH. Cfacmb VIII nocsRmeHa ynpaeneHHIO H COJ.llaHHJO 6n6nuoreK Ha PHP. Cfacm& IX nocBJlllleHa p831IlfllHbIM npueMaM nporpaMMupoeanm1 ea PHP c MHO)l(ecTBOM npHMepos. HaKoHeQ, 11acm& X oxeaTbmaeT HHCYpJMeHTbI p83pa6oTKH H o6CJIY'1CHB3HHJ1 Web-caDTa. Tenepi. qyn no.npo6Hee o K8JIC.ZJ;oR qacm KHHrH.
"lacTb I B HeR paccMaYpHBaK>TCR reopemqecKHe acneKTbI nporpaMMHpOBaHHR e Web, a TaIOKe oceoei.1 MexaHHJMa, KOTOpbIR noJBOJUleT nporpaMMaM pa6oTaTb B CeTH. Ecnu ei.1 J)l(e JHaKOMbl c 3THM Marepua.rioM (HanpuMep, JaHHMa.rIHCb nporpaMMHposanueM Ha Perl HJIH .npyrnx 13b1Kax), MO)l(ere ee cMeno nponyCTHTb. BKpame MbI onumeM, Ha qeM 6aJupyeTcR Web, qTO TaKoe HHrepcl>eRc CGI, KaJC OH pa6oTaeT Ha HHJKOM ypoeHe, KaK ucnonb3YJOTCR eoJMO)f(HOCTH RJbIKa HTML npu Web-nporpaMMupoeanuu, KaK npoucxo.llHT eJaHMo.neRcTBue CGJ n HTML u MHoroe .npyroe. B npHHmtne, BCR reopHR no Web-nporpaMMHpoBaHHIO KOpoTICo H3JIO)f(eHa HMeHHO e 3ToR 'IacTii KHHrH (H, KaK noKaJMBaJOT OTiblBbl 'IHTaTeJieA KHHrH, noceRmeHHOR npe,llbuzymefi sepcnu PHP, Mnome noqepnuynu cpyn.naMeHTaJibHhle cee.neHHR no Web-nporpaMMHposamuo HMeHHO H3 3TOR qacm). Taic KaK CGI j[BJIJleTCR He38BHCHMbIM OT nnaTif>opMbI uurepeRcoM, MarepHa.rI He "npHBlllaH" K KOHKpeTHOM)' R3bIK)' (XOTSI B npHMepax HCnOJib3YeTCR ll3blK KaK Hau6onee YHHBep-
c
npeoucnoeue
32
caJTbHOe cpe.11.crno nporpaMMHpoBaHIDI). Ec1rn Bbl ue 3Hae-re C, He CTOHT OT'laHsaTbC11: HeMHoroq11cneHHble npHMepbl Ha '.)TOM 113b1Ke He HaCTOJlbKO CJ10)((Hbl, 'tTOObl B HHX MO)((HO 6bIJl0 33JlYTaTbc11.
K
TOMY )((e, Ka)((.LJ.Oe .11.encrnHe no.11.po6Ho KOMMeHTHpyeTc11. OoJlblllHHCTBO
on11caHHbIX 11.LJ.eil 6y.11.eT noernpHo 3aTJ'ouyro e noc11e;zyiom11x rmmax, nocs11meHHblX y)((e PHP. nocne.11.HH rnaea KHHrH nocs11meHa ycTaHOBKe PHP Ha O.LJ.HOH CHCTeM: Windows, Mac
OS X
m
ocHOBHbIX onepamtOHHbIX
H Linux. BcTJ'OeHHbIR Web-cepeep n03eon11eT cpaJy )((e npH-
cTynHTb K Web-pa3pa60TKe. 11MeHHO OH 6y.LJ.eT HCITOJ1b30BaTbC11 Ha npoT11:llCeHHH seen KHHrH BITJIOTb .LlO 'lacmu
x.
4acTb II 513bIK PHP -
y.11.06HbIH H rn6KHH 113blK .1t1111 nporpaMMRposaHHj( e Web. Ero ocHoeaM no-
cs11meHa 'lacmb
II.
C noMOII{blO PHP MO)((HO HanHcaTb
I%
TJ'e6YIOTC11 B 11HTepHeTe. }J.ng OCTaBUierocg
99%
nporpaMM, KOTopi.1e o6bI'tHO
npl1.LJ.eTC11 HCITOJlb30BaTb
c
HJlH Java (HJ1H
.npyron YHHBepcaJTbHbIH 113bIK). Bnpo'teM, .LJ.a)((e 3TO Heo611JaTe11i.Ho: Bbl CHJibHO o6ner1 nHUieTe nOHCKOBYIO CHCTeMy). 00CJ1e.LJ.ffjljl TeMa B 3TOH KHHre He paCCMaTJ'HBaeTCR, flOCKOJlbKY TJ'e6yeT .LlOBOJlbHO 6oJibUIOro onblTa HH3KOypoeHeeoro nporpaMMHpoeaun11 Ha R3b1Ke C, a noTOMY He BITHCblBaeTCR B KOHUenI..lHIO .LlaHHOH KHHrH.
4aCTb Ill f..facmb III MO)((eT 6i.rri. HCITOJ1b30BaHa He TOJlbKO KaK ceoeo6pa3HbrH yqe6HHK, HO nuoKe H B cnpaBO'IHblX uenax BCTJ'OeHHbIX e PHP.
aero. B HeH pacCKaJaHO 0 6oJiblIIHHCTBe CTaH.LJ.apTHblX YHKUHff,
Mi.r
rpynn11poeaJTH YffKIJ.HH e cooTBeTCTBHH c Hx HaJHa'IeHHeM, a Hee
aJTaBHTHOM nopR.LJ.Ke, KaK 3TO HHor.11.a 6bJBaeT npHHRTO B TeXHH'IeCKOH JIHTeparype. qTO )((, .ll)'MaeM, KHHra OT 3TOro TOJlbKO Bb!HrpaJTa. Co.11.ep)((aHHe qacTH BO MHOrHX Mecrax .uy6nnpyeT .LlOKyMeHTaQHIO, conpoBO)((.LJ.alOII{)'lO
PHP,
HO 3TO HH B KOeH Mepe He 03HaqaeT, 'ITO OHa
aen11eTc11 JIHWb ee rpy6b1M nepeeo.noM (TeM 6onee, tfTO TaKOit nepeeo.n ~e cymecTByeT .1t1111 MHornx mas oHUHaJTi.Horo pyKoso.11.cTBa
PHP).
Hao6opoT, Ml.I ni.rranHCb B3rAAHyri. Ha
"KYXHIO" Web-nporpa.MMHpOBaHH11, TaK CKaJaTb, CBe)((HM B3rJljl.l{OM, eme ITOMH11 co6CTBeHHble OUIH6KH H 113i.1cKaHH11. KoHe'IHO, ace YHKl..lHH PHP onncaTi. HeB03MO)((HO (nornM)' tfTO OHH .no6aen.IUOTC11 H coaepweHCTB)'lOTCR OT BepcHH K BepcHH), .na 3TOT'O H He Tpe6yeTCR, HO JibBHHaa .nong npe.nocTaBJI11eMi.rx PHP B03MO)((HOCTeit ace )((e 6y.neT Ha.MH paccMOTJ'eHa.
4aCTblV f..facmb IV noceameHa 061.eKTHo-opHeHTHpoeauHoM)' nporpa.MMHpoeau1110 (000) Ha PHP. oon B PHP ITOCTORHHO pa3BHBaeTC11. EcJIH eme HeCKOJibKO neT HaJa,n npaKTHtfeCKH Bee nporpaMMHOe 06ecnet1eune Ha PHP BbinOJIHJIJIOCb a npoue.nypuoM cTHJie, TO e HacToJ1mHit MoMeHT HeB03MO)((H0 npe.11.CTaBHTb COBpeMeHH)'lO pa3pa6oTKY 6e3 HCnOJib30BaHHJI o6neKTHOopHeHTHpOBaHHblX B03M0)((H0CTeH 113b!Ka. KoMnOHeHTbl, H3 KOTOpblX COCTOHT COBpeMeHHoe npHllO)((emte, 3aBJl38Hbl Ha npoCTJ'aHCTBO HMeH, KJiaccbl H MexaHH3M aeT01arpy3KH.
B PHP 7 H3MeHHJICj( cnoco6 o6pa6oTKH owH6oK,
KOTOpblH Tenepi. HHTerpHpoBaH B MeXaHH3M HCKJIIO'IeHHH.
npeoucnoeue
33
Mbl nocrapanuch nonHee Bhl)l,eJIHTh ece JJ.OCTOHHCTea PHP: coxpbITHJI JJ.aHHbIX, Hacne.noeaHHe, HHTePeitcbI, ~HThl, npOCTpaHCTBO HMeH H aBT03arpy3xy KJiaccoe.
4aCTb V l/acmb v nOCBJII1leHa BCTpOeHHbIM KJiaccaM PHP. oon B PHP nOJIBHJICJI He cpaJy, Il03TOMY npoQecc HHTerpaQHH oon B SllblK 38HJIJI onpe.neneHHoe BpeMSI. B rnaeax 3TOH qacnt pacCMaTpHBaJOTCJI CBJl3b o61.e1Crno-opueHrnpoeauuoro no.llXOJJ.a c JaMbIKaHHJIMH, reHepaTopaMH, o6c)')K.ZJ.aJOTCJI xaneu,napHble KJiaccb1, ureparnphI u Mexauu'.nt OTpIDKeHuit.
4acTb VI OcHoeuasr cneQHaJIHJaQHJI JIJbIKa PHP- pa3pa6oTxa Web-npuno)l(eHHH. Oo3TOMY ceTeBble B03MO)l(HOCTH Jl3bCKa BbI,neJieHbl B OTJ].eJihHylO lfaCTb. fnaBbl lfaCTH no.npo6HO OCBell\aJOT KaK HHJxoypoeHeeyio pa6ory c CeTblO, TaK H o6pa6oTKy cookie, ceccun, ompaexy nolfTOBbIX coo6meHnit
4aCTb VII PHP CTpoHTCJI no MO.l{yJibHOM)' npuHQHny. Mo.nynu-6u6nuoreKH, pa.3pa6oTaHHhie Ha JIJhIKe
C, HaJblBaJOTCJI pacwupeHWLMu. Pa.3pa6oraHo orpoMHoe KOJJHlfecTBO CaMbIX pa.3Hoo6pa.3HbIX pacurnpeHHH, OT o6ecnelfeHHJI ceTeBhlX onepaQHH .IJ.O pa6on.1 c 6aJaMH .IJ.aHHbIX. l/acmb Vil nocBJII1leHa ynpaeneHHJO pacurnpeHIDIMH PHP u oceern.eHHIO mm6onee nonynJ1pHblX HJ HHX.
4acTb VIII Ecnu pacwupeHIDI- 3TO qacn. JIJhuca PHP, To 6u6nuorexu, paJpa6oTaHHbie Ha PHP, HaJhlBaJOTc.11 KOMnoHeHmaMu. MeHe,ZVkep naKeTOB Composer noJBOJIJleT no)J.KJIIO'fan., 3arpY*aTh, ynpaBJIJITh eepcIDIMH 6u6nuoTeK. HeeoJMO)l(HO npeJJ.CTaBHTh coepeMeHH)'lO pa3pa6oTxy Ha PHP 6e3 KOMnoHeHTOB. l/acmb VIII CJJY*HT nyreBO.IJ.HTeJieM B MHpe KOMUOHeHTOB, JJJ.eCh )l(e Mb! HayqHMCJI pa3pa6an.IBaTb H ny6JIHKOBaTb co6cTBeHHhie KOMUOHeHTbl, TaK )l(e ynaKOBhleaTb KOMnoHeHn.1 e ucnonHJ1eMhie PHAR-apxHBhl.
4aCTblX l/acmb IX nOCBJlll.leHa npaKTH'ieCKHM npHeMaM nporpaMMHpoBaHHJI Ha PHP. 0Ha HaChill.leHa npHMepaMH nporpaMM H 6u6JIHOTeK, KOTOpbIC o6Jief'laJOT pa6ory nporpaMMHCTa. B Heit oceell\aeTC.11 pa6ora c XML, JarpyJxa ct>aAJioe Ha cepeep, ucnoJih3oerurne nepeHanpaeneHHsr, TeXHHKa OTJ].eJieHHJI KOJ].a OT ma6JioHa CTpaHHUbl CQeHapIDI, npHeMhl AJAX-pa.3pa60TKH.
4aCTb X 3aKJIIOlfHTeJihHa.ll x 'lQCmb KHHrH OUHCldBaeT ycTaHOBK}' H HacTpoi:txy CpeJJ.CTB pa3pa60TKH Web-nporpaMMHCTa, e TOM 'IHcne yntJJHTbl .IJ.JIJI pa6oThI no SSH-npornxony, cucTeMy KOHTpOJIJI eepcuit Git, eupryanhH)'lO MalllHHY VirtualBox H cucTeMy ynpaeneHHJI eupryanbHhIMH MaWHHaMH Vagrant, ycTaHoexy cepeepa 6aJhI JJ.aHHhIX MySQL, cepeep PHP-FPM H Webcepeep nginx.
npeducnoeue
34
Kaic }')Ke rosopIDiocb paHee, TeKCTbl scex JIHCTHHroB KHHrH .Zl.OC1)11Hbl .ll.JISI 3fil'PY3KH qepe3 git-peno31nopun nee
600!
bttps://gitbub.com/igorsimdyanov/pbp7/issues.
Hx 01.JeHb MHoro -
60-
qTOObl Bbl He 38Il)'TaJIHCb, KaKOA ann KaKOMY JIHCTHHry COOTBeTcTByeT, npHMeHeH
cne.nyiomui:i no.nxo.n.
Cl
Onpe,neJieHHOH rnase KHHrH COOTBeTCTByeT O.UHH H TOJlbKO O,UHH KaTaJIOf B apXHBe C HCXO,UHblMH KO.UaMH. HMsr :noro KaTanora ( OObl'IHO 3TO He 01.JeHb .ll.JIHHHbIH u.o;eHTHHKarop, COCTOSllilHH H3 aHrJIHHCKHX 6YKB) 3arlHCaHO Cpa3y :lKe no.n Ha3BaHHeM rJiaBbl.
Cl
0,nHOMY JIHCTHHry COOTBeTCTBYeT OJlHH H TOJlbKO O.ZJ.HH aAJI B apXHBe.
a
Ha3BaHHSI ecex JIHCTHHl'OB 8 KHHre BbirJISl)lSIT O.UHOTHilHO: "JlHCTHHf HOe Ha3BaHHe. aAn
X".
3.n.ecb
MN-
M.N. Heo6sr3aTeJlbX - HMJI. aAna
3TO HOMep rJiaBbI H JIHCTHHra, a
OTHOCHTeJJbHO meK}'U4e20 Kama!loca 2JIG6bl.
Cl
3arJJaBHe JIHCTHHra npHBe)leHO npsrMO 8 CaMOM itiaAne H oq,opMJieHO B BH,LJ,e KOMMeHTaplUI s nepson CTPOKe:
•
< ! -- • • . ---> -
•
## ... -
.ll.JIR HTML-Kona;
.ll.Jl.SI PHP-nporpaMM.
TaKHM o6pa3oM, MbI y6HJIH cpa3y .n.syx 3aAues: so-nepBbIX, YKa3aJIH JaronoBOK JIHCTHHra s y.no6HOM MecTe, a so-sropbIX, noJBOJIHJIH qHTaTemo cpa3y :lKe Y3HaTb Ha3Ba11He nucTHHra, OTKpbm 8 TeKCTOBOM pe.naicTope COOTBeTCTBYJOIUHH eMY aAJI. Tenepb HeMHOro 0 TOM, KaK HCnOJib30BaTb aHJibl JIHCTHHfOB. DoJlblllHHCTBO HJ HHX SIBJIJlIOTCSI 3aKOH"IeHHbIMH (npas.na, npocTbIMH) cueHapH.srMH Ha PHP, KOTOpbie MO:lKHO JanyCTHTb Ha Bb!IlOJIHeHHe qepe3 TecTOBbIA Web-cepsep. HanoMHHM, qro B 2J1aee
4 ocsemaeTcsr
pa6oTa
BCTPOeHHoro Web-cepsepa PHP, KoTopb!A MO:lKeT 6b1Tb 3an)'llleH B mo6oi:i onepauHOHHoA cucTeMe. TaKHM o6pa3oM, .ll.JISI npose.n.eHHsr 3KcnepHMeHToB c JIHCTHHraMH BaM JlOCTaTO'IHO npocro pa3BepH)'Tb apxus B no.nxo.zunl(HA ICaTSJior.
npeAM9THblM yKa3aTenb KHHra, Koropyio Bbl nep:lKHTe B pyKax, co.nep:lKHT npaICTH'leCKH ucqepnblBaJOmHA yKaJaTeJib (HHJleKc) no sceM ocHoBHbIM KJIJO'leBbIM cnoBaM, BCTPeqaJOII(HMcsr B TeICcre.
B
HeM, noMHMO
npoqero, npHBO,n.srTCSI CCbUTICH Ha see pacCMOTpeHHble 4'YHKUHH H ICOHCTaHTbl, JlHpeKTHBbl PHP n MySQL, KJIJO'leBbie TepMHHbI H noHRTH.SI, BCTPeqaJOll.\Hecsr B Web-nporpaMMHpoBaHHH. Mb1 nocTapaJIHCb c.nenaTb npe.D;MeTHbIA YKaJaTeJJb y.n.o6HbIM .ll.JIJI nosce)lHesHoro ncnoJibJoBaHH.SI KHHrn B KaqecTBe cnpasoqHHICa.
6naroAapHOCTM OT AMMTPMH KoTepoea Pe.n.Kasr KHHra o6xo.nuTCsr 6eJ 3Toro npusrTHoro pa3.nena. Mb1 He cTaHeM, no:lKanyA, .n.enaTb HCKJIJOl.JeHHH H nonpo6yeM 3)leCb nepe"IHCJIHTb scex, ICTO 0Ka3aJI TO HJIH HHoe BJIHSIHHe Ha xo.n HanucaHHSI KHHfH.
npeaucnoeue
35
3AME'4AHHE
K CO>KalleHllllO,
nplllSITHOCTb AaHHOro MOMeHTa OMpa4SeTCR TeM cpaKTOM, 4TO B Cllll1Y 11111HetiiHOCTlll
noeeCTBOBaHlllR nplllXOAlllTCR KaKllle-TO lllMeHa YKa3blBaTb BblWe, a KSKllle-TO Hlll>Ke no TeKcry. BeAb nOPRAOK Cl18AOBaHlllR lllMeH nQA48C He lllMeeT Hlll4ero o6~ero c 4111Cl10M "38cnyr'' Toro 11111111 lllHOro 4e11oeeKa. BAo6aaoK, ecerAa cy~creyeT p111CK Koro-To 3a6bfTb - HenpeAHSMepeHHO, KOHe4HO. Ho y>K nycrb 11y4we 6yAeT TaK, 4eM coeceM 6e3 611aroAapHocretii.
Xo11eTCJ1 npe)fC)J.e ecero no6naro.zi;apHTb 'IHTarenen nepeoro H3.l{aHHJI KHHm "PHP 5", aKTHBHO yqacTBOBaBlllHX B ucnpaBJieHHH HeTO'IHOCTeit Bcero Ha opyMe KHHrn 6bIJ10 ony6JJHKOBaHO oKono 200 oneqaroK u ucnpaeneHun! Mb! Ha.l{eeMcR, qro 6naro.zi;apR 3TOM)' KHHra crana 3Ha'IHTeJlbHO TO'IHee, H O)KH,l{aeM He MeHbUieA aKTHBHOCTH OT l{HTaTeJleH )1,JUI .l{aHHOro H3.l{aHHJI. Or.zi;eJibHbIX cnoe 6naro.zi;apttocm 3aC.1l)'IKHBaJOT pa3pa6onHKH J13b1Ka PHP, e COTPYJJ.HH11ecTee c KOTOpblMH 6b1Jla HanHcaHa .l),aHHaJI KHHra. {B03MO'.lKHO, Bbl Yllbl6HyJIHCb npu npO'ITeHHH 3TOro npe)l;JIO:lKeHHJI, O.l{HaKO IlO.l), 11 COTPY.lJ.HH'leCTBOM" Mbl 3.zi;ecb IlOHHMaeM eoece He CaMO co3.zi;aHue HHTepnperaropa PHP! Pe11b H.zi;eT o KOHCYJJbTal.lHRX no 3JJeKTPOHHoA no11Te u OBycmopoHHeu CBJl3H aeropoe KHHfH c nporpaMMHCTaMH.) Oco6eHHO XOTeJlOCb 6bl BbI.l{eJJHTb pa3pa6oTlfHKa MO)J;yJlJI DOM Po6a Pu11ap.zi;ca (Rob Richards). KpoMe Toro, MHome .zi;pyme pa3pa60T11HKH PHP (HanpuMep, MapK)'c 6oeprep (Marcus Boerger), HJibJI AJJblllatteQKHH (Ilia Alshanetsky), ,nepuK PeTaHc (Derick Rethans) u .zi;p.) onepaTHBHO ucnpae.rumu OlllH6Ku e HHTepnpernrope PHP, uaA.zi;eHHbie aeropaMH KHHm e npouecce ee HanucattHJI. XoqeTCll TaKJKe no6naro.zi;apHTb KOJlJieKTHe op)'Ma http://forum.dklab.ru, oT.zi;eJibHbie yqaCTHHKH KOTOporo HanpRM)'JO BJIIDIJIH Ha XO.l), 11 lllJIHoBKH 11 KHHrH. HanpHMep, lOpHH HacpeT.l{HHOB npoqHTaJJ H npOKOMMeHTHpoBaJI Hal{aJJbHbie eepcHH rnae npo perynRpHble Bblpa:iKeHHJI u MySQL, a TIUOKe BblCKa3aJJ MHOJKeCTBo ueHHbIX 3aMeqaHHH no ynyqllleHmo nocne.zi;ttuif rnaeb1 KHHm, noceJ1meHHOH AJAX. AHTOH CYU\eB u Hnb.zi;ap lUaifMop.zi;aHoe noMoranu aeropaM e .zi;opa6oTICe npeouc!l06UR, Koropoe Bbl ceii11ac 'IHTaere. HaKoHeu, MHome yqacTHHKH opyMa e Toii HJJH HHon creneHH yqacTBOBaJIH e o6cy)fC)J.eHHJIX HacYU\HbIX eonpocoe, OTBeTbI Ha KOTOpbie BOlllJIH B KHHry, a TaIOKe 3aHHMaJJHCb noMep:lKKOn npoeJCTa "~eHTJtbMeHCKHif Ha6op Web-pa3pa6oT'IHKa", no3eonJ1iomero 6b!CTPO ycTaHOBHTb Apache, PHP, MySQL HT. .zi;. )l;Jlll OTJJMKH cpa3y HeCKOJJbKHX CaHTOB e Windows. HaKoHeQ, Ha.M xorenocb 6b1 no6naro.zi;apHTb COTPY.zi;HHKa H3.U.aTeJibCTBa "6XB-0eTep6ypr" EereHHJI Pb16a1eoea, Koropbtn cTon1eo BbIHOCHJl see MbICJIHMble u HeMblCJJHMbie npeebtllleHHJI cpoKoe c.zi;a11u MaTepuana, a TaIOKe repnenueo oTBe11an Ha HaWH nucbMa H eonpocb1, eo3HH1eaeurne no Mepe HanucattHJI KHHm.
6naroAapHOCTM OT MropR CMMARHOea Pe.u.Ko nporpaMMHCTa MO'.lKHO 3aCTaBHTb nucaTb CBJl3aHHbie KOMMeHTapHH, He roeopR )')Ke 0 TeXHH'leCKOM TeKCTe. 0po6paeWHCb 11epe3 6apbepbl Jl3b1Ka, TeXHOJlOrHA, OTJla.l{KH KO.l{a, OHH HaCTOJibKO norp)')KaJOTCJI B MHp KO.zi;HpoBaHHJI, 'ITO BbITaIUHTb HX H3 Hero H 3aHHTepecoBaTb 'leM-TO .u.pyrnM He npe.zi;cTaBJIJleTCJI B03MO:lKHbIM. l-13naraTb CBOH MbICJlH, 11 8H.l{eTb 11 TeKCT CKOpee He HCK)'CCTBO HJIH OCOObln .zi;ap - 3TO pa6oTa, KOTopyio Heo6xo.zi;HMO OCBaltBaTb. BbmOJIHJITb 3ry pa6ory yqaTCJI JlH6o caMOCTOJITeJJbHO, nu6o c HacTaeHHKOM. 51. 011eHb 6naro.zi;apeH 3eneHuoey Cepreio BacHJti.eeuqy, MoeM)' Hayq-
npeiJucnoeue
36
HOM)' PYKOBO.lJ.HTemo, KOTOpbttt no-rpanUJ 6eJyMHoe KOJIH'tecTBo speMeHH B nonbITKax Ha)"IHTb Meusr IlHCaTb. BrnpoH 11eJIOBeK, 6Jiaro.l1,ap'1 KOTOpOMY Bbl CMOrJIH YBH.lJ,eTb :ny H MHo:>KeCTBO .D.pyrnx KHHr, 3TO MaKCHM KyJHeuos nporpaMMHposaHHe -
HayKa, soitHa, Me.ZJ.HUHHa, nc11xonorm1, XHMIDI, HJHKa MYJbtKa,
.lJ.JUI Hero He 6bmo JanpeTHbIX o6nacTeH. OH seJ.lJ,e 6b1Jl cson H 'IYB-
CTBOBaJI ce6sr KaK pb16a B BO.ZJ.e. Ou 6blc-rpo )l(HJI H )l(HJHh ero 6htCTpO JaKOH1IHJiach. OcTaJIHCh cnaceHHble HM JIIO.ZJ.H. 3Ta KHHra B TOM 'IHCJie H ero .D.ennne. OT.D.eJibHO xoTeJI 6b1 no6naro.D.apHTb scex .D.pYJeH-nporpaMMHCTOB HJ KOMaHJJ.bI GoPromo
(http://go-promo.ru), c KOTOpblMH MHe .ZJ.OBeJIOCb pa6oTaTb IlOCJie)J.Hee BpeMJI Ha.D. caHTOM raJeThI "J·hsecrnsr" (http://izvestia.ru) H TeJieKaHana "JlaiiHbioc" (http://lifenews.ru). Hx npoeccHOHaJIH3M, CTHJlb pa60Tbl CHJlbHO IlOBJIHSIJIH Ha MeHJI H, KOHe'IHO )Ke, Ha CO.l1,ep)KHMOe 3TOH KHHrH. Ka)l(.ZJ.oro 6bl XOTeJIOCb no6Jiaro)J.apHTh OT.D.eJibHO:
D
Ba.ZJ.HMa )J(apKo, 6naro.D.ap'1 KOTOpoMy sr HaKOHeU-TO nopsan nocJie.D.HHe cssrJH c Windows, norpy3HJIC'1 B MHp
D
UNIX
H no'IyBCTBOBaJI ce6sr no-HacrnsrI.QeMy 11 .ZJ.OMa";
AneKcaH.D.pa MypaBKHHa, KOTOpbIH noJHaKOMHJI MeHsr c Y.ZJ.HBHTeJihHhIM MHpoM Ruby H Ruby on Rails. 3To nepesopa'IHBaeT, 3TO noJBOMeT no-JJ.pyroMy CMOTJleTb Ha nporpaMMHposaHHe, B TOM 'IHCJie Ha PHP-nporpaMMHposaHHe;
D
J],MHTJlH'1 M11xeesa, c KOTop1>1M Mb! nposeJIH Muoro '!eJioBeKo-JieT Ha.D. OTJia.ZJ.KOH neracHKOJJ.a H pa6oTOH Ha.D. XHTJlOYMHbIMH SQL-KOHBepTepaMlf, 6Jiaro.l1,ap'1 KOTOpb!M Jieraclf-KO.ZJ. npeBpaI.QaJICJI B H3SII.QHbie, IlOKpblTble TeCTaMlf If KOMMeHTapHJIMlf ClfCTeMbI;
D
ApTeMa H1tc-rparnsa, 3HU1tKJ1one.D.H'!ecK1te JHaHIDI 6yKBaJibHO
o6o
sceM B IT-M1tpe no-
CTOSIHHO cJiy)iclfJilf H cJiy)KaT lfCTO'IHlfKOM B.D.OXHOBeHlfJI. 11HOr.D.a, Ka)KeTCJI, HeT TaKOH npo6JieMhl, c KOTopon OH He MO)KeT cnpaBITTbCJI, H HeT TaKOro Jl3b!Ka nporpaMMlfpoBaHHJI " cepsepa, o KornpoM 61>1 OH He cMor npo'IHTaTb He60J11>lll)10 neKumo;
D
J],eHHca MaKcHMOBa, HasepHoe, TOJibKO 6naro.l1,ap,11 ero no-rp,11caio1.Qett pa6orncnoco6Hocrn H yMeHlflO rny6oKO norp~aTbCJI B JII06yio npo6JieMy y MeHJI IlOJIBlfJIOCb BpeMJI Ha 3TY KHHry;
D
AneKCeJI AB.D.eesa, reHHJI poHT-pa3pa6oTKH, 6naro.D.ap'1 KOTOpoMy JI eI.Qe He Tep'110 cnoco6uocTb op1teHT1tpoBaTbC'1 B Mlfpe COBpeMeHHblX JavaScript-peHMBOpKoB;
D
AJieKCaH.l1,pa rop6yHoBa, 1Ibe IlOTJl'1CaJOI.Qee '!YBCTBO IOMOpa He .lJ,aBaJIO onycKaTh PYKH npH C03)1.aHlflf 3TOH KHlfrn, a cnoco6HOCTb CMO-rpeTb Ha BeI.Qlf 00.ZJ. CaMbIMH HeO)l(H.ZJ.aHHblMlf yrnaMH, 6eJyCJIOBHO, OCTaBHJia HeH3rJia.D.HMbm CJie)J. Ha TeKCTe;
D
AneKcaH.D.pa I>o6Kosa, 6ecKoue11uoe TepneuHe " cnoco6HocTh pa3rpe6aTb asrneBbI KOHIOlllHH TeXHOJIOrH'IeCKOro .lJ,OJlra c 0)1.HOii CTOpoHbl B,D.OXHOBHJllf uan1tcaTb KHHry no coBpeMeHHOMY PHP, a c .n.pyron noJBOJilfJIH BhI,D.eJIHTb Ha 3To speMJ1.
JJ,e110BaJ1 If .n.py)KeJ1106HaJ1 aTMocepa B KOMaH.lJ,e GoPromo, ceM1rnapbl H lllKOJia nporpaMMlfposaum1, Kornpyio MbI BeJJ.eM .lJ.JUI scex )KeJiaJOI.QlfX, HanOMlfHaeT aTMocepy uay11Hoii na6oparnpHH, B KOTOpoit Ha'IaJiaCb MOJI Kapbepa nporpaMMHCTii. 0T.!{eJibHO
6naro.l1,apHOCTH
softtime.ru/forum:
JaCJIY)KHBaJOT
'IlfTaTeJIH
H
IlOCeTHTeJIH
opyMa
http://
salllH sonpocbl, JaMe'IaHHJI H no)KeJiaHHJI scer.n.a 011eHb Ba)l(HbI. JJ.a)Ke
eCJIH Mb! He OTBe1IaeM Ha HlfX cpaJy, OHH IlOTOM HaxO,D.JIT OTJ)a)KeHHe Ha c-rpauuuax KHHr. KoHeqHo )l(e, orpoMHoe cnacu6o Moeii )f(eHe AneHKe, KOTopM, 06J1a.D.aJ1 HeyeMHbIM xapaKTepoM, TepneJIHBO )K,D.aJia, Kor.n.a 6y.n.eT JaBeprneHa KHHra.
npec)ucnoeue
37
Or.uenbHaJI 6naro.napHOCTb ,[{MHTpHIO Korepoay 3a TO, 'ITO no3aOJIHJI .nenaTb co ceoen KHHroil ace, 'ITO yro.uuo - y,namtTb, .D;o6aBJUITb, nepeMeW.aTb rnaebI. 4HTaJI HecKOJibKO JJeT HalaJJ: KHHI)' OCHOauoro 11 KOHKYJ>eHTa11 , MHe acer.D;a XOTe.IIOCb "ue MHoro" nonpaeHTb ee. He .LzyMan, 'ITO 3TO Kor,11,a-HH6y,11,b 6y.D;eT a03M0)Kff0, KoHe'IHO )l(e, orpoMHaJI 6naro,11,apuocn. H3.D;arenbCTBY "EXB-IleTep6ypr" H oco6eHHo EareHHIO Pb16aKOBy, KOTOpbm repnen uac, MOJJO.D;bIX aeropoa, ace 3TH fO.llbl H npO.!l;Oil)l(aeT Tepnen.. Ee3 ero ycunun H ycunHD eceil KOMaH.IJ.bI H3.D;arenbCTBa poccnncKoe IT-coo6w.ecTBo He .llOC'IHTanOCb 6bI MHO)l(CCTBa KHHf.
npeAMCnOBMe K nepeoMy M3A8HMIO
.l{aHHWI KHHra llBJIJleTCJI ,llaJI&HeitlnHM palBHTHeM H .llODOJIHeHHeM H3,llaHHJI "CaMOY'fHTeJlb PHP 4" 1, nepepa6oTaHHhlM H .nononHeHHhlM HOB&IM MarepnanoM, KacaJOLQHMCll BOJMo:lKHocreA PHP sepcHH 5. Kaic MHoro 6&l11o HJMeHeHo H .no6a.BJieHo? Ha 3TOT sonpoc MO:lKHO oTBeTHT& Tax. Hos&1e (no cpasHeHmo c caMoyqnreneM) cse.neHHJI cocTaBrunoT npHMepHo nonoBHHY 061.eMa .llaHHOit KHHrH H, rJiaBHhlM o6paJOM, CKOHUeHTpHpoBaHbl B DOCJie,llHHX TpeX qacTllx. 60JibWHHCTBO mas TaJOKe 00.IlBeprnHCb cep&e3HOA .nopa6oTKe, B OCHOBHOM HanpasneHHOit Ha onncanne HOBIJX BOJMo:lKHocreA PHP 4 H PHP 5, noJ1BHBWHXCJ1 B llJ&IKax c MoMeHra B&txo.na nepsoro HJ.llaHHJI. KoHeqHo, HeJI&JJI secTH palroBop o nporpaMMHpOBaHHH, He no.nKpeIIJIJlJI ero KOHKpeTH&IMH npHMepaMH Ha TOM HJIH HHOM anropHTMH11ecKOM llJ&IKe. Ilo3TOM)' rJiaBHaJI Ja,ztaqa KHHrH no.npo6Hoe onncanae llJ&IKa PHP sepcnA 4 H 5, a TalOICe HeICoTOphlX y.no6H&IX npneMoB, no3BOJUl10lllHX C03,ZJ,a&aT& KaqecTBeHH&re Web-nporpaMM&I Ja KOpoTKHe cpoKH, nonyqaJI npoJzyKThl, nenco Mo.nnctinuupyeM&Ie H no.ll,llep:11CHBaeM&1e B 6y.zzyuteM. OcHOBHOA o6DeM MaTepnana KHHm npHMeHHM ICaIC K PHP 5, TaIC H K PHP 4. PaJJIH1e 003BOJIJIT B 3HaqHTeJibHOA creneHH o6nef"'l.HTb nporpaMMHpOBaHHe.
B TeKCTe MHoro o6meljmnoco«f>cKHX pacc~eHnif Ha TeM)' "KaK Morno 6&1 6&ITb, ecnn ... " HJIH "'ITO c,ZJ,enann 6&1 caMH a.BTOpbl e 3TOA cnryaunn ... ", OHH o6&1'1Ho ocpopMJieH&I e BH.lle npnMe'laHHfi. J.iHor,ZJ,a Mbl no3BOJIJleM ce6e nucaT& He o TOM, qro ecT& Ha cawoM oeJ1e, a KaK no ,wo2110 6bl 6blmb e 6onee 6naronpHJ1THblX o6croJ1Ten1>cTeax. 3,ZJ,ec& npuMeHJleTCJI MeTO.ll,:
1
KOTepOB ,Ll. B.
CaMO)"IHTCJlb
PHP 4. -CTI6.: 6XB-Tierep6ypr, 2001.
40
npeoucnoeue K nepeoMy U308HUIO
"paCCKWKH CHa11ana npoCTO, nycTb H He COBCeM CTpOro H TO'IHO, a 3aTeM nocreneHHO ,l\eTaJIH3HpyR, ocsemrur no,l\po6HOCTH, onymeHHble s npoumhlil pa3". no csoeMy on&rry JHaeM, 'ITO TaKOR CTHJib noseCTBOBaHHJI 11ame scero OKa3blBaeTCJI ropa3,ll0 6o11ee nJIO,l\OTBOpHblM, 11eM CTpOroe H cyxoe onHcauue rucros. Em.e pa3: aerop&1 He cTasHJIH ce6e 1.1e11&10 HanHcaTb Hctiepn&IBaJOl.Qee PYKOBO.l\CTBO s onpe,lleJieHHoR o611acTH H He CTpeMHJIHCb onHCblBaTb see MaKCHMaJibHO TO'IHO, KaK B y-qe6HHKe no MaTCMaTHKe, -
Hao6opOT, BO MHOrHX Mecrax Mbl
DbITaJIHCb OTTaJIKHBaTbCJI OT yM03pHTeJlbHblX paccy)l()leHHA, 803MO)l(HO, HeMHOro H He COOTeeTCTByJOl.QHX HCTHHe. 0cHOBHOH 00,llXO,l\- OT 'lacTHoro K o6meMy, a He HaOOOpoT. KaKHHKaK, "H3o6pereHHe BeJIOCHDe,l\a" HCDOKOH BeKOB C'llrICUIOCb JIYtIWHM npHeMOM ne,l\arOrHKH.
qACTb I 0CHOBbl
Web-nporpaMMMpoeaHM.R ~
- - !--:= -- -- -- npMH1..41.mbl pa60Tb1 "1HTepHeTa J
rnaea 1.
~-1------"1HTepQ>e'1c CGI 1r1 npoToKon HTIP
I rnaea 2.
·--TE- - - - I rnaea 3.
f-- - -
CGI M3Hyrp1r1
I
--1
-- -- ---- - - -
~naea 4. ~poeHHblM ~-pee~ PHP _
_J
rnABA 1
Cen. HHrepHCT npe.nCTilBJIJler co6oR MHO)l(eCTBO K0Mn1>10repoe, coe.nHHeHHbIX .npyr c .npyroM Ka6eJUIMH, a TIUOKe pa.wlOKaHaJlaMH, CnyrHHKOBbIMH kaHWlaMH HT. .n. 0,nHaKO, KaK H3BeCTHO, o,nHHX npoeo.noe HJlH paJlHOBOJIH .llJUI nepe.na•rn HHcpopM&UiH He.nocTaroqHo: nepe.llaJOllleR H npHHHMaJOllleR CTOpoHaM HeOOXO,llHMO npH.nepJKHBaThC.11 p.11,lla cornaweHHA, no3BOJUllOIIUIX CTporo perm1MeHTHpoean. nepe.naqy ,naHHhlX H rapaHTHpoBaTb, qro :na nepe,naqa npolt.AeT 6e3 HCKWKeHHR. TwcoA Ha6op npaeHJI HaJbIBaeTC.11 npomoKOllOM nepeoa11u. YnpollleHHO, npoTOKOJI - 3TO Ha6op npaeHJI, KOTOpbdt noJBOJI.lleT CHCTeMaM, BJaHMOn.eAcTByx>UU™ B pa.MKax CCTH, OOMeHHBaThC.11 ,lla.HllblMH B HaH6oJiee y,no6HoA .llJUI HHX cpopMe. Cne~.11 cJioXCHBmeAc.11 e no.no6Horo po.na KHHraX TPBJUfilHH, Mbl BKpame paccKWKeM, 'ITO )Ke co6oR npe.ncTilBJIJOOT OCHOBHble nporoKOJibl, HCDOJiblyeMbie B HHTepHere. 3AME'IAHHE HHc;>rAS Mbl 6yAeM H83b1118Tb HHTepHeT "Cen.IO" c 6onbWOA 6yKBbl, B OTm1~Me OT "ceTM" c M8neHbKOA 6yKBbl, KOTO~ o6o3HS...aeTC11 aoo'5~e mo6aA CeTb, noKanbHSA MnM rno6&nbH8A. TYT CMTy81.4MA CXOAHI co cnoaoM "ranatm1Ka": Hawy r&n8tm1K}' ~8CTO H83b1B8IOT ranatm1KOH c nponMCHOA 6yKBbl, a "r&l18tm1K8" co CTp()"lHOH 6yKBbl COOTl8TC'TByeT nl06oA ApyroA 3183AHOA CMCTeMe. Ha caMOM Aene, CXOACTllO CeTM M ranatm1KM MAeT HeC1ConbKo A&nbwe opQ>orpacpMM, M, AYMaeM, Bbl C«opo T8IOK8 npoHMKHeTecb 3ToA Mblcnbt0.
npoTOKOnbl nepeAa~M A8HHblX Heo6xo,nnMoCTh HeKoTopoA CTaH,llapntJa!UiH noeHJiaci. qyn, JIH He c caMoro MoMeHra eoJHHKHO&eHH.11 KOMnbKYrepHbIX cereft ,I:leACTBHTeJibHO, no,nqac o,nHoR cen.10 o6De,nnHeHbI KOMilblOTepbl, pa60Till01IlHe no.n ynpa&JieHHeM He TOJibKO p83JIH'IHblX onepauHOHHhlX CHCTeM, Ho Hepe,nKo HMeJOlllHe u coeepmeHHO p83JIH11ffYJO apXHTeKTYPY npoueccopa, opraHHJa1..lHJO Da.MJITH H T. ,ll) HMeHHO .llJUI roro, 'lrOObl o6ecnequean. nepe,naqy Me)l(,lly TaKHMH KOMDblOTepaMH, H Dpe,llHaJHa'leHbl BCeBOJMO)l(Hble npOTOKOJibl. ,I:laeaRre pacCMOTPHM 3TOT 80npoc qyn, no.npo6Hee. PaJYMeerc.11, .llJUI paJHbIX ueneii CYllleCTBYJOT p83JIH11Hb1e nporoKoni.1. K cqacri.10, HaM He H~O HMeTh npe,nCTaBJieHHe 0 KWK,llOM H3 HHX - .nocraroqHO JHaTh TOJibKO TOT, KOTOpbiil MbI 6y.neM HCDOJiblO&aTh e Web-nporpa.MMHpOBaHHH. Twcoei.IM .nn.11 Hae .11BJI.11erc.11 npomoKo!l TCP (Transmission Control Protocol, nporoKoJI ynpaeneHH.11 nepe,naqeif ,naHHblX), a roqHee, npomoK011 HITP (Hypertext Transfer Protocol, npoTOKOJI nepe,na'IH rnnepreKcTa), 6aJH-
44
l/acm& I. 0CH06bl Web-npoapaMMUp068HUR
PYJOW.Hik.11 Ha TCP. IlpoTOKOJI HTIP KaK paJ H JMeilcreyeTc.11 6pay3epaMH H Web-cepeepaMH. 3aMeTbTe, IJTO O.z:tHH npoTOKOJI MmKeT HcnoJibJOBaTb a ceoeii pa60Te .ll.pyroii. B MHpe HHTepHeTa :na CH"f)'fillHll llBJilleTc.11 coaeprneHHO o6b111Hoii. 1.faw.e acero KIDK.nhlH HJ npoToKonoe, yqacTB}'lOW.HX B nepe)J.aIJe )J.aHHblX no CeTH, peanHJyeTc.11 B BH)J,e OT)J.eJibHOro H no B03MO)f(HOCTH HeJaBHCHMOro npOrpaMMHOro o6ecne11eHH.11 HJIH .ll.paiteepa. Cpe.ll.H HHX CY.JlleCTByeT HeKotopwi HepapXH.11, KOrJ{a OJ{HH npOTOKOJI llBJUleTCll acero JlHlllb "HMCTpOilKOH" HM .ll.PYrHM, TOT, B CBOIO 011epe)J.b - HM TpeTbHM, H T. )'.{. J{O CaMOro 11 HH3KOypoeHeBOro" J{paiteepa, pa60Ta10w.ero y:>Ke HenocpeJ{CTBeHHO Ha tl>H3HIJecKOM ypoeHe c cereBblMH KapTilMH HJIH MOJ{eMaMH. Ha pHc. 1.1 npHBeJ{eHa npHMepHa.11 cxeMa npm.{ecca, npo11cxo,z:vmwro npH OTnpaeKe Janpoca 6pay3epoM noJib30BaTeJUI Ha HeKOTOpbIH Web-cepeep e HHTepHeTe. IlpllMoyroJibHHKaMH o603HaIJeHbl nporpaMMHble KOMnoHeHTbI: .ll.PaHBepbl npoTOKOJIOB H nporpaMMbla6oHeHTbl (nocneJ{HHe BblJ{eJieHbI :i1rnpHb1M rnpH4>roM), HanpaaneHHe nepeJ{a11H J{aHHblX yKaJaHo CTpeJIKaMH. KoHeqHo, a )J.eilcTBHTeJibHOCTH npoQecc ropaJ.ll.O CJIO)l(Hee, Ho HaM cen11ac HeT Heo6XOJ{HMOCTH Ha 3TOM OCTaHaBJIHBaTbC.11. 06paTHTe BHHMaHHe, IJTO B npe.LJ.eJiax KIDK)J:OJ:i CHCTeMbl npoTOKOJlbl Ha cxeMe pacnoJIO)l(eHbl a BH.LJ.e "cTonKH", O.ll.HH HM .ll.pyrHM. TaKa.11 crpyicrypa o6ycnaBJIHBaeT TO, 11To qacro ceMeiiCTBO npornKonoe o6MeHa J{aHHbIMH e HHTepHere HaJbIBaJOT cmeKo.M TCP/IP (cTeK e nepeeo.LJ.e c aHrnHACKOro KaK pa3 H 0003Ha11aeT "cTOnKy").
1--··········-----····----··-··---1 i I I I :
~
-..-...,:::~~::-"~".,::-'
"t.0:.-. , , , ..
:;:
~.:'
-
- l
"1--1'".,.KeM, npoTOKOJI IP (KOTOpbill o6ecne11HBaeT HeCKOJibKO 6onee npocToit cepeHc no cpaeHeHHJO c TCP, HanpHMep, He rapaHTHpyeT .ll.OCTaBKY coo6meHHR Mpecary) He HcnoJib3YeT B03MO)l(HOCTH npoToKona TCP, a TCP, a caoto 011epe.ll.b, "He .LJ.OfMhTBaeTC.11" o CYIQeCTBOBaHHH npoToKona HTIP (HMeHHO ero Ja.LJ.eJ:icTBYeT 6pa)'3ep H noHHMaeT Webcepeep; Ha cxeMe npornKon HTTP He o60JHa11eH).
fnaea 1.
45
npUHl,.fUnbl pa6ombl "1Hm8pH8ffl8
OpnMeHeHne Taxoif opraHHla.QHH noJBOJUleT 3aMeTHO ynpoCTIITb
ry
'lacTh onep3.l.(HOHHOR
cncTeMbI, KOTOpaJI omeqaeT Ja noJ],Aep:lKKy pa60Tbl c ceTuio. Ho He nyrai\recb. Hae 6y.IJ.eT HHTepecoeaTb B KOHe'IHOM HTOre ecero JIHIITh npoTOKOJI CaMOro BblCOKOl'O ypoBIDI,
11
B03Bbl-
lllalOW.Hhcx" Ha,zJ. eceMH OCTaJibHbIMH npoTOKOJiaMH, T. e. HTTP H TO, KaIC oH BJaHMO.z:telfcT· eye-r c nporoKoJioM TCP.
CeMeMcTeo TCP/IP BoT )':llCe HeCKOJibKO .z:tecxTKOB JieT OCHOBHOA npOToKoJI HHrepHeTa- TCP. Kax qacro 6b1BaeT, 3TOT Bbl60p o6ycnoBJieH CKOpee HCTOpH'leCKHMH npH'IHHaMH, He:lKeJIH )J.eRcTBHTeJlbHblMH
npeHM~ecTBaMH nporoKona (enpoqeM, npeuM~ecTe y TCP TaIOKe npe.IJ.OCTaTO'IHO). OH HH e Koeit Mepe He npereH,IzyeT Ha ponb HH3KoypoeHeeoro- Hao6opOT, e ceoio pa6ory TCP eoeneKaeT .IJ.PYrHe nporoKOJibI, uanpnMep IP (a ceoio oqepe.Il.b, IP TIUOKe 6aJupyeTCx Ha ycnyrax., npe.IJ.OCTilBJUleMbIX HeKoTOpbIMH .Il.PyrHMH npoTOKOJiaMH). OporoKOJibI TCP H IP HaCTOJlbKO CHJibHO CBJl3aHbl, qro npHIDITO o6t.e)J.HHJITh HX B 0.Il.HY rpynny DO.II. HaJBaHHeM
ce.fl.teucmeo TCP/JP (e Hero BKJJioqaeTCJI TaJC:llCe nporoKoJI UDP, paccMOTpeHHe KOToporo BbTXO.IJ.HT Ja paMKH non KHHrn). Tipnee.z:teM OCHOBHbie oco6eHHOCTH npoToKona TCP, exo.Il.JIW.ero B ceMeHCTBO.
Ll
KoppeKTHaJI .IJ.OCTaBKa .IJ.aHHbTX .IJ.O Mecra HaJHaqeHHX rapaHTHpyeTCx -
PaJYMeeTCR, ecnu
TaKaJI .Il.OCTaBKa eoo6w.e B03MO:llCHa. ,[(a:lKe eCJIH CBR3b He BTIOJIHe Ha,zJ.e:llCHa (HanpHMep, Ha JIHHHH noMeXH OTTOro, 'ITO B Ka6eJib nonana BO.II.a, 3aMep3WaJI 3HMOR H pa30peaswax oOOJIO'IKy npoBO.IJ.a), "norepXHHble" $parMeHTbl .IJ.aHHbIX IlOCblJialOTCR CHOBa H CHOBa .IJ.O Tex nop, noKa ecR HHopM3.l.(HJI He 6y.IJ.eT nepeAaHa
l:J
Oepe)J.asaeMax HHopM3.l.(HR npe)J.CTaBJieHa B BH.Il.e IlOTOKa -
HaDO)J.OOHe Toro, KaJC ocy-
IQeCTBJUleTCR o6MeH c $ait.rraMH npaKTH'leCKH eo ecex onepaQHOHHbIX cncTeMax. HHbIMH CJIOBaMH, Mbl MO:llCeM
11
0TKpbITb 11 Coe.Il.HHeHHe H JaTeM BbinOJIIDITh c HHM Te :llCe CaMbie
onep3.l.(HH, K KaKHM npHBbOOIH npu pa6ore c aAnaMH. TaKHM o6pa30M, nporpaMMbI Ha pa3HblX MaIOHHax (B03MO:llCHO, HaxO.Il.jflllHXCJI 3a ThICJl'IH KHJIOMeTpOB .IJ.pyr OT .IJ.pyra), no.l{KJIIOqeHHblX K HHrepHery, o6MeHHBalOTCR .l{aHHblMH TaK :llCe HenpHtty)l(.Il.eHHO, KaK H pacnOJIO)l(eHHbJe Ha O)J.HOM KOMilblOTepe.
Ll
OpoTOKOJI TCP/IP ycTpeH Tax, 'ITO OH cnoco6eH BbI6paTh onTHMaJibHbIH nyTb pacnpoCTPaHeHHJI CHrHaJia Me)l(.Il.y nepe.IJ.alOUleR H npHHHMalOUlelf CTOpOHOH, )J.a:lKe eCJIH CHrHaJI npoXO)J.HT qepe3 COTHH npoMeJKYTO'IHblX KOMilblOTepoB. B IlOCJie,l{HeM c.rryqae CHCTeMa Bbl6HpaeT nyTb, no KOTOpoMy ,l{aHHble MOI)'T 6b1Tb nepe.IJ.aHbl 3a MHHHMaJibHoe epeMJI, OCHOBblBaJICb npH 3TOM Ha CTilTHCTH'leCK)'IO HHopM3.l.(HIO pa6oTbl CeTH H Tax HaJbIBaeMble Ta6JIHQbl MapwpyrHla.QHH.
l:J
0pH nepe)J.a'le ,l{a.HHble pa36HBalOTCR Ha $parMeHThl -
IlaJCeTbl, KOTOpbte H ,l{OCTaBJUllOT-
CR B MecTO HaJHa'leHHJI no OT)J.eJibHOCTH. PaJHble naxeTbI BnOJIHe MOI)'T CJie)J.OBaTb pa3JIH'IHblMH MapwpyraMH B 11HrepHere (oco6eHHO eCJIH HX Tiyrb nponeraeT '1epe3 ,l{eCRTKH cepeepoe), Ho .Il.JUI ecex HHX rapaHTHpoBaHa npaeHJJbHaJI "c6opKa" e MecTe HaJHa'leHHJI (e tty)l(HOM nopR.IJ.Ke). Kax y:llCe ynoMHHaJIOCb, npHHHMWOIQaJI cropoHa e cnyqae 06Hapy:iKeHHJ1 He.Il.OCTa'IH IlaKeTa Jan~HeaeT nepe.IJ.WOl.QYIO CHCTeMy, 'ITOObl Ta nepe)J.ana ero ew.e pa3. Bee no npoucxo.IJ.HT ue3aMeTHo .IJ.JIR nporpaMMHoro o6ecneqeHHR, 3KcnnyaTHpyiow.ero TCP/IP. B Web-nporpaMMnpoeauHH uaM epim JIH npMeTCR pa6oraTh c TCP/IP uanpRMytO (paJee 'ITO e o'leHb 3K30THqecKHX c.rryqaJ1X), OObl'IHO MO:lKHO ncnoJibJOBaTb 6onee BbtCOKoypoeue-
46
4acmb /. 0cH06bl Web-npoepaMMupoeaHuR
Bbte "H3btKH", HanpHMep HTTP, CJI)')KaIUHit MJ1 o6MeHa HH$opMaQHeii Me)K)zy cepsepoM H 6paYJepoM. Co6crneHHO, :noM)' npoToKony nocsHI.QeHa 39aqHTeJibHaH qacTb KHHrn. Ero Mbt paccMorpHM no.n.po6uo 'I)'Tb no3)1(e. A noxa noroeopHM el.Qe HeMuoro o TOM, "!TO xacaeTC.11 TCP/IP, "ITOObt He so3spaI.QaTbC.ll x JTOM}' snocne.llCTBHH.
AApeca1.4M.R e CeTM MautHH s HHTepueTe MHoro, a 6y.n.eT __:_ ell\e 6onbme. Tax "!TO sonpoc o TOM, xax MO)KffO HX 3$exTHBHO H.lleHTHHQHpoBaTb B npe.nenax sceA CeTH, OKa3bIBaeTC.ll .nanexo He npa.3.D,HbIM. KpoMe roro, npa.KTH"leCKH see COBpeMeHHbie onepaQHOHHbte CHCTeMbl pa6oTalOT B MHOf03a.na"IHOM pelKHMe (noMep)f(HBaJOT O.D,HOBpeMeHH)'JO pa60Ty HeCKOJlbKHX nporpaMM). '.fro 3Ha"IHT, "!TO B03HHKaeT TaK)f(e eonpoc 0 TOM, KaK HaM H.D,eHTHHQHpoeaTh KOHKpeTH)'IO CHCTeM)' HJIH nporpaMMY, )l(eJiaJOIIl)'IO o6MeHHBaTbCH JJ.aHHblMH qepe3 CeTb. 3TH JJ.Be 3a)J.aqu pelllaJOTCH cTexoM TCP/IP npu noMOll\H IP-aJJ.peca H HoMepa nopTa. .lJ:aeaihe nocMorpnM, KaK.
IP-aApec JII06oA KOMilblOTep, no)J.KJIIO"leHHbIH K HHTepuery H )l(eJiaJOIUHit o6MeHHBaTbCH HH$opMauueit co csoHMH "copo.llH"laMH", JJ.OJDKeH HMeTb uexOTOpoe ynHKaJibHoe HM.II, unu IP-aopec. BoT ~e 30 JieT cpe,AHecTaTHCTH"leCKHH IP-aJJ.pec Bbt:rJIJUtHT npHMepuo Tax:
127.12.232.56 Kax Mb! BH.llHM, 3TO - "leTbipe 8-pa3p.11JJ.HbIX 'IHcna (npHHa)J.Jle)l(aIUHX .D.HanaJouy OT 0 .n.o 255 BKJIIO"IHTeJibHO), pa.3,AeJieHHbte TO"IKaMH. He ece 'IHCJia JJ.OnyCTHMbI e 3fil1HCH IPaJJ.peca: P.11.ll H3 HHX HCilOJlb3YeTCH B CJI~e6HblX QeJUIX (HanpHMep, a)J.pec 127.0.0. l (ero ell\e qacro Ha3blBaJOT localhost) Bbl.lleJieH MJI o6paiueHHH K JIOKaJibHOit MalllHHe - TOH, Ha KOTOpoH 6bUI npOH3Be,AeH 3a11poc, a qucno 255 COOTBeTCTByeT lIIHpOKOBeI.QaTeJibHOH paCCbUIKe e npeJJ.eJiax TeKYI.Qeit no.D.CeTH). Mbr He 6y.D.eM 3,Aech o6cY*.J:laTb 3TH HCKJllO'leHHH .lleTaJibHO. Bo3HHKaeT eonpoc: Be.llh KOMilhlOTepoe H ycrpoAcTB s Hnrepnere MHJIJIHap.llbt (a nporH03HPYIOTC.ll ,AeCHTKH MHJIJIHap.D.oB), xax )l(e Mbl, npocTbte nonb3osareJIH, 3anpocHe IP-aJJ.pec MaUIHHhl, e C'IHTaHHbte cexyn.llbI c Heil Coe.llHIDieMcsr? Kax "ono" (u 'ITO 3TO 3a "ono"?) y3naer, r,Ae Ha CaMOM .lleJie pacnOJIO)l(eH KOMilblOTep H ycTaHaBJIHBaeT c HHM CBH3b, a B CJI)"lae HesepHoro aJJ.peca aJJ.eKBaTHO Ha no pearnpyeT? Bonpoc axryaneH, nocxoJibKY MalllHHa, c xoTopoA Mbl co6npaeMCH CB.113aTbCH, BilOJIHe MO)l(eT HaxO,AHTbCH 3a oxeaHOM, H nyTb K HeH nponeraer qepe3 MHO)l(eCTBo npoMe:>xyTO'IHbIX cepeepoe.
B .lleTaJI.11x sonpoc onpeJJ.eJieHHH nyTH x aJJ.pecary .!J.OBOJihHO CJIO)l(eH. 0.llnaxo uerpy.D.Ho npe,AcTaBHTh ce6e o6uzyro xaprnuy, roquee, Hexoropyio ee MO.D.eJib. Dpe.D.OOJIO)l(HM, qro y Hae eCTb l MHJIJIHap.n (109 ) KOMilblOTepoB, KIDK.D.hlil H3 KOTOpblX Hanp.llM)'IO Coe.llHHeH c 11 (K npHMepy) .llPYrHMH qepe3 xa6enH. Oo.ll)"laerc.11 3TaKaH nayrnHa H3 xa6eneA, He Tax JIH? KCTaTH, no ofu.HcIDieT, no"!eMy O.D.Ha H3 Hau6onee nonyJI.11pHbIX cn~6 HHTepHeTa, 6aJHp)'lOIQaHcsr Ha npoToxone HTTP, HaJBaHa WWW (World Wide Web, HJIH BceMHpHaH nayTHHa).
3AME'4AHHE CneAyeT 38MeTITTb, l.{1'0 B pe811bHblX yC11oe1.u1x, KOHENHO >Ke, KOMnblOTepbl He coeAMHSllOT APYr c ,QpyrOM TSKlllM 6011bWlllM KOillll'ECTBOM K8H8110B. BMecTO 3Toro np111MeHSllOTCSI BCeB03MOlKHbl8 BHyrpeHHMe T8611111l.lbl, KOTOpble no3B011SllOT KOMnblOTepy "3H3Tb", rAe KOHKpeTHO pacno11ara10TCSI
47
rnaea 1. npuHL(Unbl pa6ombl HHme/)HemB
HeKOTopble 6m1>1CaMWMe ero CC>CeAM. To ecn. nl06M M8WMH8 B CeTM 14MeeT 14H4>0PM81..1MIO 0 TOM, ~epe3 KaKMe Y3nb1 AOlllKeH npoM'TM CMrHan, 'fTo6bl AOCTMrHYTb C8MOro 611M3Koro K HeM 8APf!C8Ta. A ectlM He o6118A8eT 3TMMl4 3H8HMRMM, TO OOJ1Y'4aeT MX y 6nl4>KaMwero "copoAM~a· B MOMeHT 38l'PY3KM onep&l..IMOHHOM CMCT8Mbl. Pa3yMeeTCfl, pa3Mep TBKMX Ta611141.1 orpaH11t~eH ... OHM He MOfYT COA8PllC8Tb Mapwpyn.i AO acex M8WMH B li1HTepHeTe (XOTR B C8MOM H8'l8Jle P83BMTMR 111HTepHeTa, KOfAB KOMOblOTepoB I CeTM !Sblno HeMHOl"O, MMeHHO T8K M o6crOMO A8110). noTOMy-TO Mbl lit npoBOAMM 8H&n01'1'IO C OAMHH8Al.18Tbl0 CC>CeARMM.
1-hax, Mbl CHJVIM
3a KOMilblOTeJ>OM HOMep 1 H :llCeJiaeM coe,!1.HHHThCJI c MaWHHOA example.com c HeKOTOpblM IP-a.npecoM. Mw .n.aeM HameMY KOMOblOrepy 3allpoc: BhlJICHH-Ka y CBOHX coce.n.eA, He 3HaJOT JIH OHff 'lero 0 example.com. Ou pacchlJiaeT B 0,!1.HHHa.ztU.aTb CTOpoH 3TOT 38IIpoC (C'IKraeM, 'IT() 3TO 38.HHMaeT 0,1 c, T. K. see npoffCXO,ZJ,HT npaKTff'leCKH o.n.HOBpeMeHHO - pa3Mep lanpoca He HacTOJlhKO BeJIHK, tffOObl CKaJaJiacb Ja,nep)(Q(a nepe.n.a11H ,!1.aHHbIX), H :lK,!leT, qro eMY OTBeTIIT.
llro :i1Ce npoffCXO.D.HT .n.anhme? Herpy,ZJ,Ho .n.ora.nan.c.11. K!DIC,!U>ID HJ KOMnblOTepos OKp}')KeHHJI .n.eikTByer no TO'IHO TaKOMY :i1Ce OJiauy. Ou copamffBaeT y ceoux .nec.11repblx coce.n.eA, ue CJihllllaJiff Jiff oHH qero o example.com. 31'0, B csoio oqepe,nh, JaHHMaeT ellle 0, 1 c. llro )l(e Mhl HMeeM? Bcero Ja 0,2 c nposepeuo }')Ke 11xI0 = 110 KOMObK>Tepos. Ho JTO er.ue ue see, Be.D.b npou.ecc uapacTaeT nasuHoo6paJHO. Herpy.nuo oo.n.c'lfffTaTb, 'ITO Ja speM.11 oop.11.n.Ka I c Mbl "pa36y,ZJ,HM 11 11x109 Mauum, T. e. B 11 pal 6o.rn.me, qeM Mbl ffMeeM! K0He11uo, ua caMOM .n.ene opou.ecc 6y,ner H,nTH Me.nneHHee: c o,nuoii CTopoHbI, KaKHe-To cucTeMbl MOI)'T 6blTb JaHJITbl ff ue OTBeTIIT cpaJy. .l{pyroA CTOJ>OHbI, Mbl .l{OJDKHbl HMeTb MexaHHlM, KOTOpbdt 6bl o6ecoeqffBaJI, tffOObl O)Ula MaIDffHa He "oopawHBaJI8Cb 11 MHOroKpaTHO. Ho see pasno, cornacH'reCh, pely.rn.TilTbl soeqaTJIJllOT, .n.IDKe ecnu HX u npff.l{eTCJI 38.HHJHTb .l{JI.11 peanbHblX ycJIOBffA XOTb B 100 pal.
c
3AME'IAHHE
B AeMCTBMTeJ'lbHOCTM Aeno o6cTOMT KYA8 cno>Kttee. OmM~MR OT npeACT&&neHHOM cxeMbl ~ac TM~Ho 3aKnl0"4alOTCfl B TOM, 'fTO KOMOblOTepy coaceM He o6R38T811bHO "3anpaw11taaTb" acex CBOMX coceAeM - AOCT8T~HO orpaH~MTbCfl TOllbKO H8KOTopblMl4 M3 HMX. AnR yCKopeHMR AOCTyna ace B03MOlKHble IP-a,qpeca AemrrCfl Ha '4eTblpe rpynnbl - T8K H83bl888Mble a,QpeC8 OOACeTeM Kl18CCOB A, B, C 11t D. Ho AnR Hae ce~ac 3TO He np&Acra&nReT HMK&Koro 14HTepeca, no3TOMy He 6yA8M 3aA8PlKl4BaTbCR H8 AeTanRx. 0 TCP/IP MOlKHO H&nMCBTb ~nble TOMa ('fTO "'AenaeTCR).
BepcMM npoToKona IP Ecnff so BpeM.11 pacUBeTa llilTepuera B 1990-x ro.nax JI1060A no.rn.Josare.rn., nonyqan csoA nepcouanbHbdt IP-a.npec, ff .n.IDKe Mor JaKPeOHTb ero Ja co6oli, ro co BpeMeHeM a,zq>ecos CTano He XBaTilTb. B uacro.11UlffA MOMeHT ua6mo.n.aeTC.11 .n.e$fflJ.HT a.npecHoro opocrpaucTsa IP-a.npecos. 6Jiaro.l{ap.11 }')KecTO'lfeHHJO DpaBHJI RX Bbl,ll8'lfff, BBe.L{eHHIO HOBblX cnoco6oB opraHff381J.ffff a.npecuoro DpOCTPaHCTBa (NAT, CIDR) Cff'I'YlllJ.HIO y,naeTCJI noKa .nep:lKan. 00,lJ, KOHTpoJieM. no OOTHMffCTH'lfHhlM nporuoJaM, pacnrresaTb a.npecuoe OJ>OCTPaHCTBO MO)l(lfO .l{O 2050 ro.l{a.
B JII06oM cnyqae IP-a.npec -
.l{ecpHUHTHbdt pecypc, nonyquTb KOTOphdt He npoCTo .. IloJTOMy B 6mracam11He fO,Ilbl Hae O)l(ff){aeT CMeHa sepcHH opoTOKOJia c TeKYllleA 1Pv4 Ha HOBYJO 1Pv6, B KOTOpoA a,zq>eC pacIDHp.lleTCJI c 32 ,!{0 128 6HT (IDeCTHa,nu.aTb 8-pa3p.ll,llHbIX 'lfHCeJI). nepeXO.l{OM ua 1Pv6 6y.n.er .n.ocTilTO'IHO BbI.D.eJIJleMbIX nepcoHa.rn.HbIX IP-a.npecos K8)1(.L{OMY X3)'lOlllHM CeTb .ll.Jl" o6MeHa ,ll.aHHbIMH . .[{o CHX nop Mbl pacQeHHBaJIH MallIHHbl, nO,ll.KJllO'leHHble K HHTepHery' Kate HeKHe He,n.eJIHMble CYIQHOCTH. Tate OHO, B o6meM-TO, H eCTb {npaa.n.a.. c HeKOTOpblMH oroeopKaMH) c TOqKH 3peHH'1 nporoKona IP. Ho TCP ucno111>JyeT e ceoeA pa6ore HeCKOJibKO .n.pyrue no""THJI. A 11MeHHo, .ll.J1" Hero OT.1J.eJI1>HOH CYIQHOCTblO jlBJlj[eTC" npol./ecc - nporpaMMa, JaJIYllleHHaJI r.ue-To Ha KOMilblOTepe e HHTepHere. nPHME'IAHHE 88)1(H0 nOHMMaTb pa3HMLIY M8*AY nporpaMMOM M npOL19CCOM. nporpaMMa - npOCTO "1CnOnHAeMblM Ke - 3TO nporpaMMB, KOTOpytO 3arpy3MnM B naMRTb M nepeASnM ei'I ynpaeneHMe. Bbl MO>KeTe npeACTBBMTb, l.ITO nporpaMMB - TeKCT KHMrM, KOTOpblM cy~CTeyeT
rnaaa 1. npuH«.(Unbl pa6ombl UHmepHem8
51
B BAMHCTBeHHOM 31t3BMMApe. npo1.1ecc lKB - 3TO 'IKTSTBnb KHMrlll, M He npocro BAnO BOAfl~r.1lil B3rnA,a.OM no crpo'IK8M, a BH"1K8IO~MM B H8nr.1C8HHoe M Aenal0~"1H co6craeHHble BblBOAbl. CKOnbKO nlOABH - CTOnbKO MHeHMH, n03TOMy K8>KAblH r.13 '1"1T8Tenelil YH"1K&neH "1 HenOBTOpr.1M, ASlKe ecn1i1 OHM ece 'IKT8IOT OAHY "1 TY >Ke KHMry. TO'IHO T8K l!Ce "1 npOl..leccbl OAHOM .. TOM l!Ce nporpaMMbl pa3nr.1'18IOTCR Mellf,Ay co6olil.
HMeHHo Me)l()zy npouecCaMH, a He Me)l()zy Mannrna.MH H ocyruecTB.IUleTCJI o6MeH )J.aHHLIMH s TepMHHax npoToKona TCP. MbI }')ICe JHaeM, KaK MeHTHcl>HUHPYJOTCH OTJleJibHbie K0Mnb10Tepb1 B CeTH. OcTaJIOCb paccMOTpeTh, KaK )l(e TCP onpe,ll.e.IUleT TOT npou:ecc, KOTOPOMY H}')ICHO ,ll.OCTilBHTh JlaHHbie. nycTb Ha HeKOTOpoA CHCTeMe BbffiOJIHHeTC.11 nporpaMMa (HaJOBeM ee KJIHeHT), KOTOpa.11 XOlfeT qepe3 HHTepHeT coe)J.HHHTbC.11 c KaKOA-TO JlpyroA nporpaMMOJl (CepsepoM) Ha Jlpyroit MauJHHe B CeTH. ,A.IUI noro JlOJDKeH BbmOJIHHThC.11 p.11.ll. ycnosnii, a HMeHHO:
LJ nporpa.MMbl JlO.IDICHbl 11JlOI'OBOpHTbCJl 11 0 TOM, KaK OHH 6y)J.YT Jlpyr Jlpyra HJleHTHcl>Hu:Hpo-
Ban.; LJ nporpaMMa Cepsep JlOIDKHa HaxOJlHTbC.11 B pe:>K:UMe O:>tCUOaHwi, 11To ceA11ac K HeH KTO-TO IlOJlKJllO'IHTCJI. YCTaHoeKa coe,QMH&HMfl
OcTaHOBHMC.11 Ha nepBOM nyHKTe qyn. no)J.po6Hee. TepMHH 11)1.orosopnTbC.11" -ryr He cosceM YMecreH (npHMepHo TIU< )l(e noJIHUH.11 11)1.orosapHBaeTC.11" c TOJibKO 'ITO Ja,ll.ep)l(aHHbIM 6aH,ZJ.HTOM o noMeI..UeHHH ero B TIOPbMY). Ha caMoM JleJie, KaK TOJibKo JanycKaeTC.11 nporpaMMa Cepsep, oHa rosopuT JlpaHsepy TCP, 11TO co6upaeTC.11 ucnoJibJOBaTh Jl.IUI o6MeHa JlaHHbIMH c KimeHTaMH HeKoropbdi H,AeHTnct>mcaTop, HJIH nopm, - uenoe 11ncno B JlHanaJoHe OT 0 ~o 65 535 (HMeHHO TaKHe qffcJia MOryT xpaHHTbCJI B gqeRKe IlaMJITH paJMepoM 2 6aATa). TCP pernc-rpnpyei- no B csonx BHyfpeHHHx Ta6nuu:ax - PaJYMeeTC.11, TOJibKO B TOM cnyqae, ecnn KaKIUl-HH6YJlb Jlpyra.11 nporpaMMa ~e He "JaHHJia" HY)l(HblH HaM nopT (s nocne,ll.HeM cnyqae npoucxo,ll.HT own6Ka). 3areM Cepsep nepexo,ll.HT s pe)l(HM O)l(HJlaHH.11 nocrynneHH.11 JanpocOB, npHXO,ll..llll\HX Ha 3TOT DOpT. 3ro OJHaqaeT, 'ITO mo6on KnHeHT, KOTOpblli co6upaeTC.ll scrynnn. s ".1U1anor" c CepsepoM, .lJ.OJl)l(eH JHaTb HOMep ero nopTa. B npoTHBHOM cny11ae TCP-coemtHeHue HeBOJMO:>KHo: KyJla nepe,ll.a&aTb .ll.aHHbie, ecnu He JHaeWb, K xoMy no,ll.KJIIO'iHThCH? Tenepb nocMOTpHM, KaICHe )leikIBH.11 npe./lilpHHHMaeT KnueHT. OH, KaK MbI ycnOBHJIHCb, JHaeT:
LJ IP-a,ll.pec MamHHbI, Ha KOTopoR JanyI..UeH Cepsep; LJ HOMep nopn, KOTOpblH HCOOJlbJYeT Cepsep.
Kaic BH.ll.HM, 3TOH HH4'oPMaIJ.HH snonHe JlOCTaTO'IHO, n03TOMY KnneHT nocblJlaeT JlpaABepy TCP KOMaHJlY Ha coe,wrneHHe c MamHHoA, pacnono:>KeHHoA no 38,llaHHOMY IP-8.ll.pecy, c yKaJaJIHeM H)')KHOfO HOMepa IlOJ)Til. nocKOJlbK)' CepBep "Ha TOM KOHQe" fOTOB K JTOMY, OH OTICJJHK8eTCJI, H coe)lHHeHHe ycTaHaBJIHBaeTCJI. TonbKo 'iTO 6bmo yno-rpe6neeo cnoso "oTICJimc:aeTC.11 11 , oJHaqa10ruee, 'ITO Cepsep ompaBJIHeT KaKoe-To coo6I..UeHHe Knneirry o csoeA roTOBHOCTH K o6MeHy JlaHHbIMH. Ho scnoMHHM, 'ITO MJI TCP CYI..UCCTB)'IOT TOJibKO JlB8 noHJITH.ll Jl.IUI MeHTH«l>HKaIJ.HH npou:ecca: a,ll.pec H nopT. Tax K)'Jla )l(e HanpaB.IIJITh "OTKJIHK" Cepsepa? QqesH)J.Ho, nocne)lHHR JlOJl)l(eH KciKHM-TO o6paJoM yJHaTh, KaKOA nopT 6y)leT HCilOJlbJOBaTb KnHeHT Jl.IUI npHeMa coo6meHHA OT Hero
52
l/acm& I. OcHoebl Web-npoapaMMupoeaHuR
(Be.llb Mbl 3HaeM, 'ITO npHHHMaTb )laHHbie MO)KHO TORbKO 3ape3epeHpoeae JJ.nH 3Toro y TCP ttoMep noprn). 3ry HHQ>opMaUHIO eMY KaK pa.3 " npeJJ.OCTaBRHeT JJ.paneep TCP Ha MarnHHe KJrneHTa, KOTOpblH HenocpeJJ.CTBeHHO nepe.ll ycTaHOBKOR coe)J.HHeHHH BbI6HpaeT He3aHHTbIH nopT H3 cnHCKa CBOOOJJ.HbIX Ha )laHHblH MOMeHT nopTOB Ha KnHeHTCKOM KOMnblOTepe H "np11csa11saeT" ero npoueccy KnHeHT. 3aTeM JJ.paiisep HHopMHpyeT Cepsep o HoMepe nopTa (s nepsoM )Ke coo61Uenuu). Co6cTBeHHO, 3TO H COCTaBRHeT CMbICR TaKoro coo61UeHHH nepeJJ.aTb Cepsepy HOMep nopTa., KOTOpbIR 6yJJ.eT HcnoRb30BaTb KnHeHT. CTopT 3anucb1eaeTcH nocRe IP-a.n.peca HRH .llOMeHHoro HMeHH qepe3 JJ.Boerncrne. HanpHMep, BMecTO a.n.peca yandex.ru MO)KHO 3anHcaTb http://yandex.ru:80 HRH https://yandex.ru:443. nopT 80 HBJIHeTCH CTaH)lapYHbIM )J.nH o6MeHa )laHHbIMH c Web-cepsepoM, nopT 443 - CTaHii:apTHbIM JJ.nH 3arnuq,poeaHHoro SSL-coeJJ.HHeHHH. B nosceJJ.Heenon )ICH3HH npH HcnoJih30BaHHH a.n.pecoe MbI He YKa3blBaeM cTaHJJ.apTHbie nopTbI, 6pay3epb1 u .llpyme ceTeeble KnHeHTbI Ha3HalJalOT HX aBTOMaTHlJeCKH. 0)1.HaKO, eCJIH cepeep 11cnORb3yeT KaKOH-TO HeCTaHJJ.apTHbdl nopT, HanpHMep
8080, ero
npHJJ.eTcH yKa3bIBaTb e a.n.pece HBHo:
http://localhost:8080.
06M8H ACIHHblMM KaK TOJihKO o6MeH "npHBeTCTBeHHbIMH" coo6meHHHMH 3aKOH'leH (ero eme Ha3hIBaJOT 11 TJ)OHHblM pyKono)KaTHeM", noTOMy lJTO B o6meH CJIO)l(HOCTH nOCbUialOTCH 3 TaKHX coo61UeHHH), Me)l(Jl)' KRHeHTOM H CepeepoM ycTaHaBJIHBaeTCH llO?U'leCKUU KQHaJl C8.R.3U. nporpaMMbl Moryr HcnORb30BaTb ero, KaK o6bI4HhlH Kanan UNIX (:no nanoMHHaeT CJI)"laH Kene30"), a He ROm'lecKaH 1.JacTh CeTH, OH MO:>KeT HMeTb HeCKORhKO pa3nH4HhIX IP-a,n.pecoe (ne roeopH ~e o .noMeHHhIX HMeHax), TaK 'ITO enoJIHe MO)ICeT BbirRll)l.eTb H3 11HTepHeTa KaJC HeCKOJibKO He3aBHCHMblX CHCTeM.
ToJibKO lJTO 6binO 3ruIBJieHo, 'ITO cepeep - 3TO 11 :>KeJie30". no)ICaJiyil, 3TO CRHIIIKOM MexaHHCTH'leCKHH no.nxo.n. Mbl MO)ICeM npHJJ.ep)l(HBaTbCH H .llpyron TO'IKH 3peHHll, TO:>Ke B HeKOTOpoH cTeneHH npaeHJihHOH. OrnHlJHTeJibHOH 'lepToii cepeepa JIBJIJleTCH TO, 'ITO OH HCnOJih3YeT
53
rnaaa 1. npuH1.4Unbl pa6ombl llfHtnepHem8
0.11.HH-e.11.HHCTBeHHblli crex TCP/IP, T. e. Ha HeM JaII)'l.QeHo TOJlbKO no 0.11.HoMy 11 3KJeMnm1py" .11.paiteepoe Ilp0TOKOJIOB. noiKanyii, 3TO 6y.ll.eT .11.lliKe IlpaBHJlbHee, XOTll B HaCTOlll.[\ee BpeMJI o6a onpe.11.eneHIDI IlO'lTH 3KBHBaJieHTHbl (npocro COBpeMeHHbIB KOMilblOTep He HaCTOJlbKO MOl.l.UfbIH, 'ITOObl Ha HeM MOrnH cf>yHKU,HOHHpOBaTb 0.11.HOBpeMeHHO .11.ee onepaiumHHble CHCTeMbl H, cne.11.oeareJibHo, HecKOJibKO crexoe TCP/IP). IlocMOTpHM, xax 6y.D.eT c 3THM o6cTollTb .11.eno e 6y.ll.)'l.l\eM. Y repMHHa "cepeep" ecTb eUle 0.11.HO, coeepweHHO .11.pyroe, onpe.11.eneHHe - 3TO nporpaMMa (e repMHHOJIOrHH TCP- npouecc), o6pa6aTbIBalOl.l.\all Janpochl KJIHeHToe. HanpHMep, npHJimKeHHe, o6CJI}')KHB8K)Ulee no11b3oeare11eR WWW, Ha.JbIBaeTCll WebcepeepoM. ,[{aHHoe onpe.11.eneHHe H.D.eHTH'IHO noHJITHIO "cereeoi1 .n:eMoH" HJIH "cepeHc". Kax npaeHJIO, HJ KOHTeKCTa 6y.11.eT llCHO, 'ITO KOHKpeTHO HMeeTCll B BH.ZJ:Y.
Y3en Jlio6oif xoMnblOTep, no.11.KJI10'leHHb1R x 11HrepHery, HMeeT ceon yHHKaJibHbrn IP-a.11.pec. HeT a.11.peca- HeT yJna. Y3e.11 - coeceM He o6llJaTeJibHO cepeep (THnH'IHbIH npHMep - KJIHeHT, no.11.KJiio'leHHbIR qepeJ MO.lJ.eM K npoBfili.n.epy). Boo6Ule, Mbl MOlKeM .11.aTb Taxoe onpe.11.eneHHe: 11io6aH CYl.l.\HOCTb, HMelODlall yHHKaJibHbIR IP-a.11.pec e HHTepHeTe, Ha.JbIBaeTcll y3!loM. C 3TOH (JIOnflleCKOi1) TO'lKH JpeHHJI 11HTepHeT MOlKHO paccMa'I'pHBaTb KaK MHOlKeCTBO YJJIOB, KlliK.11.bIH HJ KOTOpblX IlOTeHUHaJibHO MO:llCeT CBJIJaTbC.ll c JII06b1M .lJ.PyntM. 3aMeTbTe, 'ITO Ha O.D.HOR CHCTeMe MO:llCeT 6b!Tb pacnoJIO:itCeHO CPa.JY HeCKOJibKO YJJIOB, ecJIH OHa HMeeT HeCKOJibKO IP-a.11.pecoe. HanpHMep, 0.11.HH yJeJI MO:itCeT JaHHMaTbCll TOJibKO .11.0CTaBKoii H pacCblJIKOH no'ITbI, eTopoit- HCKJDO'lHTeJibHO o6cJJY:llCHBaHHeM WWW, a Ha 'I'peTueM pa6oTaeT DNS-cepeep. noMHHTe, Mbl roeopHJIH o TOM, 'ITO TCP HcnonbJyeT repMHH "npouecc", H KIDK.11.bIR npouecc .11.Jlll Hero 0.11.HOJHa'lHO H.11.eHTHcf>HUHPYeTCll IP-a.11.pecoM H HoMepoM nopTa. Tax BOT, 3TOT caMbiil IP-a.11.pec H llBJilleTCll OCHOBHbIM a'I'pH6YTOM, onpe.11.eJIJllODlHM yJeJI.
nopT llopm -
3TO Hexoropoe 'lHCJIO, KOTOpoe H.11.eHTH$HUHPYeT nporpaMMY. :itCeJiaiouzyio npHHH-
MaTb .11.aHHble HJ 11HTepHeTa. TaKHM o6pa.JOM, IlOpT -
BTOpall COCTaBJI.lllODlall a.11.pecauHH
TCP. Jlio6aH nporpaMMa, CTpeM.lllllallCll nepe.11.aTb .11.aHHbie .11.pyroR, .11.0JI:llCHa JHaTb HoMep nopTa, xoropb!R 3aKpeOJieH Ja nocne.11.HeR. 06bl'lHO Ka:llC.ll.OMy cepeHcy Ha.JHa'laeTCJI cf>HKCHpOBaHHblii HOMep IlOpTa. HanpH~ep, 'I'pa.11.HUHOHHO Web-cepeepy Bbl,ll.eJIJleTCll IlOpT c HOMepoM 80, Il03TOMy, KOr,lla Bbl Ha6HpaeTe KaKOii-HH6y.llb a.11.pec B 6payJepe, Janpoc H,lleT HMeHHO Ha IlOpT
80 YKaJaHHOro yJJia.
CeTeeoi A8MOH, cepeMc, cny>K6a Ceme6ou oeMOH (HJIH ceP6uc, HJIH c.11y:J1C6a)- 3TO nporpaMMa, pa60Ta10lllall Ha cepeepe H JaHHMalODlallCll o6cJJY:llCHBaHHeM pa.JJIH'IHbIX noJibJOBaTeneii, x0Topb1e MOryT K Heil no.n:KJIIO'laTbCll. 11H&IMH cnoeaMH, cereeon .neMoH Mep -
3To nporpaMMa-cepeep. THnH'IH&Iii npH-
Web-cepeep, a TIUOKe FTP- H telnet-cepeepbI. nPHME'IAHHE CaM TepMMH "ceTeeolil AeMOH" B03HMK Ha 15a3e ycroAewelilcA TepM..iHononi..i UNIX. B 3TOlii c..icTeMe AeMOHOM H83blB8IOT nporpaMMy, KOTopaA nOCTOflHHO pa60TaeT Ha M8WMHe B cpoHOBOM peJ1G1Me, o6t>NHO c c..icreMHblMM np..ie..inentAMM cynepnonb30eaTenA (T. e. aTa nporpaMMa MO-
54
l/acmb /. 0cH06bl Web-npoapaMMupoeaHuR >KeT AenaTb HS MSWMHe ece, 'lTO eill yrOAHO, M He nOA'lMHR8TCR npaeaM Aocryna O~HblX nonb30BaTene'4). ,IJ.eMOH He MMeeT HMKSKOM C8R3M c TepMMHanOM (3KPSHOM M KnSBMaTypoM), no3TOMY He MO>KeT HM npMHMMSTb ASHHble c KllSB!.iSTypbl, HM BblBOAMTb MX Ha 3KPSH. BOT M3-3S 3TOM "6ecTenecHOCTM" ero M HS3b1BSIOT AeMOHOM.
CKopee Bcero, BaM HHKor.n.a He npH)leTCJI co3.u,aBaTb co6crBeHHbIX .n.eMoHoB.
B 6onhlDHHCTBe
CJJY11aeB Bee 6y.u.eT CBO,lJ,HTbCJI K ycTaHOBKe, HacTpoAKe H HCilOJib30BaHHIO rOTOBOll cJJ}')K6bI. Pa3pa6oTKa co6cTBeHHolt cereBoH CJI)'JK6h1, oco6eHHO cooTBeTCTB)'lOIUelt CTaH;a;apTaM 38.l\a'la He npocTWI H 'lal.Qe Be,ZJ,eTCJI c HCilOJib30BaHHeM 6onee npOH3BO,lJ,HTeJibHbIX Jl3bIKOB, TaKHX KaK C, Go HJIH Java.
3AME'IAHHE 4aCTo npMXOAMTCR CMblWSTb cppa3y "cepBMC pa6oTaeT Ha y3ne TSKOM-TO, nopT TSKOM-TO". Ho BeAb cepBMC - 3TO npo1..1ecc, M OH 3any~eH Ha cepeepe, a He HS yane. no3TOMY cneAyeT nOHMMSTb MCTMHHblM CMblcn 3TOM cppa3bl: cepBMC pa6oTaeT Ha MSWMHe, OAH8KO o6paTHTbCR K HeMY MO>KHO no aAJ)ecy y3118 c YK83SHMeM HOMepa nopTa. Bo3MO>KHO, 'l'TO K 3TOMY >Ke cepeMcy MO>KHO o6pan1TbCR M no coeepweHHO ApyroMy SApecy.
XocT Xocm -
c TO'IKH 3pemu1 nonb30BaTeJUI KaK 6y.u,To TO :itee, 'ITO H y3en.
B
o6meM-TO, o6a no-
HJITHJI O'leHb '!aero CMeWHBaIOT. 3To o6ycnoBJieHO TeM, '!TO mo6olt YJeJI JIBJUieTCJI XOCTOM. Ho XOCT -
COBCeM He o6naTeJibHO OT,lJ,eJibHblA yJeJI, eCJIH :no BHpTyanbHbli:t XOCT. qacro
XOCT HMeeT co6cTBeHHoe YHHKMbHoe ,lJ,OMeHHoe HMJI. Haor.n.a (o6bf'IHO npocro 'IT06bl He IlOBTOPJITbC.11) Mb! 6y.n.eM Ha3bIBaTb XOCTbl cepeepaMH, '!TO, Boo6me roeopR, COBepweHHO He BepHO. aKTH'leCKH Bee, 'ITO OTnH'!aeT XOCT OT Y3Jia aJibHbIM. HTaK, eme pa3: mo6oA y3en -
:no TO, 'ITO OH MO:iteeT 6bITh BHpry-
XOCT, HO He mo6oR XOCT -
pen,
H HMeHHO TilK Mhl
6y.u,eM IlOHHMaTh XOCT B :noR KHHre.
BMpyYanbHblM XOCT 3To xocT, He HMelOIUHR YffHKaJII.Horo IP-a.n.peca B CeTit, HO, TeM He MeHee, .ll.OC1)'11HblA yKa3aHHeM KaKoro-Hu6y;a;h .n.onoJIHHTeJibHoro a.n.peca (HanpHMep, ero DNS-uMeHH).
B
nocne.u.Hee epeMJI KOJIH'leCTBO BHpTyaJibHblX XOCTOB B HHTepHere DOCTOJIHHO B03pacTaeT,
'!TO ceJl3aHo c noBceMeCTHhIM pacnpocTpaHeHHeM npoToKona
HITP 1.1.
C TO'IKH 3peHHR
Web-6pay3epa (BepHee, c TO'IKH 3peHID1 DOJib30BaTeJUI, KOTOpbIR 3THM 6pay3epoM DOJib3yeTCJI) eHpryan1>Hb1A xocT BhirJUI.U.HT TaK :itee, KaK H o6bNHhIR xocT coBaTb no IP-a.n.pecy.
K
npae;a;a, ero HeJib3JI a.n.pe-
co:iteaneHmo, ece eme cymecTBYJOT Bepc1rn 6payJepoe, He no.u..n.ep-
:lKHBalOIUHe npoTOKOJI HTTP
1.1,
KOTOphle, cooTBeTCTBeHHO, He Moryr 6b1Ti. HCDOJib30BaHhI
.U.JUI o6pameHIDI K TaKHM pecypcaM.
3AME'IAHHE noHRTMe "eMpT)'anbHblM xocr" He orpaHM'lMeaeTCR ronbKO cny>K6oi:1 Web. MHorMe APY™e cepeMCbl MMelOT CBOM nOHRTMR 0 BMpT)'SllbHblX XOCTSX, coeepweHHO He CBR3SHHbl8 c Web M npoTOKOnoM HTIP 1.1. Cepeep sendmail cnYJK6b1 SMTP (Send Mail Transfer Protocol, npoToKon nepe,o,a'lM nO'lTbl) TSIOICe MCnonb3yeT nOHRTMe "eMpryanbHblM xocr", HO AllR Hero 3TO llMWb CMHOHMM maeHoro, oCHoaHoro xoCTa, Ha KOTopoM 3any~eH cepeep. HanpMMeP, ecnM xocr syn.com RBnReTCR CMHOHMMOM AllR mlcrosoft.com, To aAJ)ec e-mail [email protected] Ha caMOM Aene 03Ha'laeT [email protected]. npMMe'laTenbHO, OAHaKO, 'lTO BMpryanbHblM XOCT M B 3TOM nOHMMaHMM He MMeeT yHMKanbHoro IP-SApeca.
fnaea 1.
npuH~Unbl
pa6ombl "1HmepHem8
55
npoaaiAep llpoBaUdep -
opraHHJallHJI, HMelOJ..UWI HeCKOJibKO MO,lleMHblX BXO,llOB, K KOTOpbIM Moryr no;:ucmoqaThCJI nonb3oBaTeJIH .!lJIJI ,llocryna B 11HTepHeT. Bee :no OObl'IHO npoucxo,llHT He 6ecnnaTHO (.!lJIJI nonb3oearenef:t, paJ)'MeeTc.11).
XocTMHr-npoaaiAep (xocTep) '.ho opraHHJallHJI, KOTopru1 MO)l(eT co3,llaeaTb xocTb1 (BHpryanbH1>1e, o6na11Hb1e HJIH o6b1'1Hb1e) B 11HTepHeTe H npo,llaeaTb ux pa3.JIH'IHbIM KJIHeHTaM, o61>iqHo 3a onpe,lleJieHH)'IO DJiary. CyLUecTByeT MHOJKeCTBO xocTHHr-npoBaf:t,n:epoB, pa3JIH'la10UU1Xc.11 no ueHe, ypoBHIO o6cnyJKHBaHH.ll, noMep)ICJ(e ssh-,llocryna (T. e. ,llocryna B peJKHMe TepMHHana K onepaQHOHHof:t CHCTeMe MaIIlHHbl) H T. ,ll. 0HH Moryr OKaJbIBaTb ycnym no pemcTpaQHH ,llOMeHHoro HMeHH B 11Hrepnere, a Moryr H ne OKaJblBaTb. npu HaDHCaHHH 3TOf:t KHHrH Mbl paCC'IHTblBaJIH, 1ITO 'IHTilTeJib COOHpaeTCJI BOCDOJlb30BaTbCJI YCJiyraMH O,llHOro H3 XOCTHHr-npoBaf:t;J:epoB, KOTOpbllt npe,llOCTaBJIJleT B03MOJKHOCTb HCnOJib30BaHHJI PHP. Ecnu Bbl em.e He Bbl6panu xocTHnr-npoBaltnepa H TOJibKO na11unaere OCBaHBaTb Web-nporpaMMHpoBanHe, He 6e,lla: B "acmu KHHrH no,llpo6HO pacCKaJaHO, KaK MOJKHO ycTanOBHTb H HaCTpoHTb co6cTBeHHblit Web-cepeep Ha mo6oM KOMnbJOTepe c OCHOBHbIMH onepaQHOHHbIMH CHCTeMaMH. '.ho MOJKHO C,lleJiaTb ,llaJKe Ha TOA CaMOit MawHHe, Ha KOTOpoit 6y.l{eT pa60TaTb 6pay:Jep - se,llb .l{paf:tsepy npoToKona TCP coBepweHHO 6e3paJ.JllfqHO, r,lle BblDOJIHJieTC.11 npouecc, K KOTOpoM)' 6y.!leT OCYIIleCTBJieHO no,llKJllO'leHHe, XOTb ,llaJKe H na TOM JKe CaMOM KOMnblOTepe. 11cnOJib3YJI 3TOT cepeep, Bbl CMOJKere HeMHoro noTpeHHpoBaTbC.11. KpoMe roro, oH HeJaMeHHM npu orna,llKe Tex nporpaMM, KOTOpbie Bbl s 6y.zcymeM nnaHHpyere pa3MeCTHTb Ha naCTOJlllJ.eM XOCTe B 11HTepHeTe.
x
XOCTMHr '.ho ycnym, KOTOpble npe,llOCTaBJIJllOT KJIHeHTaM XOCTHHr-npoBait,llepbl.
BMpyYanbHblM cepaep B nocne.!lHHe ro,llbI wupoKoe pacnpocTpaHCHHe nonyquna reXHonorn.11 snpryanHJal..lHH pecypcos, nOJBOJIJllOlllWI o6De,llHHJITb pecypcbl HeCKOJlbKHX cepsepoe B O,llHH cepBHC, KOTOpbdt JaTeM MOJKeT 61>m pa36HT Ha HeCKOJibKo BupmyaribHblX cefJ6epoe. B peJyJibTare KOMnbIOrepn1>1e pecypCbl paCXO.ll)'IOTC.11 6onee 3KOHOMHO. no,ll He60JibWOf:t cepBHC MOJKHO Bbl.!leJIHTb ManeHbKYJO supryanbH)'IO MawHHY BMecTo nonnoueHHoro cepeepa. CepsepbI MOlKHO eso.!lHTb .!lHHaMuqecKH npu yseJIH'leHHH narpy3KH u yHH'ITO:>KaTb 11acTb supryanbHbIX cepsepoe c B03epaToM pecypcos no Mepe Toro, KaK Harpy3Ka Ha cepeuc na,naeT. Ha O.!lHOM u3uqeCKOM cepeepe MO:>KeT 6b1Tb ycTaHOBJieHO HeCKOJibKO BHpryanbHblX cepsepos c pa3HbIMH onePal..lHOHHbIMH CHCTeMaMH. BbIXO.!VILUHe H3 CTpOJI mu11ecKHe cepeep1>1 MO:>KHO BblBO.l{HTb, He repu .!laHHbie supryan1>HblX cepeepoB. 06'be,llHHeHHbie s paMKax supryanHJal..lHH cepsepb1 HaJhIBalOTCJI K.1acmepo.M HJIH o6!laKo.u. XocTunr-nposaf:t,n:ep, npe,llocTaeJIJIIOLUHit ycnym no npe.l{ocTaeneHHIO supryanbHblX cepBepoB, HaJbmaeTCJI o6JiaqffbIM npooait,llepoM . .[{onroe BpeM.11, CaMbIM ,l{elIIeBbIM npeMOXruoeoA CHCTeMe cepeepa HanpnMep, caAT HOBOCTeit MO:>KeT HCilOJlbJOBaTh BHpTyaJlbHble nyrn THila /YIMIN.html AIUI OTo6pWKeHHJI ecex HOBOCTeA 38 'IHCJIO N MeCJIQa M ro.na Y, TaK 11To noJibJOeaTeJib, Ha6pae e 6paYJepe a.npec Hano.no6ne http:llno11ocm11ou_cept1ep/20lSI 10/20.html, CMO:llCeT npoqHTaTh HOBOCTH Ja 20 mm16pJ1 2015 r. TipH JTOM aAna c HMeHeM 20.html «t>nJnqecKH HeT, CYIQeCTBYeT TOJlbKO BHPTyaJibHblA nyTh K HeMY. a BCIO pa6ory no reHepaium CTJlaHHQbl 6epe-r Ha ce6J1 nporpaMMHoe o6ecneIJeHne cepeepa (e nocne.nHeA qacTH JTOA KHHm MLI paccxWKeM, K8.IC Taxoe nporpaMMHoe o6ecneIJeHne MO)l(HO HanncaTI>).
Ecn. H .npyroA MeXaHHJM o6pa6cmcn BHPTyaJibHbIX nyTett, Kor.na JanpoweHHbie aAJJbl npe.ncnmruoOT co6oA cTaTH'lecKHe o61.eK1ld, HO pacnommuoTCJI r.ne-To e .npyroM Mecre.
c TO'IKH 3peHHJI nporpaMMHOro o6ecne11eHHJ1 nyTb K .ztOIC)'MeH-ry OTC'IHThIBaeTCJI OT HeKOTOporo KOpHeeoro KaTaJiora, KOTOpbIA yxaJblBaeT a.nMHHHC-rparop cepeepa. TipaKTH'leCKH see cepeepHble nporpaMMbI noJBOJIJDOT coJ.ztaeaTu ncee.noHHMbI AIUI HJH'lecKHX nyreA. HanpnMep, ecJIH Mbl 880.ztHM:
http://eumple.com/cgi-bin/sometbing OTCIO,na He cne.nyeT, 'ITO cymecTByeT KaTaJior cgi-bin, .n:oHHMa, CCbIJialOIIterocJI Ha K8.Koit-TO .npyroA KaTanor.
JTO MO)l(eT 6o1Th JJHIIIb HMJI ncee-
PacmnpeHne HTML (OT aHrn. HyperText Marku.p Language - JIJI>IK pa3MeTKH rnnepTeKcTa) npHIDITO .naeaTu .nolC)'MeHTilM co c-rpaHHQaMH Web. HTML npe.ncTa.BJU1eT co6oA JIJLIK, Ha xoropoM Ja,naeTCJI pacnoJIO)l(CHHe rexcTa, pHC}'HKOB, mnepccLIJIOK H T. .n. KpoMe HTML qacTo BCTpe'laJOTCJI n .npyme opMaTu1 .llaHHI>IX: GIF, JPG - .wu1 H3o6pWKeHnA; PHP, PY -AIUI cQeHapnee (nporpaMM, JanycKaeMI>IX Ha cepeepe) HT. JJ.. Boo6me roeopJ1, cepeep MO:>KHO HaC-rpOHTb TIUCHM o6pa30M, 'lTOObl OH KOppelCTHO pa6oTaJI c mo6I>IMH pac1111:1peHHllMH, HanpHMep, HHKTO He JanpemaeT HaM CKOH!l>HrypHpoeaTu ero, 'ITOObl allnbl c pacw1:1peHHeM HTM TaIOKe paccMa-rpHB8JIHCb K8.IC HTML-.nolC)'MeHTI>I. 3AME'IAHHE
Eipay3epy coaepweHHO ace paaHo, KaKoe pacw111peHHe y 3anpoweHHoro o6'beKTa -
OH op111eH-
™PYeTCS1 no OTBefY, KOTOpbllii npeAOCTaBnAeT eMy Web-cepaep.
Pelt0Me B Jl:aHHoA rnaee MI>I noJHaKOMHJIHCb c OCHOBaMH yc-rpoAcTea ceTH l1HTepHeT H npoTOKOJiaMH nepe.nalfH .ztaHHblX, 6eJ KOTOpbIX Web-nporpaMMnpoeaHne HeMbICJIHMO. Mb! yJHaJJH, KaK MawHHbl a.npecyioT .npyr .npyra 8 rJIOOaJJbHOA CeTH, KaK OHH o6MeHHBWOTCll JJ.aHHbIMH, a TIUOKe paccMo-rpenH Ba)l(ffoe noHJITHe - URL, c KorophlM HaM HeOJJ.HOKpaTHo np1:1.neTCll CTaJIKHBaThCJI B )laJlbHeA111eM.
rilABA
2
lllHTepeMc CGI
M
npoToKon HTTP Jh1CTMHrM A3HHOA rllaBbl MOlKHO HaATM 8 nOAKaTanore
cgi.
TepMnu CGI (Common Gateway Interface, o6mui1 wmo3oaou HHTepcpei1c) o6muaqaeT ua6op cornaweuuR, KoTopbie .nomKHbI co6mo.naTbCH Web-cepaepaMH npu BbmonueuHH HMH pa3nH'IHbIX Web-npuno:iKeuui1. B uacTOHI.UHi1 MOMeHT npaKTH'IeCKH noaceMeCTHO ucnonbJYeTCH 6onee 6bICTpbIH H 6eJonacHbIH aapnauT HHTepeiica FastCGI. CornarneHHH, onucaHHbie a rnaee, ocTaJOTCH cnpaee,IVIHBbIMH u .IVIH Hero. B 3TOH u cne.ny10meit rnaeax Mbl 6y.neM pa36upaTb OCHOBbI Tpa.zJ.HUHOHHoro CGI-nporpaMMHpoeaHHJI, ue KacaHcb HanpHMYJO PHP. B Ka'IecTBe H3bIKa .IVIH npnMepoe Bb16paH C, IlOCKOnbKY ero KOMilHJUITOpbl MO:lKHO HaRTH npaKTHtfeCKH B nJ06oi1 onepaUHOHHOR CHCTeMe, u no TOA npH'IHHe, 'ITO OH "Han6onee Kpacueo" noKaJb1aaeT, no'IeMY··· ero He cne.nyeT ucnoJJb3oeaTb e Web-nporpaMMHpoeauuu. ,Aa-.na, 3TO He one'IaTKa. BcKope Bbl noHMeTe, 'ITO Mbl XOTeJJH CKaJaTb.
4To TaKoe CGI? fttTaK, Mbl ua6upaeM e HameM 6pay3epe
http://example.com:80/path/to/image.jpg H O:lKHAaeM, 'ITO ceiiqac nonyqnM HTML-.noKyMenT (HJJH .IJ.OKyMeHT .npyroro opMaTa, HanpHMep, pucynoK). HHblMH CJJOBaMH, Mbl paCC'IHTbIBaeM, 'ITO Ha XOCTe B KaTanore /path/to/ pacnono:iKeH lj>aifn image.jpg, KOTopb1R HaM ceiiqac .nocTaBHT (nepe.nacT ero, KCTaTH, Webcepeep, noAKJ1I01JeHHb1R K nopry 80 ua cepeepe ). 0AHaKO Ha CaMOM .nene CHyYauHH HeCKOnbKO HHaJI. no .llBYM npH'IHHaM.
Cl nYTb /path/to/, paeuo KaK u lj>aAn image.jpg ua xocTe, MO:lKeT aoo6me He cymecTeoeaTb. Be.nb a.zJ.MHHHCTpaTOp cepeepa HMeeT B03MO:lKHOCTb 3a,naTb nceBOOHUM (alias) .IVIH nJ06oro OO'beKTa ua cepeepe. KpoMe TOro, AWKe ecnu u He HaJHalfeHo HHKaKoro nceB.IJ.OHHMa, ece paeuo HMeeTCSI BOJMO:lKHOCTb True HanncaTb nporpaMMbI .IVIB Web-cepaepa, tfTO OHH 6yAYT "nepexBaTblBaTb11 Kll)l(AOe o6pameHHe K TaKHM IlyYJIM H COOTBeTCTBYIOI.UHM o6pa30M pearHpoBaTb Ha 3TO.
Cl aRn image.jpg Mo:JKeT 6b1Tb BOBCe He rpalj>H'leCKHM AOKyMeHTOM, a nporpaMMOH, KOTOpruI B OTBeT Ha Harn 3anpoc MOJJHHeHOCHO JanycTHTCSI, He MeHee CTpeMHTem.Ho Bbmon-
rnaea 2. 11Hmepc/Jeuc CG/ u npomoKon HTTP
61
HHTCJI H B03BpaTHT nonb3oearemo peJyJibTilTbl cBoeH pa6oTbI, xoTJI 6hl B TOM )l(e HTML«flopMare (HJIH, pa3}'MeeTCJI, B mo6oM .npyroM, -
HanpHMep, JTO MO)l(eT 6bITb H306pa)l(e-
HHe ). nm1b30BaTeJib H He JlOra)laeTCJI, 'ITO Ha caMoM )leJie npoH30llIJio. ,[{.ru1 Hero Bee paBHo, JarP~aeT JlH OH )lOK}'MeHT HJIH HeBOJlbHO 38JT)'CKaeT nporpaMMY. lionee Toro, OH HHKaJC He CMO)l(eT YJHaTb, 'ITO )l(e Ha caMOM .nene CJIY'IHJIOCb. nocne.D.HHH nyHKT oco6eHHO BTie'laTJIJllOUl. ECJIH Bbl npoHHICJIHCb ero H)leeit, 3Ha'IHT, TIOHJIJIH B 061..UHx qeprrax, 'ITO TaKoe CGI. Tpa,D.HUHOHHO nporpaMMbI, pa6oTa10mne B cOOTBeTCTBHH c cornaweHHJIMH CGI, HaJbIBaJOT c4eHapwwu, HJIH cKpunmw.1u -
cKopee Bcero H3-3a Toro,
'ITO B 60JlbWHHCTBe cnyqaeB HX TIHID)'T Ha Jl3blKax-HHTepnpe-raTOpax, TIO)lOOHblX Basic (HanpuMep, Ha Python HJIH PHP). 3a,D.yMaeMCJI Ha MrHOBeHbe. Mb1 TIOJIY'IHJlH JlOBOJlbHO MOUlHblH MeXaHH3M, KOTOpbIB TI03BOJIJleT HaM, B 'IacTHOCTH, «flopMHpoBaTh JlOKyMeHThI "Ha
ne-ry". K
npuMepy, nycTb HaM H)'JKHO,
'lTo6b1 B KaJCOM-TO .u.oKyMeHre npocTaBJIJIJIHCb TeKyll.\aJI )laTil H BpeMJI. PaJyMeeTCJI, MbI He MmKeM 3apaHee nponncaTb HX B JlOtcyMeHTe -
Be.ZU, B JaBHCHMOCTH OT Toro, Kor.na OH 6y,neT
3arpy)l(eH TIOJlb30BareJieM, 3Ta JlaTil JlOJI)l(Ha MeHJITbCJI. 3aTO Mbl MO)l(eM uanucaTb cueHapHH, KOTOpblH Bbl'IHCJIHT .nary, BCTaBHT ee B JlOK}'MeHT H JaTeM nepe.nacT ero TIOJlb30Baremo, KOTOpblH ,!l,a)l(e HH'Iero H He JaMeTHT! 0,nHaKO B TIOCTpOCHHOH HaMH MO)leJIH He XBaTileT OJlHOro 3BeHa. ,neitcTBHTeJlbHO, npe.nnoJIO)l(HM, HaM H)')KHO, 'lTo6bl BpeMJI B Haweu CTpaHHl..le npocTaBJUIJIOCb Ha OCHOBe qacoeoro noRca nonb30BaTeJIJ1. Kaic cueuapnit yJHaeT, tcaicoii qacoBoii noRc y peruoHa, B KoropoM
)l(H-
BeT '.HOT qeJIOBeK, HJIH KaJC)'IO-HH6y,nb .npyryio HH«flopMaumo, KOTopylO MO)l(eT npe.noCTaBHTb noJibJoBaTeJib? Bu,nuMo, .U.OJI)l(eH 61>1Tb KaKoii-TO MexauHJM, no3eoJIJ110muii noJib30BaTen10 He TOJlblCO nonyqaTb, HO TillOICe H nepe,naeaTb HH«flopMaUHIO cepeepy (B ,naHHOM cnyqae, HanpHMep, nonpastcy BpeMeHH B qacax oraocuTeJibHO MocKBbl). 3TO TO)l(e o6ecnequeaeT CGI. 0.!lHaKO BepHeMcR npe)l(Jle K
URL.
CeKpeTbl URL CToMHHTe, Mbl B npe.u.hi.nymeA rnase onucbmanu, KaJC BbirJlJl,!l,HT URL? Taic BOT, JTO 6h!Jl TOJibKO tiaCTHhlii cnyqait Ha CaMOM .nene URL HMeeT 6onee "JlJlHHHbIH" «flopMaT:
http://example.com:80/patb/to/image.jpg?parameters Kruc
HeTpy.nuo JaMeTHTh, MO)l(eT CYllleCTBOBaTb eme cTpoKa
parameters,
cne~'lOlllaJI nocne
eonpocHTeJlbHOro 3HaKa. B HeKOTopoii CTeneHH OHa aHaJIOfH'IHa KOMaHJlHOH CTpOKe
oc.
B HeH MO)l(eT 6b1Tb see, 'ITO yro.U.HO, OHa MO)l(eT 6b!Tb mo6oii JlJlHHbl ( o,nHaKO cne.u.yeT yquTblBaTb, 'ITO HeKoTOpbie cHMBOJibI .U.OJI)l(Hbl 6b1Th URL-JaKo,nupoeaHbl, cJW. Huxe). BoT KaJC pa3 3Ta-To cTpoKa H nepe.U.aeTCJI CGI-cueHapu10.
3AME4AHHE Ha caMOM Aene cy~ylOT HeKOTOpble orpaHM"leH1.u1 Ha AnMHY CTpOKM napaMeTpOe. Ho HaM npl4XOA14TCSI CTanKMBaTbCl'I c Hl4Ml4 AOBOnbHO P8AKO, "ITOObl 14Meno CMblcn 06 3TOM roeopl4Tb. EcnM URL cnMWKOM AnMHeH Anl'I 6pay3epa, TO Bbl 3TO nerKo o6Hapy>KMTe - cooreercreyt01J.1al'I rnnepCCblnKa npocro "nepecraHeT HalKMMaTbCl'I". BepHeMCJI K HaweMy npe,D;bucyllleMy npuMepy. Tenepb noJib30Barenb MO)l(eT ytcaJaTb caoii qacoBoH noRc cueuapmo, HanpHMep, TilK:
http://example.com/script.cgi?time=+3
62
L/acmh /. 0cH06hl Web-npoapaMMupoaaHUR
CueHapHA e HMeHeM seript.egi, noene TOro KaJC 3anyernTeS1 H OOJl)"IHT 31)' eTpOKY napaMeTpoB, .u.omKeH ee npoaHaJJH3HpoBaTb (HanpHMep. e03,!J,aTb nepeMeHHYIO time H npHeBOHTb eA 3Ha'leHHe + 3, T. e. 3 'laea enepe.u.) H .D.aJJbllle pa6oTaTb 1opMaT: GET cueHapuft? napaMe'Ipbl HTTP I 1 • 0
CJ flepeMeHHble OKp)')keHHJI: HHe
napaMe'Ip1>1,
REQUEST_URI;
8 nepeMeHHOli
B nepeMeHHOH QUERY_STRING COXpaHJleTCJI 3Ha'leKJIJO'leeoe CJIOBO GET.
REQUEST_METHOD -
'.3TOT JarOJIOBOK J1BJIJ1eTCJ1 o6J1JaTeJILHl:.IM (ecJIH TOJibKO He npuMeHJ1eTcJ1 MeTO.D.
u onpe,D.eJUleT a,D.pec 3ailpamusaeMoro ,IlOtcyMeHTa Ha cepsepe. TaJOKe JaJJ:aK>TCJI napaM~bI, KOropb1e nepecblJiaIOTCJI Cl\eHapHIO (ecJIH cueuapmo HH'lero He nepe,D.aeTCJI, HJIH :lKe JTO o6L1'1HaJ1 CTaTH'leCKaJI CTPaHHL\a, TO see CHMBOJibl nocne 3Hatca sonpoca H CaM 3HaK onycKaJOTCSI). BMecro CTpoKH HTTP/LO MO)KeT 6LITL ytcaJaH H .D.pyroJi nporoxon - uanpHMep, HTTP/1.1. 11MeHHO ero cornameHHJI u 6y.IJ.YT }"IHTbIBaTbCJI cepeepoM npu o6pa6oTKe .n:aHHhIX, nocrynnewnx OT nOJib30BaTeJISI, H .D.PyrHX 3afOJIOBKOB. POST)
CTpotca CL{eHapuf:r?napaMe'Ipbl JaJJ:aeTCJI B TOM :lKe CaMOM ct>opMaTe, B KOTOpOM OHa B_XO.D.HT B URL. Hennoxo 6b1Jlo 6bl HaJBaTL Jry CTJ>OKY Katc-Hu6y.IJ.h 6onee peanHCTH'IHO, 'ITOObl yqecTh soJMO*HOCTL npHCYfCTBHJI e Hen KOMalf.D.HbIX napaMe-rpoe. Tatcoe HaJeaune ,l\eHcTBHTeJihHo Cyll(ecTByeT H 3eyqur xax URI (Universal Resource Identifier, yuHsepcanhHbrn HJJ.eHTuct>utcarop pecypca). QqeHL qacro ero CMernHBaK>T c noHJITHeM URL (emJOTh .l\O roro, 'ITO TIO nponcxo.n:HT .D.IDKe s ct>Hl\HMbHOA .n:otcyMeHTaUHH no crau.n:aPTaM HTfP). )l,asaihe ,l\oroeopHMCJI, 'ITO e 6y.D.yll(eM MhI ecer,l\a 6y,l\eM HaJbIBaTL cnoeoM URL nollHblU nyTb K HeKOTopoii Web-CTPaHHL\e BMecre c napaMeTPaMH, u yq10BHMCJ1, 'ITO no.n: cnoeoM URI 6y.n:eT noHHMaTbcJI ero 'lacmb, pacnono:lKeHHaJI nocne HMeHH (unu IP-a.zi:peca) xocra H HOMepa IIOPTa.
POST LI
opMaT: POST
cueHapu£1?napaMe'!p~
LI nepeMeHHble OKp)')KeHH.11: HHe
napaMe'!phl,
HTTP/1.0 REQUEST_URI; B nepeMeHHOii QUERY_STRING coxpaHJleTCJI 3Haqe-
B nepeMeHHOt:i REQUEST_METHOD -
CJIOBO POST.
3ToT 3aroJIOBOK HcnoJib3YeTCJI npH nepe.uaqe .uaHHblX MeTO.llOM POST. BcKope Mbl paccMOTpHM .llaHHblH MeTO.ll no.npo6Hee, a noKa CKIDKeM JIHlllb, qTQ OH OTJIHqaeTCJI OT MeTO,na GET TeM, 'ITO .naHHble MO)l{HO nepe.uasaTb He TOJibKO qepe3 KOMaH.llH)'IO CTpOKy, HO H B CaMOM KOHQe, nocJie ecex 3arOJIOBKOB.
Content-Type LI opMaT: Content-Type: application/x-www-form-urlencoded
LI nepeMeHHaJI OKpy)l{eHm1:
CONTENT_TYPE
,lJ;aHHblt:i 3arOJIOBOK H.ZJ;eHTmt>HL\HPYeT THn nepe.naeaeMblX .naHHbIX. 06blqHo .ll.J'UI noro yKa3blBaeTCJI 3Ha'leHHe application/x-www-form-urlencoded, onpe,neJIJ1101Uee opMaT, B KOTOpoM Bee ynpaBJIJllOIUHe CHMBOJibl ( OTJil:fqffble OT a.ruj>aBHTHO-QHpoBblX H .npyrnx OT06pa)l{aeMblX) cneQHaJibHblM 00pa30M KO.llHP)'IOTCJI. 3To TOT CaMblH opMaT nepe,naqH, KOTOpblif HCllOJib3YeTCJI
MeTO,naMH GET H POST.
,lJ;OBOJibHO pacnpocTpaHeH
H .npyroif opMaT:
multipart/form-data. Mb! pa36epeM ero, Kor.na 6y.ueM o6c~aTb eonpoc, Kaca10mHiicJ1 3a-
rpYJKH ooJioe Ha cepeep. XoTHM o6paTHTb Ballle BHHMaHHe Ha TO, qTQ cepeep HHKaK He HHTepnpernpyeT paccMaTpHeaeMblif 3arOJIOBOK, a npocTO nepe.naeT ero cQeHapHIO qepe3 nepeMeHHYIO OKpy)l(emur.
Host LI
opMaT: Host: MMR XOCTa
LI nepeMeHHaJI OKpy)l{eHHJI:
HTTP_HOST.
B coOTBeTCTBHH c npoTOKonoM HITP I . I e 11HrepHere Ha Ka)l(.ZJ;OM y3ne MO)l{eT pacnonaraTbCJI cpa3y uecKOJibKo xocTOB. (HanoMHHaeM, 'ITO y3eJI HMeeT oT.n;eJibHhlif IP-a.npec, H BllOJIHe THllH'IHa cmyauID!, Kor.ua pa3Hble .llOMeHHble HMeHa COOTBeTCTBYIQT 0.llHOMY H TOMY )Ke IP-a.npecy.) no3TOM)' .nomKeH cymeCTBOBaTb KaKoii-TO cnoco6, c IlOM01UblO KOTOporo 6pay3ep CMO)l{eT COOOIUHTb cepeepy' K KaKOMY XOCTY OH co6HpaeTCJI o6pamaTbCJI. 3aronoBOK Host KaK pa3 H npe.llHa3Ha'leH .llJIJI 3Toif QeJIH. B HeM 6pay3ep YKa3bIBaeT TO )Ke caMoe HMJI XOCTa, KOTOpoe BBeJI nOJib30BaTeJib B a.npecHoA CTpOKe. nPHME'IAHHE
B HaCTORIJ.IMH MOMeHT Aocryn K 6oilbWMHcrey ca.:1Toe MO>KHO non~HTb TOilbKO no npoToKony HTTP 1.1 (Ho He 1.0), a 3Ha4MT, yKa3aHMe aaronoeKa Host RBilReTCR o6R3aTenbHblM. nepeMeHH)'IO
OKp)')KCHHJI
HTTP_HOST
oqeffb
'laCTO
nyTalOT
C .llpyroit
nepeMeHHOH,
SERVER_NAME. B 60JibllIHHCTBe cnyqaeB cepeepbl KOHHrypHpYIQT TaK, qTQ o6e nepeMeHHbJe
rnaea 2. llfHmepc/>eiic CG/ u npomoKOn HTTP
65
co.Zlep)l(aT OJJ.HHwcosoe JHa11euue (HanpHMep, e Apache ,lJ,JUI :noro cymecTByeT .ZlHpeKTHBa UseCanonicalName
off),
O,ZlHaKO
803HHICaJOT CH1)'a.QHH,
KOf,Zla TIO
He Ta.IC.
BeJIH'IHHa
HTI'P_HOST ecer.Zla H,ZleHTH'IHa TOM}' .ZlOMeHHOM}' HMeHH, KOTOpoe BBeJI 00Jlb30Barem. B 6pay3epe, B TO epeMJI KaK SERVER- NAME HH0f.Zl8 MO)l(eT CO.Zlep'llCan. CTJ>OKY. )l(eCTICO 3aDHCaHH)10 B KOHcl>Hrypa.QHH cepeepa (B Apache 3TO npoHCXO.ZlHT npu yKaJaHHH usecanonicalNarne on).
8HHMAHHEI Cy~ecreye-r OAHO Ba>KHoe OTnM~Me MelKAY nepeMeHHblMM HTIP- HOST M SERVER- NAME. Aeno B TOM, ~o SERVER- NAME HMKOrAa He BKmo~aeT HOMep nopTa, K KOTopoMy OOAKnlO'lMJ1CR 6pay3ep (o6bl~HO 80), B TO BpeMR KaK HTTP_HOST, HaoeiopoT, COAeP>KMT 3Ha'-IE!HMe BMA8 XOCT: nopT, KOrAa nopT OTnM'-IE!H OT 80.
Kwcyio 'llCe nepeMeHHYJO ucnoJihJOBan. e CKpHJITa.X? BepollTHee ecero, HTTP_HOST, HO e cny11ae, ecnu OHa He ycTaHoBJleHa (HanpHMep, ucnonb3yeTCJ1
HITP 1.0,
r,Zle .ZlaHHhdi JarOJIOBOK
He DO.Zl.Zlep'llCHBaeTCJI ), 00.ZlCTilBJIJITh BMecTO nee SERVER- NAME.
User-Agent Ll
opMaT: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0
L] IlepeMeHHaJI OKpY'llCeHHJI: HTTP_USER_AGENT. 'llepe3 3TOT JarOJIOBOK KJIHeHT coo6lllaeT cepeepy cee,ZleHHJI o ce6e, He acer.Ila TO'IHhie u npaB.ZlHBhle. HJ npHee.ZleHHoro ehlllle npnMepa MO)l(HO YJHan. eepcmo 6paYJepa (e .IJ.aHHOM CJIY11ae JTO Firefox) u onepa.QHonuoA chcreMhl (3.ZleCh 64-6HTHaJI Windows
8.0).
Referer Ll
opMaT: Referer: URL_a.o;pec
L] IlepeMeHHaJI OKpY'llCeHHJI: HTTP_REFERER. Kax npasHJio, JTOT JaroJioBOK cl>opMHpyeTCJ1 6payJepoM u
co,Zlep'llCHT
URL
CTpaHHQhI,
c KOTopoA ocyllleCTBHJICJI nepexo.Zl Ha TeK)'ll(YJO cTpauney no mnepcchlJIKe. Bnpo11eM, ecnu Bbl DHWere Cl{eHapHH, KOTOphlA B l{e.TUlX 6eJOnacuoCTH OTCJie'llCHBaeT 3Ha'leHHe .ZlaHHOro JafOJIOBKa (HanpnMep, ,lJ,JUI ero Janycxa TOJlbKO c onpe,ZleJieHHOA CTpaHHl..{bl), DOMHHTe, 'ITO .ZlaHHblM JarOJIOBOK MO'lKeT 6b1Tb o6peJaH 6paH.ZlMayJpoM HJIH no.Zl.Zlenau YMbIWJieHHO.
3AlllE'4AHHE Bbl, HaeepHoe, noAyManM, ~o cnoao referer n1o1weTCR no-aHmMMcKM c ABYMR 6yKBaMM "r". Aa, Bbl npaebl. 0AHaKo Te, KTO nPMAYMbl&an CTaHABPT HTTP, 3Toro, BMAMMO, He 3HanM. TaK ~o He no3eonR~Te cronb AOCBAHOMY cpaKry BeeCTM ce6R B 386nY>KAeHMe, KOrAa 6yAeTe B ~HapMM MCnonb30BaTb nepeMeHHYIO OICpy>KeHMR HTTP- REFERER.
Content-length L] opMaT: Content-length: JVIUHa
L] IlepeMeHHaJI OKpy'llCeHHJI: CONTENT_LENGTH.
66
l./acmi:. I. OcHoebl Web-npoapaMMUpoeBHUR
3aronOBOK COAep:lKHT CT)X>Icy, .llBJUllOIIzylOC.11 AeClITH'IHblM npeACTaBneHHeM MHHbl AaHHblX B 6aihax, nepeAaeaeMblX MeTOAOM POST. EcnH 3a,!J,eHcTByeTC.11 MeTOA GET, TO 3TOT 3aronoBOK OTCYTCTByeT, H 3Ha'lHT, nepeMeHHa.11 OKp)':lKeHH.11 He ycraHaBnHBaeTC.11.
Cookie D opMaT: Cookie:
3Ha~eHnH
cookies
D DepeMeHHM OKpY:lfCeHH.11: HTTP_COOKIE. 3Aecb xpaH.11TCJ1 ece cookies B URL-KOAHpoBKe ( o cookies MbI noApo6Hee noroeopHM e cne~meit rnaee ).
Accept D opMaT: Accept: text/html, text/plain, image/gif, image/jpeg
D nepeMeHHa.11 OKp)':lKeHH.11: HTTP_ACCEPT. B 3TOM 3aronoeKe 6pay3ep nepequcmie-r, KaJCHe THDbI AOKYMeHTOB OH "noHHMaer". Ilepe'lHCJieHHe npoH3BOAHTCJI 11epe3 3arurry10. K co:lKaneHHJO, B nocneAHee epeM.11 6pay3ephI CTaJIH HeCKOnhKO He6pe:lKHbl H qacTO npHCbIJlaJOT B 3TOM 3aronOBKe 3Hal!eHHe */ .. ,'ITO o603Hal!aeT mo6oil THn. CymecTByeT eme MHO:lKecTBO 3arOJIOBKOB 3anpoca ('laCTb H3 HHX BOCTpe6YJOTCll TOJlbKO npoTOKOJIOM HTTP 1.1), HO MbI He 6yAeM Ha HHX 3a,n.ep:lKHB8TbCJ1.
3MynH'-'MH 6pay3epa ~epe3 telnet Me:lfCAY npo'IHM, npn nepeAal!e 3a0poca 6pay3ep "npHTBop11e-rc11" noJib30BareneM, KOTOphdi 3anycTHJI telnet-KnHeHTa (nporpaMMY, KOTOpa.11 YMeeT noAKnlQ'laTbCJI K 3a,!J,aHHOMY IP-a,n.pecy H nop-ry, rrochIJiaTh Ha6pauHoe Ha Knaeuarype H oro6pa:lKaTh Ha 3Kpaue rrocryrraiomne 11 CHap)':lKH 11 AaHHhle) H BBOAHT CTpoKH 3afOJIOBKOB epyqHyJO, T. e. B TeKCTOBOM BH)l,e. HanpHMep, BMecTo Toro 'ITOOhI Ha6paTh B 6pay3epe http://ei:ample.com/, nonpo6yilre e KOMaHAHOi:t c-rpoKe OC (UNIX, Windows-, Mac OS X HJIH JIJ06oif Apyroit) BbmoJIHHTb cne~mne KOMaHAbl (BMeCTO KnaBHWH BBO,ll;a TeKCTa HWKHMWI COOTBeTCTBYJOll\YJO KnaBHwy): telnet exarrq:>le.com 80 GET /index.html HTTP/l.l Host: example.com
Bbl YBHAHTe, KaK nepeJJ; BaMH npoMeJibKffYT CTpoKH HTML-JJ;OKYMeHra index.hbnl. O'leHb peKOMeH,ll;yeM npo,ll;eJiaTb OTTHCaHHYJO npoue,ll;ypy, 'IT06bl H36aBHTbCJI OT JJ;yXa MHCTHl..lH3Ma rrpu ynoMHHaHHH o nporoKoJie HTTP. Bee 3TO He TaK cJIO:lKHO, KaK HHOrAa MO:lKeT noKa3aTbCJI.
rnaea 2. UHmepc/JeiJc CG/ u npomo1OkY napaMerpoe. TaICHM o6paJOM, ecnu JanycTun. e 6paYJepe
http://example.com/scriptcgi?name=Thomas&bom=l962-03-11 Mbl nonyqHM CTpaHHQY c H)')ICHblM peJynbTaTOM:
np:l'.!BeT, Thomas!
~
3HaJO, Bbl
po,n11J!J1Cb
1962-03-11!
3aMen.re, 'ITO Mbl paJ,zteMeM napaMerpld aMDepcaH.IJ.OM CTBa =. 3ro HecnpocTa. Ceff'lac o6cy.IJ.HM, notteM)'.
&,
a TIUOKe HCDOnbJyeM JHaK paeeH-
Mcnonb3oeaHMe opM1:11 llro Tenepb HaM cneJlYeT c,ztenan., 11ro6b1 nonbJoearem. Mor e y.IJ.o6Hoii cpopMe eeecTH ceoe HMJI H BoJpaCT? 011eeHJJ;Ho, npH.IJ.e'l'CJI coJ,ZJ.an. 'ITO-TO epo,zte ,ztHanoroeoro OKHa Windows, TOnbKO e 6paYJepe. HTaK, HaM noHa.u,o6HTCll o6wrnb1D HTML-.ztOKYMeHT (HanpuMep, c HMeHeM getfonn.htm H pacnono)l(eHHhdi e KopueeoM KaTa.11ore) c 3neMeHTilMH 3TOro .IJ.Hanora nonJIMH BBO,zta H KHOnKoii, npu HWICaTHH KOTOpoii 3anyCTHTCll Ham cueHapun. TeKCT 3TOfO .IJ.OKYMCHTa npHBe,zteH B nHCTHHre 2.1.
< ! IXlCTYPE html>
noJCYMeHT c ~PMO~
Bee.w-iTe ~:
Bee.w-iTe ~aTY ~eH:l'.!R:
3arpYJHM Ham .11.0KYMCHT B 6paYJep. IlonyqHM cpopM)', Dpe.IJ.CTaBJICHH)'JO Ha pHC. 2.1. Tenepb, ecnn JallecTH e none name ceoe HMJI, a e none .IJ.M eoJpacrn - eoJpacT H HWICaTb KHOnKy HaEMHTe KHomcy, 6payJep o6paTHTCJI K cueuapn10 no URL, yKaJaHHOM)' e aTpH6yre action Tera cpopMbl:
http://example.com/scriptcgi
70
4scmt. I. OcHoet.1 Web-npoapaMMupoesHUR
+- -+
• C: . [.'} example.com/getlorm.html
Bae~ RMS:~~~~---------------;
E
__________ _
BaeJDJTe ;urry po:&AeBIUI: :H8M3MCTIIO :~---------,
!_____
~
---------------------------------
PMC. 2.1. HTML-¢opMa
OH nepe.nacT 11epe3 ? BCe napa.MeTPLI, KOTOpLle noMew;eHLI BttyrpL TeroB B «t>opMe, OT,lleJUl.R HX aMnepcaH,llOM (&). 11MeHa noJieA H HX 3Ha'l:eHHJI 6y.nyT pa3,lleJieHLI 3HaKOM =. Tenepi. Bbl IlOHHMaere, no'leMY MLI c Ca.Moro Ha11ana HCnOJIL30BaJIH 3TH CHMBOJILI? liTaK, peaJILHLltt URL, KOTOpLilt c«t>opMnpyer 6pay3ep npH CTapTe cueHapIDI, 6y,Ller TIUCHM (Y'l:HTLIBaJI, 'ITO Ha CTPaHHUe 6i.m noJIL30BaTeJIL no HMeHH Thomas, H OH po.nHJicR I I Mapra 1962 r.):
http://example.com/script.cgi?name=Thomas&born=t962-03-11 A6conlOTHblM M OTHOCMTenbHblM nyrM K c ...eHapMIO 06paTHM BHHMaHHe Ha IlOJie action Tera . IlOCKOJILKY 3HatieHHe 3TOfO nOJIJI He npe,lleap.11erc.11 cnerneM (/), TO npe.LlCTaBJIJleT co6oA OTHOCHTeJILHLilt nyri. K cueHapmo. To ecn. 6paY3ep npn aHaJIH3e Tera noni.rraerc1 BLl,llaTL 3anpoc Ha 3anycK cueHapn.11, HMe10mero HM.II script.cgi n pacnoJio:lKeHHoro e TOM :lKe ca.MOM KaTaJiore, llTO n opMa (TOtIHee, HTML.noKYMeHT c «l>opMott). 3AME'IAHHE
KaK Bbl, HaeepHoe, AOraAanMCb, TepMMH "KaTanor'' 3AeCb BeCbMa ycnoeeH. Ha caMoM-TO Aene MMeeTCA B BHAY He pe811bHbllii K8Ta110r Ha cepeepe (o KOTOpoM 6pay3ep, KCTarn, HMl.ferO He 3HaeT), a 1.1aCTb URL, npeAweCTBYIOLllBA nocneAHeMy CMMaony I a nonHOM URL cpaliina c cpopMolii. B HaweM CflYl.lae 3TO npoCTo http://example.com. 3aMeTbTe, l.ITO 3AeCb Y1.1ITTb1eaeTcA MMfl xocT8. KaK BMAMM, ace 3TO Mano noxo>Ke Ha o6bl1.1Hyt0 cpalii11oeyt0 cne1.1HcpMtpMa, HJIH HeT. 3TO no3BOJIJleT C03.z:taBaTb caJtn.1, pacnOJIO:lKeHHhle Ha HeCKOJlbKHX xocrax, "npo1pa11Ho" .llJIJI HX noceTHTeneA. BcJ1 H.z:teononut ceTH HHTepHeT H cJI)'llC6hl World Wide Web nocTpoeHa Ha :noA H.z:tee- eo1MO)l(HOCTH ceo6o.z:tHoro nepeMemeHHJI (H ee nerKOCTH) no mnepccblJIKaM, r.z:te 6bl HH Haxo.z:tHJICJI cepeep, Ha KOTOpbIA OHH )1C83bIBaIOT.
MeTOA POST M opMbl llro llCe renepb H)')KHO C.z:tenan., 'ITOObl nocnaTb .ll8HHhle He MeTO.llOM GET, a MeTO.z:tOM POST? HeTpy.z:tuo .z:tOra,llaTbC.11: .z:tOCTaTO'IHO BMeCTO method="GET" )'ICaJaTb method•" POST". liOJlbWe HH'l:ero MeHJITb He Ha,llO. 3AME'IAHH~ EcnM He 38A8Tb STpM6yr method B Tere Boo6~e. TO no YMO.INSHMIO nOAP83YM8B88TCA M8TOA GET.
TaICHM o6p830M, Mhl MOllCeM renepb COBCeM He HarpJ:llCaTb nO.Jlb30BaTeJIJI TaICOA HHclK>pMaQHeA, KaIC HM.II CQeHapHJI, ero napaMeTpbl HT. .z:t, OH ecer.z:ta 6y.z:teT HMCTh .llCJlO TOJTbKO c IlOJlJIMH eeo.z:ta, nepeKmO'laTeJIJIMH H KHOIIKaMH clK>pM!.1, a T810Ke c mnepccbIJTKaMH. 0.z:tHaICO 8 paccMOTpeHHOA cxeMe He see fJla,lllCO c TOtfKH 3peHH.11 npocTOTbI: CQeHapHA O.z:tHH, a «tiaA.na-To .z:tea (.z:toKyMeHT c clK>pMoA H ct>aAn cQeuapu.11). EcTb npocroe o6xo.z:tHoe peweHHe 3TOit IlpOOJieMbl, KOTOpoe peKOMeH.ll)'eTCJI npHMCHJITb BCIO.z:tY, r.z:te TOJlbKO 803MOllCHO: nyCTb cQeHaplltt e nepeyio 011epe.zn, npoeepJ1eT, 1anymeu JTH OH c napaMeTpaMH HJIH 6e1 HHX. Ecnu napaMerpoe HeT, TO cQeuapHA Bbl.llaeT noJib3oearemo HTML-.z:tOKJMeHT c clK>pMolt, e npoTHBHOM C.II)"lae- pe3yJibTaTbl pa60Tbl. 3TO Y.z:t06HO eme H nOTOM)', ti:TO, 803MOllCHO, Bbl 38XOTHTe, 'IT06b1 noJib3oeareJib o6Jl3aTeJibHO seen ceoe HM.II. To ecTb, ecJIH OH 1a6y.z:teT 3TO c.z:tenaTb, eM)' 6y.z:teT Bbl.llaHa eel! Ta xce «tiopMa c coo6meuneM HanpoTHB noJIJ1 eeo.z:ta .llJIJI HMeHH:. "l13BHHHTe, HO Bbl 3a6bIJTH BBeCTH CBOe HM.II. Ilonpo6yAre eme, B.z:tpyr Ha 3TOT p83 no.II)"IHTCJ1? 11 A e cne.zzyiomeA rnaee Mbl nonyTHo paccMOTPHM, KaIC npome ecero onpe.z:teJIHTb, 6bIJT 3aII)'UteH CQeHapHA no HaXOeHbI CGI-cueHapHH.
~3blK
C
Ka)l(.L(brli nporpaMMHCT o6H3aH xOTH 6b1 e o6mHX qe~ JHaTb, KaIC "Ha HHJKOM ypoene" pa6oTaeT TO, 'ITO OH HCil0Jlb3yeT -
6y.L(b TO onepauuoHHaH CHCTeMa HJIH Y.LIOOHbIH H3blK-
HHTepnpeTaTOp .L(JIH HanHCaHHH cueHapuee (KaICHM HBJIHeTcH
PHP).
l-1TaK, peqb noH,neT o nporpaMMHpoeaHHH Ha C. Mbt Bbt6paJJH ero, noTOMY 'ITO uMeHHO Ha TIOM HJbIKe qame ecero nmnyr cueHapuu, KOTOpbtM TJ>e6yeTcH Hau6onee TecHoe eJaHMO.L(eiicTeue c cepeepoM H MaICCHMaJJbHO KpHTH'IHoe 6blCTpo.L(eiicTBHe (6a3bl .L(aHHbIX, TIOHCKOBble CHCTeMbl, CHCTeMbl noqToeoii paCCblJIKH c COTHHMH TblCll'i TIOJlb30BaTeneii H ,Lip.).
8
Il0Jlb3Y ll3b1Ka
c roeopHT TaIC )l(e H TO, 'ITO ero KOMilHJUITOpbl MO)l(HO BCTJ>eTHTb npaICTH'ie-
CKH 8 mo6oii CKOJlbKO-HH6y,Lib cepbe3HOH onepal.{HOHHOH CHCTeMe. Bbl He HaH.nere 8 3TOH rnaee HH 0.L(HOH cepbe3HOH 3aICOH'ieHHOA nporpaMMbl Ha KJIIO'ieHHeM paJee 'ITO caMoit npocTOA, rnna "Hello, world!").
11 ,Lla)l(e
c (3a HC-
6onee TOro: ece nuc-
THHrH nporpaMM, npe.LICTaBJieHHbie e cneey10mux rnaeax, .L(aJJeKH OT H,LleaJJa H MOryT 6b1Tb C.L(eJiaHbl 6onee YHHBepCaJJbHblMH, HO JaTO H 3Ha'IHTeJlbHO 6onee MHHHblMH. AnHHOH npulllJIOCb IlO)l(epTBOBaTb H Bb16pan npocrory.
8
.L(aHHOH rnaee Mbl IlOilblTaJJHCb OilHCaTb see OCHOBHble npHeMbl, KOTOpbte MOryT TIOH8,!{0-
6HTbCll npH nporpaMMHpoBaHHH cueHapuee Ha
c (KpOMe pa60Tbl c COKeTaMH, 3TO TeMa Mll
OT.L(eJibHOii KHHrH). flo B03MO)l(H0CTH Mbl He 6y.L1eM npHBll3blBaTbCH K CTiellH$HKe KOHKpeT-
73
rnaea 3. CG/ U3Hympu
HoA onepaQHoHHoR eHeTeMld, Be,itb CGI - 3TO eTa.H,ltapT, HeJaBHeHMbm OT onepauHoHHoR eHeTeMbl, Ha KOTOpoA 6y):{eT BbmOnIDITheJI eueHapHA. Boop}"lCHBWHeb MaTepHaJIOM :noA fnaBbJ, MO:llCHO HanHeaTb eaMbJe pa3Ho00pa3Hble euenapHH - OT npoeTblX .lJ.O eaMbJX eno)ICHldX (npaB):{a, .lJ.M noene):{HHX ncrrpe6yeTCH TaIOKe He,IUO)ICHHHWI eHopoBKa). Hama uenb - ua6pocaTh OOIIlHMH MaJKaMH, KaK Hey,ito6Ho (BOT HMeHHO - Hey'A06Ho!) nporpaMMHpoBaTh eueHapHH Ha Jl3hlJCax, o6bl'IHblX .lJ.M npHICJJ8,nHOro nporpaMMHera (B TOM qnene Ha C H C++). KaK ronbKO Bbl npoHHICHeTeeb noR H,lteeR, MW nnaBHO H He Topomeb .lJ.BHHeMeH B MHp PHP, r.ite npe.lJ.)'eMoYpeHbI npaKTH11ee1CH Bee y,ito6CTsa, TIUC Heo6XO.zmMl>le eep~3HOM)' HlbIK)' nporpaMMHpoBa.HHJI eueHapHeB.
Pa60T8 c MCXOAHblMM T8KCT8MM Ha
c
Eenu Bbl He 3HaKOMbl e "3bllCOM C, He OT'laHBa.i:treeb. Bee npnMepw xopomo xoMMeHTHpoBaHbl, a eno:>KHble yqaeTKH He ffY'IC,llaJOTCH B HenpeMeHHOM noHHMaHHH "c nepBoro npo1neHHJ111. Eme pa3 oroBOpHMeH, qro Marepuan :noR rnaBbl npe):{HaJHaqeH .lJ.M Toro, 1ITOObJ Bbl nonyqHJIH npu6nu3HTenbHoe npe,iteTaBneHne o6 yeTpoReTBe nporoxona HTTP H HHTepli>eRea CGI H YJHaJIH, JCaIC nporpaMMbl BJaHMo,iteAeTBYJOT e ux noMOIUblO. BeponHo, 6e3 3THX 3HaHHR HeB03MOXeeenoHanbHWI pa60Ta Ha nonpnme Web-nporpaMMHpoBaHHJI.
Tax 1ITO He oco6eHHO paceYpaHBaRTeCb, ecnu Bbl eoeeeM He 3HaeTe C, - Be.lJ.b na rnaBa eo):{epXoPM H HX HaH6onee none3Hb1e aYpH6yn.i, npHeMbl eoJ,itaHHJI 3anpocoB H MHoroe ,itpyroe. Bee 3TO, 6eJyenoBHO, noHa.no6HTCJI HaM H npH nporpaMMHpoBaHHH Ha PHP.
Boo6me, H3yq8" npHBe,l:{eHHhle B ,lJ,aHHOR rmme IlpHMepbl, Bbl MO)ICeTe H He npoBepnb HX Ha npaKTHKe (oco6eHHO ee.!TH y sac eme HeT pa60Taiomero Web-eepeepa). Bee nHeTHHrn 3.l:{eeb npe.ll.HaJHaqeHbl nHIDb M" reopeTHqe,cxux ueneA. I
0.l:{HaKO, eenH Bbl see )Ke peurnreeb 3anyeTHTh napy-Ypo8xy npoBepo1IHbIX CGI-cxpumoe, HanHeaHHbIX Ha C, 3Haihe, 11ro no HeenO)ICHO. BHaqane uexO.lJ.HbtA xo.u. Heo6xo.u.HMO npeo6pa3oeaTb B HenonHJleMblA lj>aJtn, T. e. OTKOMDHJIHpoBaTh, npu11eM B TOA onepauHOHHOi:t eueTeMe, B KOTOpoA 6y,iteT JanyeKaThC.JI expHDT (Henonm1eMble lj>ai:tnbl B pa3HblX oc eHnbHO pa3nH1IalOTC.JI). J:lanee e rnaBe npe.11.nonaraeTC.si, qTo Bbl pa6oTaeTe a UNIX-no.11.06Hoif onepauuoHHOif eneTeMe H BaM .ll.OC1)'11eH GNU-xoMnHJI.JITOP gee. J:lng KOMTIHnHpoeaHHH nporpaMMhI .11.oeTaro11HO BbinOnHHTb KOMaH.lJ.)': gee seript.e -o "seript.egi"
npu 3TOM e03,itaeTC.JI HenonHJleMbIB li>aiin seript.egi, Ha KOTOpblH MOXopM (KaK HMeHHO- 'IHTai:tTe .11.anhwe). npH Henonb30BaHHH Windows MO)KffO BOCDOnb30BaTbe.JI BHpyYaJihHOH MamHHOH (CM. 2!la6}'
53).
74
l/acmb I. OcHoebl We~poapaMMUpo68HUR
nepeAa"la AOKYM&HTa nonbloeaTent0 BHatiaJie pacCMOTJ>HM 6onee rrpocTOtt Bonpoc: KaIC nporpaMMa IlOCbIJiaer CBOtt OTBet (r. e. .IlOKyMeHr) noJib30Batemo. C.aenaHo Jto rrpocro H norntfHO (a rJiaBHoe, yHHBepcaJibHo H nepeHocHMo Me)K,lzy onepauHOHHDIMH CHCTeMaMH): CQeHapHA npocro IlOMew;aer .IlOKYMeHT B cmmu)apmHblU nomoK BblBO(B ll3bIKe C OH Ha3bIBaeTCll stdout), KOTOpblit HaxO,IlHTCll no.a KOH'I'pOJieM nporpaMMHOrO o6ecneqeHHll cepsepa. HHbIMH cnosaMH, nporpaMMa pa6oraer raic, KaIC 6y.nro Her HHKaICoro noJib30BaTeM, a Hy)ICHO BbIBeCTH TeKCT npllMO Ha 3KpaH. 3ro OHa raK 11 .nyMaer", Ha CaMOM .aene BbIBO.IlHMM HH4>opMauH1 6y.aer nepeHanpaBJieHa cepeepoM s 6pay3ep non1:.3oeareM. JICHO, tfTO y CQeHapHll HHKaICOro 11 3KpaHa" Her H 6bITb He MO:JtCeT.
oa
3AME'IAHHE
FastCGI a3a..,MOAeli1CTByeT c Kll..,eHToM 'lepe3 coKeTbl ..,11.., TCP/IP-CeA..,HeH..ie.
Orser rrporpaMMbl, KaIC H 3anpoc nOJib30BaTeM, .aoJI:JtCeH COCTOllTb H3 3arOJIOBKOB. HHblMH CJIOBaMH, Mbl He MO:JtCeM npocro HanpaBHTb .IlOKyMeHT B CtaH,!laptHblit noTOK BblBO,Ila: HaM cHatfaJia Hy)ICHO, no KpaliHeH Mepe, yKaJaTb, s KaICOM cpopMare HHpMaQHll .aoJI:JtCHa 61:.1r& nepe.aatta noJI&3oearemo. ,Zl,eAcreHreJI&Ho, npe.acTaB1:.re, tiro npmnofuler, ecnu 6pay3ep non1:.1raerc1 oro6pa3HTb GIF-pncyHoK s teKCTOBOM BH.!le? B xy.aweM cnyqae eawHM noJI&3osateMM npH,IleTCll BCIO :JICH3Hb JietfHTbCll OT 3aHKaHHll - oco6eHHO eCJIH .ao 3TOro HX npoCHJIH BBeCTH HOMep Kpe.aHTHOtt KaptOtfKH ...
3aronoBKM OTB8Ta 3arOJIOBKH OTBeTa ,IlOJI:JICHbl cne.aoeatb TOtfHO B TaICOM )Ke ct>opMaTe, KaIC H 3aroJIOBKH 3anpoca, paCCMO'I'peHHble HaMH B npe.abIJzyI.UeH rJiaBe. A HMeHHO, 3TO Ha6op CTJ>OK (3aBepwaiolllHACll rrycroA CTJ>OKOH), KWK,L{M H3 KOTOpblX npe.nctaBJilleT co6oi:t HMll 3aroJIOBKa H ero 3HatieHHe, pa3,neneHHbte .neoero•rneM. HaJIHtiHe nycTOro 3aronoeKa e KOHQe raic :Jtee MO:JtCHO HHtepnperHpoeatb, KaK .nea CTOlllllHX no.npll.Il CHMBOJia nepeeo.na CtpoKH \n \n. 3areM, KaK o6bttfHO, MOryt CJie.!lOBaTb .!laHHbte oreera, llBJilllOlllHeCJl .!lOKyMeHTOM, KOTOpbIA 6y.ner OT06pa:JtCeH 6pay3epoM.
3aronoeoK KOAa OTBeTa 3.aecb ece )Ke HMeetCll O,IlHO OTJIHtfHe OT cl>opMara, KOTOpblH HCnOJib3yetcll B 3arOJIOBKax 3anpoca. ,Zl,eJio B roM, tiro nepe&1A 3aronoeoK omera o613aH HMetb cneraa cneQHcl>HtfHbIB BH.!l- B HeM He .!lOJI:JICHO 61:.rrb ,IlBOeTOtfHll. OH 3a,IlaeT TaK Ha3bIBaeMblli KoO omeema cepeepa H BblrJIJl,IlHT, HanpHMep, true: HTTP/1.1 OK
HJIH TaK: HTTP/1.1 404 Not Found
B nepeoM npuMepe 3aronoBOK roeopur 6pay3epy, tiro ece e nopJ1,nKe H ,naJI1:.we cne.nyer neKorop1:.1u .noKyMeHr. Bo eropoM npHMepe coo6maerc1, tiro 3arpe6oeaHHbtA ct>attn He 61:.m Haii.neH Ha cepeepe. KoHetiHO, cymecreyer eme MHO:Jteecreo .npyrnx Ko.uoe owH6oK.
75
rnaea 3. CG/ U3Hympu 11
nOAQem11&aHM811
38ronoau OTB8Ta
IlpH pa60Te c HeKOTOpLIMH pacIIpOC'I'p8HeHHLIMH KO,ll8MH OTBeTa (404, 403 HT. ,ll.) 6paYJepbl He o6paIIUUOT oco6oro BHHMaHIU: Ha 3arOJ10BOK KO,lla O'l'Be'I'a, a npocro BhlBO,llllT CJle.zzyK>mmt 3a HHM ,llOKyMeHT. HecMOTpl Ha ro, trro KO.ll OTBCTa cl>opMHpyen:1 cepsepoM e nepsyio 011epe.zu,, eme .llO JanycKa cueHapm MY MoxceM H3Memm. ero B CGl-cICpHme npe noMo~ "cl>HKTHBHoro" JaronoeKa Status. HjnpHMep, Hane11aT&B B CICpHOTC Status: 404 Not Found
Mhl 38CT8BHM cepsep DOCJian. 6paYJCpy KO,ll OTBeTil 404, a TaKXCe BhlIIOJlHHTh CTaH,llapmhrlt o6pa6or'IHK 404-A OIIlH6KH, CCJIH OH Ha3Ha11eH ,llHpeKTHBOA Web-cepBepa IlpH 3TOM CBM 3arOJ10BOK Status OTDpaBJieH He 6y,ller - OH o6pa6an.maerc1 cepBepoM H DOCJle 3TOro Bblpe3aeTCl. IlpHBe.lleM ,llpyrne HaH6onee pacnpocrpaaeHHhle 3arOJIOBKH OTBeTa
Content-type opMar: Content-type: mime_'I'UI1; charset=utf-8
3a,llaeT THD ,llOKyMeHTa H ero KO,llHpOBKY. IlapaMerp charset yICa3b1BaeT KO,llHJX>BKY .llOKYMeHTa (B HaIDeM DpHMepe no UTF-8). none mime_= onpe,llemleT THD HHcl>oPMailHH, KOTOpyio CO,llepxcHT .llOKyMeHT:
a
text/html -
HTMl...-,llOKyMeHT;
0 text/plain - npocroA TeKCTOBLdl ct>aJUi;
a
image/gif -
GIF-H306paxceHHe;
['.] image/jpeg -
JPG-H3o6paxceHHe;
eme HecKOJlbKO ,lleClTICOB .llPyrHX THDOB.
Pragma opMar: Pragma: no-cache
3anpemaer IOIIIHpoB8HHe .llOKyMeHTa 6paYJCpoM, TaK 'ITO npH DOBTOpHOM BH3HTe Ha crpaHHizy 6pay3ep rapaHTHpoB8HHO 3arpYJHT ee CHOBa, a He H3BJle'leT H3 CBoero IOIDa. 3To MO)l(eT 61.rn. none3HO, ecJIH CTp8HHQa CO,llepXCHT, HanpHMep, ,llHHaMfftleCKHA C'ICT'IHK nocemeHHA. 3aronoeoK Pragma ncnom.3yen:1 TaIC '.lKe H .llJ1JI .llP)THX Qenei:t (H cOOTBeTCTBeHHO, nocne .llBoeTO'IHJI HaxO,lllTCJI HHhlC 3H8'1CHHl CTJX>KH), HO Mhl He 6y.JJ.CM HX 3,llCCL paccMaTpHBaTb .
. Location opMar: Location: http://www.otherhost.com/somepage.html
76
4acm& /. 0cH06bl Web-npoapaMMupoeaHUR
'.)roT JaronoBOK OCOOeHHbdi H onpe.n.eJIJleT, 'ITO 6pay3ep nonb30BaTem1 JJ.OJDKeH HeMeJJ,neHHO nepettrn no YKaJaHHOM)' a.ztpecy, He JJ.O)KHJJ.WICb Tena JJ.OK)'MeHra oTeeTa (KaK 6y.n.rn 6b1 n~nbJoearenb Ca.M Ha6pan B a.ztpecHolf CTpOKe H)')KHbdi URL). TaK qrn ecnu Bbl co6upaerecb HCilOilbJOBaTb JaronOBOK Location, TO 38. HHM He JJ.Oil:lKeH CJieJJ,OBaTb JJ.OK)'MeHT. 3AME'IAHHE PeKOMeHAY8TCA B 3aro11011Ke Location acerAa YK83blB8Tb at5contOTHblM "YTb BMeCTe c MMeHeM xocra, a He OTHOCHT8IlbHblj'.:1. ,Qeno B TOM, "ITO. K8K nOK83b1BaeT npaKTHK8, He ace 6pay3epbl npallHilbHO pearnpytOT Ha OTHOCHT8IlbHble "Y™ H BblTBopAIOT ace, "ITO HM 3a(511aropaccyAHTCA.
Set-cookie opMaT: Set-cookie:
napaMe'JP~_cookie
YcTaHaBJJHeaeT cookie e 6paYJep nonbJoeareJIJI. B KOHUe :noA rJiaBbI (cJ\.f. pa30. "f../mo maKoe cookies u ,, c 'leM ux eoam "? '') Mbl paccMOTpHM no.npo6Hee, 'ITO TaKoe cookies u KaK c HHMH pa6oraTb.
Date opMaT: Date: Sat, 08 Jan 2000 11:56:26 G>1T
YKaJbIBaeT 6paYJepy .nary ompaBKH .llOK)'MeHTa.
Server opMaT: Server: nginx/1.4.6 (Ubuntu)
y CTaHaBJIHBaeTCJI cepeepoM H YKaJblBaeT 6payJepy THU cepeepa H .u.pyryio
HH$opM~HJO
o cepeepHoM nporpaMMHOM o6ecneqeHHH.
npMMepbl CGl-c ...eHapMee Ha
c
Hacrnno epeM.11 npueecrn He6oJiblliOH cueHapnA Ha C, HJIJIIOCTpHpYJOI.UHjf HeK0Topb1e eoJMO)l(HOCTH, KOTOpble 6bIJIH onucaHbl Bblllie (nHCTHHr 3.1 ).
#include I I ,Ul1peKT11Ba Hy;iKHa JJJIR 11tH111J,11a.I!J.13d!.lX1'1 i;LlYHKI.lX1'1 rand () #include II BKJl]()qaeM no.!Ulep~ i;LlyttKll.11t1 eeoDaleb!BoDa #include II A 3TO - .IIJIR no.!Ulepl!CKll! iPYHKU.ll!lll rand() II rnaettaR iPYHKU.ll!R. YlMeHHO otta 111 3arrycKaeTCR np111 cTapTe cuettap111R int main (void) { II l1Hl1l.l,ll!an11t3111pyeM rettepaTop CJiyt.Iai7!HblX 4!1Cen int num; time_t t; srand (time ( &t) ) ;
rnaea 3. CGI uJHympu
77
II B num 3arn1CblBaeTCR CJIY'laHHoe tn1CJ10 OT O ;uo 9 n\.Un - rand() % 10;
II ~anee BblBO.IIHM 3aI'OJIOBlJ yBHJI11M a OpaY3epe printf(""); printf(""); printf(""); printf("C!rytiail!Hoe tn1cno"J;1 printf(""); printf(""); printf(""); printf("3.apaBCTB~Te!");
printf("
CJ!yt,iaf!Hoe tn1cno a JI11ana30He 0-9: %d
", num); printf(""); printf(""); rne nporpaMMHoro KO.lla, "leM HTML-Teroe). HTaK, nporpaMMa B 1111cTHHre 5.1 npe.llCTaBJI.ffeT co6on HTML-eTpaHHU.Y c "e11M" KO.llOM Ha PHP. A pa3 TaK, TO HaJOBeM ee, HanpHMep, sq.php 11 paen0.10)1(ffM B KaTanore .llJl.!I .llOKYMeHTOB Ha Webeepeepe. Tenepb e TOlfKH 3peHHJI Web-no11bJoeare11.R oHa - npoeTO eTpaH11ua. PneyHoK 5.1 .!IBJI.ReTeR HJIJIJOeTpauueif OTKpbITOro e 6pay3epe npuMepa. Bee BblrJIR.llHT TaK, KaK 6y.llTO Mbl npoeTO OTKpbIJIH o6b£lfHYJO Web-cTpaHHlfKY. noKa "ITO Mbl npueBOHJIH 3TOH CTpau11ue paeurnpeHne PHP .llJl.sI Toro, "IT06b1 eepeep CMOr noH.RTb, "ITO eMY H~Ho Ha eaMOM nene ucnonb30BaTb PHP-HHrepnpernrnp .llJI" o6pa6oTKH noKyMeHTa. Boo6I11e, MO)l(HO HaCTpOHTb cepeep TaK, "ITOObI OH accounupoean PHP e JJI06b1M pacrnnpeuueM. 0.llHaKO ceJiqac .llJlj( npocTOTbI MbI .noroeopuMcR naeaTb PHP-eueHapH.sIM pacumpeuue PHP.
125
lnaea 5. XapaKmepucmuKa R3blKB PHP
~
• --~- -
-- -__
C · D localhost:4000/sq.php ...,_·--~-·:;.
___________ -···------- --- - -----'- - --·· _,..
3;:tpaBCTByiiTe! TO.J'~ ;i;na 18.10 15 ro~ Tn1"1Iltt ape.U1: 1141: 3 5 A BOT~ 1111.)iiw nepswx 5 Rl!IT)"J>IL'll>HWX 'DICe:t • la~=l.la11.)-6c=l • 2·~=4. :!llll.)-6c=8 • J B U!l,lpllTe = 9. J B J;}-6c = 27 • 4 B ~llTe = 16. 4 8 ll.)-6c = 6-1 •. 5 B oa,:1,pue = 25. 5 a t..")-6c = 125 PMc. 5.1. Pe3ynbTaT pal5oTbl c1.1eHapHS1 Ec,1H B pe3ynbTilTe pa6oTbI CKpHOTa BblBO~HTCJI npeeynpe)f()\eHHe 0 TOM, 'ITO B HaCTPOifKax He 38,AaH qacoeoif OOJIC (pHC.
5.2),
cneeyeT BbICTIUIHTb 3Ha'leHHe .AHPCKTHBe date. timezone
B O~HH H3 DOMCp)K.HB3CMblX 'IaCOBblX 00.!ICOB, HanpHMep, 'Europe/Moscow'.
u
• -:c~ C . localhost:4000/sq.php ------·-'--···---.. _._... _____.,:.. ___ ·_:...:..: _____ :.. ____ , ··"- ---·-·-----·----
-··--· ···----·-
-·----·-----···-
3;:tpaBCTByiiTe! '\Vaniq date(): It i5 not sale to rely on the system's timezone settinp. You are •required• to use the date.timezone setting or the date_default_timezone_5Ct() functioo. In case you used any of those methods and you arc \1ill l!ettinl! this wanung. you most likely Dlllispelled the timezone identifier. We selected the timezone 'LIC' for now. but please set date.tuna~ to ~lect your tunezoue. w D:\dropbo:s:lpbp\cocho\pllpbasics\sq.plap on line 12 Warams: da1e(): lt is not liafc to rely on the system's timezone settings. You are •required• to use the date.timezone setting Of the date_default_timezone_r.et() function. In caae you used any of thOlie methods and you are still l!c1lllll!" this w~. you most hltely misspelled the tunezoue identifier. We selected the timezone 'FfC' for now. but please se1 date.timezone to select yOUJ timezone. m D:\dropbnlplsp\cock\plq>basics\Mi.plip on line 14
Tu;yI11U ,111mL: 18 .10 15 ro~ Tea.)'lllee apeia: 08:51:31 PMC. 5.2. npeAynpe>KABHHe 0 HeHacrpoeHHOM
.[l.mi :noro
~aCOBOM
nosice
B KOHHrypa.QHOHHOM aiine php.ini HMO HaifTH ~HpeKTHBY date. timezone
H npHBCCTH ee K cneeyrow.eMY BHey:
date.timezone = 'Europe/Moscow'
126
4acm& II. 0CH06bl R3btKa PHP
Cne,zzyer o6paTHTh BHHMaHHe, lJTO B KatJeCTBe KOMMeHTapIDI B KOHaj:fJie BbicrynaeT TOlJKa c Jarnnoj:f, KOTOp)'IO H)')KHO y6paTb, eCJIH OHa pa3Mell.{eHa B naqane .llHpeK-
PHP 35.
THBbl. C HaC'rpoiiKon 3HaKOMHMCSI B ?Jla6e
11 KOH«l>ttrypauHoHHbIM «l>aiinoM php.ini MbI 6011ee no.npo6Ho no-
PeJt0Me B
JJ.a.HHoj:f rmme MbI noJHaKOMHJIHCb c HCTOpHej:f pa3BHTHJI
PHP,
co6hITHJIMH H COLUtaJibHbI-
MH CeTSIMH, KOTOpbie nOBJIHJIJIH Ha ero pa3BHTHe. KpoMe Toro, Mbl n03HaKOMHJIHCb c HOBOBBe,D.eHHJIMH
PHP 7. B
Ko Hue rnaeb1 Mbl no11yq111111 HatJaJibHOe npe.D.CTaenenHe o TOM, KaK
BhlrJTSl.llHT THJIH'IHblj:f cKpHm Ha
PHP.
rnABA
6
nepeMeHHble, KOHCTaHTbl, TMnbl AaHHblX JlHCTHHrH )la.HHOlt r.1aBbl MOlKHO HaltrH B IlO)lKaTa.aore
expr.
Bo3MO'lKHO, Bbl 3aMeTHJIH, CTp)'IC1)'J>a PHP-nporpaMMbI oecbMa HanOMHHaeT cMecb )J3bIKOB Basic H
C,
.11.a ew.e c oKJllOqeHH)JMH Ha HTML. lJTO 'lK, Tax OHO, o o6meM, H ecTb. 0.11.HaKo o
npe.llbl.lJ.YI.QeR rnaoe Mbl pacCMOTpeJIH JIHlllb oqeHb npocroii npHMep nporpaMMbI Ha PHP, no3TOMY opg.11. JIH cMo'lKeM ceiiqac yBH.lleTb o6w.yio KaPTHHY )J3bIKa. Tenepb HaCTano opeM)J 3a.If)JTbC)J KOHCTpYKl.lH)JMH PHP BUJIOTH)'JO. HaqHeM MbI c OCH OB 11Jb1Ka. MrnK ...
nepeMeHHble Kax H o nl06oM .11.pyroM ULIKe nporpaMMHpoBaHH)J
(Ja
HcKJTIOqeHHeM, MO)f{eT 6blTL, )JJblKa
Forth), o PHP cyw.ecTByeT Taxoe noH)JTHe, KaK nepeMeHHaR. ,[{IDKe o npocTOM npHMepe, KoTOpbm 6hm onucaH o npe.1lbl.1lYlll.eii rnaoe, MbI HCnOJibJOBaJIH
3 nepeMeHHbie!
npn nporpaMMHpOBaHHH Ha PHP npHHJITO He CK)'TIHTbC)J Ha OO'b)JBJTeHHe HOBblX nepeMeHHbIX, .llIDKe eCJTH MO'lKHO o6oITTllcb H 6e3 HHX. HanpHMep, B TOM npoCTOM cu.eHapm1 Mbl ononHe MornH 6hl HcnoJTb30BaTb ocero O.llHY nepeMeHHyIO -
cqeTtJHK U.HKJia. 0.11.Haxo cu.e-
Hapuii 6y.lleT 3HaqHTeJlbHO qHTa6eJibHee, ecJIH onpe.D.emlTb HeCKOJlbKO nepeMeHHbTX. '.}ro CB)J3aHO c TeM, ~o C03JlaHHe HOBOro H.11.eHTHHKaTOpa HHTepnpeTaTOpy o6XOJlHTC)J JlOBOJlbHO ".llemeeo". HMeHa nepeMeHHbIX qyocTBHTeJibHbI K perucTpy 6yKB: HanpHMep, Smy_variable -
He TO 'lKe
caMoe, ~ $My_Variable HJIH SMY_VARIABLE. KpoMe Toro, HMeHa ecex nepeMeHHbIX JlOJl)f{HbI HaqHHaTbC)J co 3HaKa $-Tax HHTepnpeTaTopy 3HalfHTeJlbHO nerqe "nOH)JTb 11 H OTJIHqHTb HX, HanpHMep, B CTpOKax. CToHaqany TIO JlOBOJlbHO CHJlbHO pa3JJ.pIDKaeT, HO UOTOM npHBbIKSelllb (H .D.IDKe aBTOMaTHqecKH HaqffHaelllb IlHCaTb 11.D.OJIJiapbl" nepe.ll HMeHaMH nepeMeHHblX B nporpaMMax Ha Jlpyrnx !13bIKax). SHHMAHHEI
B 0¢>111~lll8JlbHOM AOK)'MeHTS~M CK838HO, 4TO lllMR nepeMeHHOM MO>KeT COCTORTb He TOilbKO 1113 IlSTMHCKMX 6yKB "' ~111¢>p, HO TSK>Ke Ill 1113 Ill06b1X CMMBOilOB, KOA KOTOpblX crapwe 127, B 48CTHOCTM, "' 1113 "pyCCK111x'' 6yKB! 0AHSKO Mbl KSTerop1114eCKM He coeeryeM BSM np111MeHRTb KlllPllllllllll~ a MMeHax nepeMeHHblX XOTR 6bl 1113-38 Toro, 4TO B pa3HblX KOAMpoBKSX ee 6yKBbl MMelOT p83Illll4Hble KOAbl.
l./acm& II. OcHoe&1 R3b1Ka PHP
128
Kom1tpoeaHMe nepeMeHHblX TiepeMeHH&Ie B PHP -
oco6&1e 061.eKTbl, KOTop&1e MOI)'T co)lep:>KaT& B 6yKB8JlbHOM CM&1cne
see, 'ITO yro)lHO. Ecnu B nporpaMMe 'ITO-TO xpaHHTCJI, TO OHO seer.Ila xpaHHTCJI e nepeMeHHoR (HCKJIJO'feHHe -
KOHCTaHTa, KOTOpaH, Bnpo'leM, MO)l(eT CO.!lep)l(aTb TOnbKO 'IHCJIO, C1'p0-
Ky, a Ha'fHH~ c PHP 7 cymecTByeT -
Maccue). TaKoro noHJ1THJ1, KaK yKa.JaTen& (KaK e
C),
e JIJbIKe He
npH npHCB8HB8HHH nepeMeHHaJI B 6onbWHHCTBe cnyqaes KOnHpyeTCJI 0.!lHH
B O.!lHH, KaKYJO 6&1 CJIO)l(HyK> CTJlYKTYPY OHa HH HMena. E.!lHHCTBeHHoe HCKJIJO'feHHe HJ :noro npasHJia -
KOilHpOB8HHe nepeMeHHOH, CCbUlaJOII.leHcJI Ha 06'beKT HJIH MacCHB: B 3TOM
cny-
qae o6"heKT OCTaeTCJI B e)lHHCTBeHHOM 3K3eMnJIHpe, KOOHpyeTCJI JIHWb CCbUIKa Ha Hero ...
nPHME'IAHHE
06b8Km -
noHATMe o6aeKTHo-opMeHTMpoaaHHOro nporpaMMMpoaaHMA. 0Ho o6o3Ha'laeT nepeMeHHytO, xpaHAIJ.IYIO COBOKynHOCTb CSOMCTB HeKOTopolll CYIJ.IHOCTM, M KOA, pa60Tat01J.1MM c 3TMMM caolllcraaMM. npo o6aeKTbl Mb1 noroaopMM a IV M V Ke roeopunoc&, e PHP He HY:>KHO HH onuc&1eaT& nepeMeHH&Ie JIBHO, HH yKa.JbIBaTb ux THO. 11HrepnpeTaTOp see 3TO )lenaeT caM. 0.!lHaKO HHOr)la OH MO)l(eT OWH6aTbCJI (uanpHMep, eCJIH B TeKCTOBOR CTpoKe Ha caMOM .rteJie Ja,!laHO )leCJITH'IHOe 'IHCno ), TI03TOMY H3pe.!lKa noJIBnJleTCJI Heo6xO)lHMOCTb JIBHO YKa.JbIB8Tb, KaKoA :>Ke THO HMeeT TO HJIH HHOe BblpIDKeHHe. HeMHoro 'lame BOJHHKaeT 001'pe6HOCTb e&monHHTb p8.l.llH'IHb1e )leRcTBHJI e JaBHCHMOCTH OT THOa nepeMeHHOH (HanpHMep, nepe)laHHOH B napaMel'pax YHKUHH) npJIMO BO epeMJI BblnonHeHHJI nporpaMMbl. B 3TOH CBJl3H )laeaRTe IlOCMOTJlHM, KaKHe )Ke THIThl )laHHblX OOHHMaeT PHP.
TMnb1 nepeM&HHblX PHP ueoocpe.!lCTBeHHO OO.!lJtep:>KHeaeT Hecxoni.Ko THnoe nepeMeHHLIX, KOTop&1e Mbl oepe'IHCnHM H KOpoTKO OIIHWeM. 3a6eraH Biiepe)l JaMeTHM, 'ITO THil nepeMeHHOit MO:>KHO YJHaTb npH OOMOII.lH Bbl30Ba YHKUHH gettype ($variable)' KOTOpaSI npuMeT 3Ha'leHHe, paeHoe HMeHH THOa B CTJlOKOBOM ope.rtCTllBJieHHH.
Integer ("'enoe 'IMcno) Uenoe 'IHcno co JHaKoM, pa3Mep KOToporo JaBHCHT OT pa3pJl.ltHOCTH PHP. B 32-6HTHOM eapHaHre uenoe 'IHCno. MO:>KeT npHHHMaTb 3Ha'leHHe OT -2 147 483 648 .!lO 2 147 483 647. B
64-6HTHOM OT
-9 223
372 036 854 775 807 .!lO
9 223
372 036 854 775 807. BbIJICHHTb,
MaKCHManLHoe 3Ha'leHHe .!lnJI uenoro 'IHcna e PHP Mo>KHo, o6paTHBWHCb K npe.!lonpe.!leJieHHOH KOHCTaHTe PHP_INT_MAX.
B Ka'leCTBe anbTepHaTHBbl MO>KHO BOCilOnb30BaTbCJI KOHCTaHTOH PHP_INT_SIZE, KOTOpaH coo6mHT, CKOJibKO 6aitT OTBO)lHTCJI IlO.!l QeJioe 'IHCno. B 32-6HTHOit eepcHH KOHCTaHTa opHHH-
129
rnaea 6. nepeiweHHb1e, KOHcmaHmbl, munb1 daHHblX
MaeT 3Ha'leHHe 4. 4 6aHTa Ha 8 6HT B KIDK,llOM KaK pa3 .aaeT 32 6Hra, KOTOpble y.ao6HO o6pa6aTblBaTb 32-pa.Jp.11.!lHbIM npoueccopoM. ,LLn.11 64-paJpR.!lHOfi eepcHH PHP KOHCTaHTa npHHHMaeT JHa'leHHe 8. npH Bb!XO)le Ja JlHaTiaJOH UeJJOe 'IHCJIO aBTOMaTH'leCKH npeo6paJyeTC.ll B 'IHCJIO THTia double.
double (~llt,8CTB8HH08
'tMCno)
llHCJia c TIJlaBIDOIUeH TO'IKOfi HMefOT .aee opMbl JaTIHCH. 06bI'IHa.ll opMa coena.aaeT c npHH.llTOfi e apuMeTHKe, HanpHMep, 346.1256. 3KcnoHe111iUW1b1um opMa noJBOJIReT npe.acrnBHTh 'IHCJia B BH,!le npOHJBe.neHH.11 MaHTHCCbl 3.461256 H COOTBeTCTBYfOIUei:f CTeneHH 'IHCJia 102• ,lLn.11 uup MeH&We HyJI.11 cTenettb 'IHCJia IO .11en.11eTc.11 OTPHUaTeJI&Hoi:f. True, 'IHcno 0.00012 B 3KcnoHeHUHaJJbHOH opMe MO)f(eT 6hITh JanHcatto xaic I .2x I0 4 (puc. 6.1).
346.1256
0.00012
PMC.
'
H H
3.461256
1.2
x
x
102
10-4
H H
3.461256e+2
1.2e-4
6.1. npe.QCTSBlleHMe 4Mcna C n11aeato~BM T04KOM
B KOMTihfOTepHblX nporpaMMax HeT BOJMO)f(HOCTH HCTIOJlb30BaTb Cl-fMBOJlhl eepxHero perHCTpa, noJTOMY KOHCTPyKUHfO x I0 JaTIHCblBafOT e BH,!le CHMBOJia e, nocne xoroporo YKaJ&IeaeTC.11 3Ha'leHHe CTeTieHH. OepeMeHHble Sx H Sy COBepweHHO paBHOJHa'IHbl.
0.00012; l.2e-4; 346.1256; 3.461256e+2;
HMeeTCR HeCKOJibKO eapHaHTOB peanuJaUHH 'IHCen c nnaecuoiueif TO'IKofi, pa.3JIH4afOIUHXCR KOJIH'lecTBOM 6afiTOB, OTBOJlHMhIX no.n 4HCJIO, H B03MO)f(Hb!MH .nuana.JOHaMH JHa'leHHfi. B pattHHX eepcHRx .11Jb1Koe MO)l(HO 6&mo .11etto yKa.JbIBaTh rnn lfHCJia c nnaeaioiuei.t T04Kol1. Mo.nenu Single (S), Extended (E), Double (D) ncnon&JyfOT 4, I0 H 8 6aifToe cooTBeTCTBeHHO. CHMBOJI E, pacwupoBbIBIDOW,Hnc.11 e coepeMeHHblX peanHJaUHRX KaK 3KcnoHeHTa (oT attrn. exponent), 'lacro ucnonb30BaJJC.ll JlJIR Bbl6opa pacwnpettHon Mo.nenu (Extended), B TO epewr KaK Bb16op CHMBOJia D TI03BOJUIJI J~eikTBOBaTh lfHCJia JlBOHHOR TO'IHOCTH (Double). Orc)'TCTBHe )Ke cnMoonoo D H E n0Joo11.11no Ja.aei:fcTeoeaTb 'IHCJia, ucnoJib3yJOIUHe Mo.nenn Single. Taicoil wHpoxnn e&16op 6&IJ1 o6ycnoenett TeM, 'ITO npoueccop&1 qacTo He HMeJin annapaTHOro conpoueccopa, o6ecne4uocuoiuero onepauuu c nnaecuoiueil TO'IKOil. noJToMy Ha ypoeHe npoueccopa 6bmH .nocrynH&I TOJibKO onepauHH c uenhIMH 'IHCJiaMH, onepauHH )Ke c nnaBafOIUeH TO'IKOfi JlOJl)f(Hbl 6bIJIH o6ecnelfHTb pa.3pa60T'IHKH KOMTIHJISITOpa RJbIKa. B HaCTO.llIUHH MOMeHT TIO.UaBlllfOIUee 60JihlllHHCTBO KOMTihfOTepoo cua6)f(aeTC.ll conpoueccopoM. HcKJifOtteHHe cocTaBJI.11fOT JIHlllh ue6onbllloe KOJIH'lecrno Mttoronpm.leccopHbIX cynep3BM, HCTIOJih3)10IUHX RISC-apxHTexrypy. Taxoe nono)f(eHHe .nen np1rneno K TOMY· 'ITO peanHJaUH.11 'IHcna c nnaeaiomen TO'IKOH Ja'laCTyro onpe.nem1eTCR conpoueccopoM,
130
l./acm&
II. OcHoe&1 R3b/Ka PHP
KoTOpbdl peannJyeT Mo.nenb Double, T. e. no.n qncno c nnaBaJOmeA rnqKoA OTBOJ:IHTC.R
8 6aAToB,
qrn o6ecneqnsaeT .nnanaJoH 3HatJeHHA OT
±2.23xl0- 308
±1.79xl0 308 •
.no
TeM He
MeHee, BO Bcex COBpeMeHHbfX S!3bIKax nporpaMMHpOBaHHS! .z:tnS! 3KCflOHeHL{HaJibHOA $opMbl
E.
:mnncn ncnonb3yeTc11 CHMBon
flpH BbfXO,l],e 3a ,lJ,OnyCTHMblA ,l),Hana30H BMeCTO 'IHCna BblBO,lJ,HTCS! KOHCTaHTa INF, CHMBOJIH3HPYJOlllaSI 6ecKOHeqHOCTb. Jho6bre onepauHH c TaICHM qffCJIOM OilS!Tb B03BpamaK>T INF.
KpoMe KOHCTaHTbI INF B PHP peanHJOBaHa KOHCTaHTa NAN, KOTopa11 o6oJHaqaeT HeJJ,onycrnMoe qHCJIO:
B
npnse.neHHOM Bblllle npnMepe H3BJieKaeTC.R KBaJJ,paTHbIA KOpeHb HJ
-1.
TaIC KaIC PHP He
co.nep:lKHT BCTpOeHHOA flOMep:lKKH KOMnneKCHbIX qHcen, 803BpamaeTCS! HeJJ,onycTHMoe qHCnosoe JHaqeHne NAN. TaIC >Ke, KaK H B cnYKe H Hynesble CHMBOnbI, 'ITO HHKaIC He flOBJIH.ReT Ha nporpa.MMY. liHblMH CJIOBa.MH, CTpOKH MO:lKHO
2
HCnOnb30BaTb ,lJ,n.R
XpaHeHH.R
6HHapHbIX )J,aHHbfX.
,lVIHHa CTpKH
orpanHqeHa
r6a.HT. 0.z:tHaICO 3TOT npe.nen pe)J,Ko )J,OCTHraeTC.R, T. K. K3.)J(.!J.LIB PHP-cKpHnt orpaHHqeH
B o6beMe noTpe6n.ReMoA IlaM.RTH .z:tHpeKTHBOA lirni t, KOTOpbIA no YMOJJ'laHHIO COCTaBJI.ReT
128 M6aAt.
TaK. qro snonHe peanbHO npoqHratb B O.IlHY CTpOKy co.ztep)l(HMoe uenoro MeTHqeCKHX onepau.nA ua.n nomqecxoA nepeMeHHOA oHa npeo6paJYeTCR B OObJ
PHP http://exmaple.com/ nopTan, noceR!jeHHbli1 PHP ru Tue, 17 Dec 2015 15:30:00 +0300
Bbl!lle.n PHP 7.0.1, KOMaH.lla pa3pa0oT'Yl-!KOB PHP cooOwaeT o BblXO~e sep~ PHP 7.0.1, B KOTOpOM ycTpaHeH PR.Il Ollll100K. BceM nonb30BaTenRM PHP 7.0 peKOMeHJIYeTCR OOHOBHTbCR ~o 3TO~ eepc~.
http://example . com/news/177 news/177 Tue, 17 Dec 2015 15:30:00 +0300
Bbllllen PHP 7.0.0 KoMaH.lla pa3pa0oT'Yl-IKOB PHP cooowaeT o BblXO~e sep~ PHP 7.0.0 . ~aHHbli1 BbDlYCK Ha'Yl-IHaeT HOBYJO cep1110 PHP ?. http://example.com/ news/178 news/178 Tue, 03 Dec 2015 22:30:00 +0300
Bbllllen PHP 5.6.16 KOMaH,lla pa3paOOT'll-1KOB PHP cooOwaeT 0 BblXO~e eep~ PHP 5.6.16, a KOTOpoM ycTpaHeH pR.Il 01m10oK . BceM nonb30BaTenRM PHP 5.6 peKOMeH,qyeTCR oOHOBHTbCR ~o 3TO~ sep~.
rnaea 46. XML
859
http://example.com/news/179 news/177 Tue, 26 Nov 2015 18:12:59 +0300
J!JblK paJMeTKH XML, Ta.K )l(e Ka.K H HTML, co,llep)ICHT OTKpbIBaJOIUHe H JaKpbIBaJOIUHe Tern, HanpHMep,
Bbllllen PHP 7.0.0 BffYTPH TeroB He ,llonycKaeTCSI HCnOJIDJOBaTb CHMBOJlbl H &, OHH .llO~Hbl 6bITb npeo6pa30Ba.Hbl e 6eJonacHbiit cj>opMaT H &. B TOM cnyqae, ecnu co.u.ep)ICHMOe Tera .llOIDKHO xpaHHTbCSI 6e3 npeo6pa30Ba.HHR, B XML npe,llyCMOTJ)eHa ceKUHSI CDATA ( COKp. OT
character data),
B KOTOpOM MO:>K.HO xpaHHTb mo6yio nocne.u.oeaTeJibHOCTb, BKJl10t:faSI CHMBO-
Jlbl H &.
JDOObJe TeI'J1 ]]> IlpH 3TOM CllM Ter MO)ICeT CO,llep)ICaTb aTJ)H6)'Tbl, HanpHMep, url H type B Tere :
0rJIHt:flfreJibHOR oco6eHHOCThIO
XML SIBJISleTCSI TOT
a.KT, 'ITO B HeM He cymecTeyeT HKCH-
pOBaHHOro Ha6opa Teroe, Ka.IC B HTML. IlOJibJOBaTeJIH MOryr BBO.U.HTb CBOH rem. RSS-KaHaJI, npe,llcTaeneHHbIR e JIHCTHHre
46.1,
o6bNHO no,llKJIIOt:faJOT K HTML-CTJ'aHHUe
e pa3,llene head npu noMOlUH Tera :
RSS-KaHan Hat:fHHaeTCSI c JaronoeKa , CCbUIKH Ha nopTaJI-HCTOt:fHHK , onucauue
, JI3b1Ka H ,llaTbI nocne,llHero o6uoBJieHHSI . Tern co.u.ep)ICaT JaronoeoK HoeocTH , KpaTKoe onHcauHe , CCbUIK)' Ha ,lleTaJibHYJO CTJ'aHHUY , HHOr,lla YHHKaJibHblit H,llCHTHHKarop HOBOCTH B npe.u.enax nopYana-HCTOt:fHHKa , ,llaT)' ny6JIHKaUHH Jf, 803MO)((ffO, CCbUIK)' Ha Me,llnacpa.Hn e Tere . CocTaBHbie
ArperaTOpbl HOBOCTeA, HCnOJIDJyeMbie noceTHTeJIJIMH HJIH .u.pyrnMH nopYaJiaMH, o6paIUaJOTCJI K RSS-neare H npoeepJIIOT HaJIH"IHe o6Hoeneanit EcnH c MOMeHTa nocne.u.Hero o6uoeneHHJI 6bUIH ony6JIHKOBa.Hbl CBe)ICHe HOBOCTH, arperarop npOH3BO.!J.HT pa360p XML-aR,1a, H3BJICKaeT .u.onoJIHHTeJibHbie HOBOCTH H CHrHaJIH3HpyeT 0 HHX nOJib30BaTeJIIO.
l-ITeHMe XML-aina nonyquTb .u.ocryn K co,llep)ICHMOMY XML-aRna e PHP-cKpnme npome ecero qepe3 KJiacc
SimpleXMLElement. KoHCTJ'YKTOp KJiacca HMeeT cne.llYJOIUHA CHHTaKCHC:
final public SimpleXMLElemant: :_construct( String $data [ I
= 0 [I bool $data_is_url • false [, int $optiC118 string $ns
=
1111
[I
bool $is_prefix • false ]] ) ) ) 11epe3 napaMeTp $data nepeAaeTCJI coAep)l(HMoe XML-ct>aIDia HJIH
URL K HeMy, ecnn napaXML-
MeTp $data_is_url YCTaHOBJieH B true. $options n0380J]jleT 33,llan. napaMCTphl AOKYMCHTa, qacn. HJ KOTOphlX npeACTaBJieHa B Ta6JI.
46.1 (c noJIHhIM cnHCKOM MO:lfCHO 03Ha-
KOMHThCJI B O$HUHaJihHOH AOKYMCHTauHH). napaMeTp $ns npeAHaJHa'leH AJIJI 33,llaHHJI npoCTpaHCTBa HMeH H paccMaTpHBaeTCJI KaK npe$HKC, CCJIH 3Ha'leHHe napaMCTpa $is_prefix ycTaHoeneHo B true, HHa'le napaMeTp $ns paccMaTpHBaeTCJI KaK
URL.
Ta6nuqa 46.1. napaMempbt XML-OotKeT nOBblCHTb 6blCTPOA8lilCTBll1e np111no>KeH111A 6e3 BHeceHll1A ' 1113MeHeHll1lil B KOAe
i I
LIBXML- NOBLANKS LIBXML NOCDATA
! 06'be,QHH111Tb ceKL.1111111 CDATA K8K T8KCTOBble Y311bl
LIBXML NOEMPTYTAG
i'
! nyCTble Y311b1 YA811AIOTCA
'
Pa3BOP8"1111B8Tb nyCTble T81l1 (Hanp111Mep,
B
)
B o6'beKTe SimpleXMLElement Tern XML-tf:>aIDla npe,ncTaeneHhl e en,ne ceoRcTB. B JIHCTHH46.2 npneo,nHTCJI npHMep 'ITeHHJI paHee onpeAeneHHOro ct>anna rss.xml.
re
I11McTMHr
46.2. "fTeHMe
XML"'4>aflna. CS»&Mn read.php
yH.D.aMema.JibHbIM mrrepHeT-j(3bIKOM .llJUI npe.IJ.cTilB.TleHIDI
.naHHbIX. HecMOTpJI Ha TO, 'ITO OH He 3aMeHHJI co6oH HTML, OH ocTaeTCs O.llHHM H3 OCHOBHbIX cnoco6oe Me)((CaHTOBOTO H Me)((cepBHCHOfO o6MeHa.
B
TeKyw.eit rnaee Mbl paccMoTpeJIH KJiacc SirnpleXMLElernent, n03BOJij(IOllJ.HH KaK ocymecTB-
JUITb pa36op XML-cj>aunoe, TaK H no,ZJ.fOTilB.TIHBaTb co6cmeHHb1e XML-cj>aJiJibl.
rllABA
47
3arpy3Ka ct>aMnoe Ha cepeep
JlHCTHHnt .aaHHOii rnae1.1 MOlKHO HaiirH e no.aKaTa.1ore upload.
HHor.D.a 6b1saeT npocTO Heo6xo.D.HMO pa3pemHTb nonbJOBaTemo He TOnbKO 3anonHHTb TeKCTOBbie non.11 «tiopMbl H ycTaHOBHTb COOTBeTCTB}'IOl..l{He «tinaJKKH H Pa.D.HOKHOnKH, HO TaJOKe H yKaJaTh HecKonbKO «tiannos, KOTOpb1e 6y.!J.YT snocne.D.CTBHH 3arp)')KeHb1 c KoMnblorepa nonbJosaTen.11 Ha cepsep. ,[{JJ.11 :noro B .113bIKe HTML n npoToKone HTTP npe.!J.YCMOYpeHbI cneunanbHb1e cpe.D.cTBa.
nPHME'IAHHE 4T06bl He np111MeHATb ABYCMblcneHHOM TepMlllHOflOrlltllt, Mbl 6yAeM 111cnonb30BSTb cnoao "3aKa48Tb" AflA 0603H84eHlllA 3arpy3Kllt ¢>alilna KnllteHTa H8 cepeep Ill TepMlllH "CKS48Tb" AflA "111fllOCTpa1..1111111 o6paTHoro npo1..1ecca (c cepeepa - K/1UeHmy) 1.
Mb1 y)((e paccMaYpHBaJIH MexaHH3M, KOTOpbm npnMeH.11trrc.11 npn JaKa'IKe «tirutnos, e 2110ee 3. Bbl, B03MO)((HO, noMHHTe, 'ITO OH Bbirn.11.D.en He oqeub-TO npnsneKaTeJibHO. Ha Haw Blrn.11.D., 3aKa'IKa «tianJIOB H 80061..l{e pa6oTa C multipart-MeTO,!J,OM nepe,!J,a'IH «tiopMbl - ,!J,OBOJlbHO HeYpHBHaJlbHbie Ja.D.a'IH. 0.D.HaKO cnewuM o6pa.D.OBaTb: B PHP see ::no .D.aBHO peaJIHJoeaHo n ornIDKeHo. Ho 060 sceM no nop.11.D.Ky.
1 PyccKHll lllblK, HJHa'flUlbHO o6na.naiomHll raraHTCKoll ceo6o.11oll e eb16ope c.i1oea, nocrollHHO p81BHBaerc11. To, qTO KaJa'IOCb HenpHeMJJCMblM eqepa, cerO.IIHll CTaHOBHTCll HOpMOH, H Hao6opor. lie'.JycJJOBHO, y ruo6oro o6paTHewero BHHMaHHe Ha JTH crpoKH npH npo'fTeHHH CJ!OB "38KaqaTb" H "cKa'faTb" epll,IJ. JlH BOJHHKHYT accoUHaUHH c 6pHraJIOH MycKynHCThlX a.nMHHHcrparopoe, npH.!1810lllHX nepe.11aeaeMbIM no CeTH ctiallnaM Heo6XOID!M)'IO KHHeTH1 cn1>1wanu o B03MOJKHOCTllX JavaScript, c noMOIUblO KOToporo MOJKHO C03,I(aBaTb HHTepaKTHBHble CTpaHHUbl, pearupyiow;ue Ha ,neiicTBHJI nonb30BaTeJIH B peanbHOM BpeMeHH. HanpHMep, MOJICHO HanucaTb KO.I( Ha JavaScript, KOTOpbIB 3anycKaeTCll, Kor,na nonb30BaTenb HaJKHMaeT KaKyio-Hu6y,n1> KHOnKY B opMe Ha CTpaHHU:e HJIH 880.I(HT TeKCT B o,nHO H3 TeKCTOBbIX nonen. npHMeHeHHe JavaScript He OrpaHuquoaeTCll ynoMllffYTbIMH B03MOJICHOCTllMH. B qacTHOCTH, YMenhlH nporpaMMHCT MOJICeT C03,naBaTb CTpaHHU:bl, KOTOpble 6y.zzyT aBTOMaTHqecKH opMHpOBaTbCll u OTCblJiaTbCll Ha cepoep opMbl 6e3 oe,noMa nonbJOBaTeJIH. B npnHu:une, o JTOM HeT HHKaICOro "KpHMHHaJia": oe,nb see OTnpaBJieHHble .I(aHHbie creHepupOBaHbl 3TOH JKe CTpaHHU:eH. 1-ITo JKe nonyqHTCll, ecnu pa3pewHTb rery HMeTb napaMeTp no )'MOnqaHHIO? npe,nnonOJKHM, nonb30BaTenb XpaHHT BCe CBOH naponH B 11 3aCeKpeqeHHOM 11 a.ffne C:\zionmainframe.codes. Tor.na B3JIOMIUHK naponen MOJKeT HanucaTb Ha JavaScript H BCTpoHTb B cTpaHuu:y nporpaMM)', KOTopu co3,naeT H ompaBJIHeT Ha "coon" cepoep opM)' He3aMeTHO ,nJill DOnb30BaTeJIH. npu 3TOM ,I(OCTaTOqHo, qro6bl B opMe npHCYTCTBOBaJIO e,nnHcTBeHHoe none JaKaqKu aAna c npocTaBJieHHbIM napaMeTpOM value="C: \ zionmainframe. codes". EcTeCTBeHHhlH 01>mo.n: o cnyqae ecnu 61>1 napaMeTp no yMonqaHHIO 6bln pa3peweH ,nJIH Tera 3aJCaqKH wa, TO nporpaMMHCT Ha JavaScript, 11 3aMaHHB 11 Ha CBOIO CTpaHHn:y nonb30BaTenll, Mor 61>1 HMeTb 803MOJICHOCTb CKODHpOBaTb JIJ060H aAJI c KOMnblOTepa KJIHeHTa. Tenepb Bbl noHHMaeTe, noqeM)' Ter He ,nonycKaeT ncnonb30BaHHll aTpH6YTa value? ..
noAQep>KKa 3aKa'tKM B PHP TaIC KaIC PHP cneu:uan1>HO pa3pa6aTblBaJICll KaIC ll3blK ,nJill Web-npnnoJKeHHH, TO, ecrecTBeHHO, OH 11)'MeeT" pa60TaTb KaIC c npHBbJ11HbIMH HaM, TaIC H c multipart-opMaMH. Eonee Toro, OH TIOOKe no.n,nepJKHBaeT 3aKa'IKY woo Ha cepoep.
npocTb1e MMeHa
nonei 3aKat.tKM
KaIC Mbl YJKe rooopHJIH, HHrepnpe-raropy cooepmeHHO see paoHo, B KaICOM opMare npuxoAAT ,naHHb1e H3 opMbl. OH yMeeT nx o6pa6aTblBaTb H "paccoobmaT1>" no nepeMeHHblM B JIJ06oM opMaTe. 0,nHaICO ,naHHble o,nHoro cnen:HaJibHOro nonll opMbl - a HMeHHO DOJIJI JaICaqKu - OH HHTepnpeTHpyeT oco6bIM o6pa30M. ,l(aoaATe DOCMOTpHM Ha npHMep cu:eHapm1 B nHCTHHre 4 7.1. OH BbIBO.nHT B 6paYJep multipart-opM)', a o HeH - none JaICa~ wa. nonpo6ynre 01>16paTb KaICoit-Hu6y,n1> aAn u HaJKaTb KHOnKy laK&'l&Th.
rnaea 47. 3aapy3KB cpaiinoe Ha cepeep
869
PHP aaTOMaTJ-Nec1G1 co3,naeT nepeMeHHblE! npH 3aKaqKe
ElblOepHTe KaKO~-HH0Y..llb ~aKn B ~pMe liK'r(e;