PHP 7 9785977537254


135 51

Russian Pages 1073 Year 2016

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
PHP7 в подлиннике. Часть1
1
3
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
Binder2.pdf
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
PHP7 в подлиннике. Часть 2
Глава 26. Обработка ошибок и исключения
Глава 27. Предопределенные классы PHP
Глава 28. Календарные классы PHP
Глава 29. Итераторы
Глава 30. Отражения
Глава 31. Работа с HTTP и WWW
Глава 32. Сетевые функции
Глава 33. Посылка писем через PHP
Глава 34. Управление сессиями
Глава 35. Расширения PHP
Глава 36. Фильтрация и проверка данных
Глава 37. Работа с СУБД MySQL
Глава 38. Работа с изображениями
Глава 39. Работа с сетью
Глава 40. Сервер memcached
Глава 41. Компоненты
Глава 42. Стандарты PSR
Глава 43. Документирование
Глава 44. Разработка собственного компонента
Глава 45. PHAR-архивы
Глава 46. XML
Глава 47. Загрузка файлов на сервер
Глава 48. Использование перенаправлений
Глава 49. Перехват выходного потока
Глава 50. Код и шаблон страницы
Глава 51. AJAX
Глава 52. Протокол SSH
Глава 53. Виртуальные машины
Глава 54. Система контроля версий Git
Глава 55. Web-сервер nginx
Глава 56. PHP-FPM
Глава 57. Администрирование MySQL
Приложение. HTTP -коды
Предметный указатель
Recommend Papers

PHP 7
 9785977537254

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

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("");

Hcxo.iuu.1A reKCT MO)l(ffO OTKOMDHJIHposaTL n noMecTHTh e xaTaJior c CGI-cueHapHRMH Ha cepeepe. 06wmo cTapaJOTCJI ece cueHapnn xpaHHTI> e OJJ.HOM Mecre - e xaTaJiore cgi-bin, y KOTOpOro HMeeTCJI pa.3pe1DeHHe Ha BblJIOJlHeHHe BCeX $lltJIOB BffYTPH Hero. Opae)J.a, 3TO npaeHJio He JIBJlJleTCJI 00"38TeJlbHblM - KOHe'IHO lKe, MO'.llCHO pa3MecTHTb aHJlbl cueHapHJI r)J.e ,eyme yro.ll.Ho (He Ja6bl8 npocTIUIHTI> COOTBeTCTB}'IOUUfe npaea Ha xaTaJior e HacTPQiiKax cepeepa). Ha Ham BJrnsm, norH'rno xpaHHTL aJiJII>I cueHapnee TilM, rJJ.e no mm6onee eaM YJJ.06Ho, a He nonI>JOsaTI>CJI 06m,enpHHJ1TLIMH IDTilMilaMH. Tenep1> aa6epeM B aJJ.pecHoH CTpOKe 6payJepa:

http://www.myhost.com/cgi-bln/script.cgi MI>I nonyqnM Hamy HTML-cTpaHnizy. 3aMen.re, 'ITO npn Ha'.llCaTHH KHODKH Reload (a TaIOKe npn IlOBTOpHOM nocem,eHHH CTpaHHUbl) 6paYJep nepeJarPYJHT CTpaHHizy uenHKOM, a ne 803bMeT ee KODHJO HJ ceoero K31Da (3TO MO:llCHO BH}{eTI. no OOCTOJIHHO HJMemnomeMyCJI CnytlaHHOMY 'IHCny HJIH no JlaMllO'IKaM MO,ZJ;eMa). Mb1 )J,OOHJIHCI> TaKOro pe3yn1>rara 6naro,ZJ;aPJ1 3ar'OJIOBKY Pragma: no-cache

Bb1BOA 6MHapHoro ct>allna )J.aeaJire renepb IlOCMOTpHM, 'ITO ffY:llCHO HJMeHHTL 8 HameM cueHapHH, 'ITOObl ero BbIBO,ZJ; npe)J,CTaBfil(Jl co6oit c TO'IKH 3peHIDI 6paYJepa He HTML-JJ.OK)'MeHT, a pHCyHOK. nycTI> HaM ffY:llCeH cueHapHA, xorop1>m 6I>I nepe.naean nonI>JOBaTemo GIF-pncyaoK (HanpHMep, BI>I6HpaeMhlit CnytlaHHblM o6paJOM H3 HeKOTOporo CilHCKa). )J.enaeTCJI 3TO coeepmeHHO aHaJIOrH'lHO: BbIBO.lJ,HM JaroJIOBOK Content-type: image/gif

78

4acmb I. OcHoebl Web-npoapaMMupoeaHUR

3areM KOnHpyeM 0.11.HH B 0.11.HH HY)f(Hblif HaM GIF-cpaiirr B eTaH,llapTHbrn noToK BblBO.lla (rryqllle Beero - YHKUlileA fwrite, T. K. HHaqe Moryr B03HHKHyTb npo6rreMbl e 11 6HHapHOCTb}() 11 GIF-pHeyHKa). Tenepb MO)f(HO lilenoJib30BaTb JTOT eueHapuil ,lla)f(e B TaKoM KOHTeKeTe: •••

KaKO~-To

TeKcT CTPaHl1Ll.bl •••

• • . npOJIOJJJl(eH11e cTpa.Hll!Ll.bl •.•

B pe3yJibTare TaKHX ,lleiieTBHH B Hawy eTPaHHUY 6y.1leT no,lleTaBJU1TbCJ1 KWK.llhlH pa3 eJI)"taltHoe mo6pa)f(eHlile, reHepHpyeMoe eueHapHeM. Pa.JyMeeTeJI, qToobI li!36e)f(aTb HenpHuHocreif e K3llIHpoBaHlileM, KOTOpoe oco6eHHO HHTeHeHBHO npHMeHJleTeJI 6pay3epaMH no OTHOWeHH}() K KapTHHKaM, Mbl ,llOJDKHbl ero JanpeTHTb BblBO,llOM eOOTBeTCTB)'lOmero JaroJIOBKa. HMeHHO TaK yeTPoeHbI rpaqiw-ieeKHe eqeTqHKH, eTOJib paenpoeTPaHeHHbie B HHTepHere. Ellle pa.3 o6pamaeM Bawe BHHMaHHe Ha TaKoif MoMeuT: CGI-eueHapHH Moryr uenOJibJOBaTbeJ1 He TOJibKO .llJIJI BbIBO.lla HTML-uuqiopMaUHH, HO H ,1lJIJI n~oro .llpyroro ee THna - HaqHHaJl e rpa$HKH " JaKaHqm1as JBYKOBblMH MIDI-atirraMH. THn .llOKYMeHTa Ja,naeTCs e e,llHHeT&eHHOM Meere 3arOJIOBKe Content-type .. He Ja6bIBaiiTe ,llOOaBJIJITb 3TOT JarOJIOBOK, B npOTHBHOM erryqae OOJlb30BaTemo 6y.1leT OT06pa)f(eHa eTaH,D;aPTHaJl eTPaHHUa eepBepa e eoo6meHHeM 0 500-A OIUH6Ke, H3 KOTOpoif OH BpJI.ll JIH qTo noAMeT. 3AME'IAHHE ~cxoAHblH KOA cKpHnTa, 0Tot5pa)l(a10~ero GIF-¢al1n, Mbl 3AeCb He npHBOAHM H3·3a o6HnHR B HeM H3nHWHHX TeXHH4eCKlllX AeTaneH. Bbl MO)l(eTe HaITTH ero B apxHee c lllCXOAHblMlll KOASMlll H3 ASHHOH KHHrH, AOcrynHOM B ~HTepHeTe. al1n Ha3blB8eTCR c/gif.c.

CTpo6rreMa npueMa napaMeTPOB, Ja,llaHHbIX noJib3oBaTeJieM (e ToqKH 3peHHJI eueuapHJI - Bee paeuo, qepe3 PMY HJIH BpyqHyio), HeeKOJibKO erro)Kffee. MbI ~e qaeTnqHo JaTParHBaJIH ee H 3HaeM, qro OCHOBHaJI HHopMaUHJI npHXO,llHT qepe3 3arOJIOBKH, a TaK)f(e (npH HenOJib30BaHHH MeTO,lla POST) noerre Beex 3aroJIOBKOB. PaeeMQTPHM 3TH BOilpocbl no.npo6Hee.'

nepeM8HHbl8 OKpy>KeHMJI Henocpe,lleTBeHuo nepe.n JanyeKoM eueHapHJI eepBep nepe.naeT eMY ueKHe nepeMeHHble oKpyxeHWL e HH$opMauHeif. B onpe.neneHHbIX nepeMeHHblX eo,nep)f(aTeJI ueKoTOpb1e JafOJIOBKH, HO, KaK ~e fOBOpHJIOeb, He Bee (IlOJI)"tHTb BCe 3afOJIOBKH HeJib3JI) . .[lanee npHee.neH nepeqeHb Hali!6onee Ba)f(HblX nepeMeHHbIX OKp~eHHJI (60JibWHHeTBO H3 Hli!X Mbl y:llCe paeeMaTpHBaJIH, HO eeifqae 6y.neT noJie3HO nOBTOpHTbeJl H eHeTeMaTH3lilpoBaTb Beeb Haw enHCOK HMeHHO e TOqKH 3pemtJI nporpaMMHCTa).

D HTTP ACCEPT B noii nepeMeHuoA nepeqHeneHbI Bee (Bo BCJIKOM enyqae, TaK roeopHTCJ1 B .noKyMeHTaUHH) MIME-THnbl .naHHbIX, KOTOpble Moryr 6bITb BocnpHHJITbl 6pay3epoM. KaK MbI ~e JaMeqanH, coepeMeHHble 6pay3epb1 qaeTeHbKO 11eHJ1TCJ1 H nepe.naJOT eTPoKy 'IWOl.U)10, 'ITO OHH JIK06bI OOHHMWOT JI}()60if THn.

*I*, 03Ha-

0 HTTP REFERER 3a,naeT HMJI .noKyMeHTa, B KOTopoM Haxo.nHTeJI opMa, JanycTHBIUaJI CGI-eueHapHif. 31)' nepeMeHH)'lO OKpy:llCeHHJI MO:llCHO 3a,neAeTBOBaTb, uanpnMep, ,1lJIJI TOro, •no6bI OTCJie)f(H-

rnaea 3. CGI u3Hympu

79

eaTb nepeMellleHHe no.llh3oeaTeJIJI no eaweM}' caAzy (a noroM, HanpHMep, rae-Hu6y.zu, pacneqaTbIBaTb crarncnncy caMblX nonyJIJ1pm.IX Mapmpyroe).

LJ

HTTP USER AGENT

~eHTHcl>HUHpye-r 6paYJep noJib30BaTeJIJI.

KIUC npaBHJio, ecnu e AaHHoit nepeMeHHoA OKp)')KeHHJI npucyrcTByeT DOACTJ>OKa MSIE, TO 3TO Internet Explorer, B npOTHBHOM cnyqae, ecJIH e HaJiffqHH JIHIIlh cnoeo Mozilla, - Firefox, Chrome HJIH apyroA 6pay3ep.

lJ

HTTP HOST

~oMeHHoe HMJI Wekepeepa, Ha KOTOpoM 3811YCTHJIC.11 cueHapeit. Toqffee, TO, 11To 6b1Jlo

nepeaaHo B 38fOJIOBKe Host npOTOKOJia HlTP 1.1. 3zy nepeMeHH)'Kl OKp)')KeHHJI AOBOJlhHO yao6Ho ucnoJlh3oeaTb, HanpeMep, AJlJI reHepaUHH nonHoro nyru, Korop1>1A 1'pcljyeTc.11 e JafOJIOBKe Location, 11To61>1 ee npHB.1131>1BaTbC.ll K KOHKpeTHOMy cepeepy (Boo6llle roeop.11, qeM Meei.me cueHapHA 38,ZleACTByeT "3annrryK>" B Hero HHcl>PM8.llHIO 06 HMeHH cepeepa, Ha KOTOpoM OH 33I1)'1QeH, TeM nyqwe - B H,ZJ:eane ee He AOIDKHO 61>1Th BOBce). Ecnu o6pallleHHe npoHlBO.DJl.llOCh K eecTaH,llapraoMy, OTJiuqHoMy OT 80-ro, nopzy (HanpuMep, 81 HJlH 8080), TO HTTP_HOST coaep:lKHT T8IOICe R HOMep nopTa, YKaJaHHhIA nocne HMeHH XOCTa H )lBoeT011HJI: XOCT: nopT.

lJ

SERVER PORT

IlopT cepeepa (o6Mt1Ho 80), K KOTOpoMy 06paTHJic.11 6pay3ep noni.JoeareJI.11. TWOKe Mo)l(eT npHBJieK&TbC.11 .lJ)l.ll reHepaumt napaMeTpa 38fOJIOBKa Location.

lJ

REMOTE ADDR

3Ta nepeMeHHa.11 OKp)')KeHH.11 38,ZlaeT IP-aJq>ec (HJIH aoMeHHoe HMJ1) yJJia nonh3oearen.11, Ha KOTOpoM 6hlJI la.nyllleH 6pay3ep.

lJ

REMOTE PORT

IlopT, KOTopi.u:t JaICpeIImleTC.11 38 6paYJepoM nonh3oeaTeJI.11 eepa.

lJ

.lJ)l.ll

nonyqeHHJI OTBera cep-

SCRIPT NAME

BHJYIYaJI1>Hoe HMJI BhlilOJIH.1110l1leroc.11 cueHapH.11 (T. e. 11acTb URL nocne HMeHH cepeepa, HO ao CHMeona ?). 3zy nepeMeffHYJO OKp)')KeHHJI, Oil.llTb )l(e, 011effh yao6Ho 6paT1> Ha eoop)')KeHHe npH cl>opMHpoeamrn JaroJIOBKa Location npu "nepeaapecaUHH Ha ce6.11" (selfredirect), a TaJOKe npn npocTilBJleHHH 3HaqeHHJI a1'pH6yra action rera Ha CTJ>aHHQe, KOTopyio BhI,ZJ.aeT CQeHapHA npH 3811YCKe 6eJ napaMeTpoB (AJlJI TOfO 11To61>1 He npHBJl3hIBaTbC.ll K KOHKpeTIIOMy HMeHH CQeHapH.11).

lJ

REQUEST_METHOD

Meroa, Korophlit npHMeH.lleT nonh3oeareJlh npu nepeaa11e AaHHhlX (Mhl paccM&TJ>HeaeM TOJlhKO GET H POST, XOT.11 CYllleCTBYJOT H apyrne MeTOAhJ). Haao 3aMeTHTh, 11TO rpaMOTHO COCTaBJieHHhu:t cueHapHA AOJl)l(eH CaM onpeaeJI.llTh Ha OCHOBe 3TOit nepeMeHHOA, KaKOit MeTOA 38,ZleitcTByeT Il0Jlh30BaTeJih, H npHHHMaTb AaHHhle Hl COOTBeTCTBYJOUlero HCT011HHKa, a He pacC11HTblBa1D, qro nepeaaqa 6yae-r OCYlllecTBJI.llThC.11, HanpHMep, TOJlhKO MeTO)\OM POST. BnpoqeM, ace PHP-ctteHapuu TaK H yc1'pOeH1>1.

lJ

QUERY_STRING

napaMeTphl, KOTOp1>1e B URL YKaJaHhl nocne eonpocHTeJihHOfO 3HaKa. HanoMHHaeM, qro OHH AOC'I)'IlHhl KaK npH Meroae GET, TaK H npH MeTO,l\e POST (eCJIH B nocJie,l\HeM CJIY11ae OHH 6hlJIH onpe,l\eneHhl B 31'pH6yre action Tera ).

r:J CONTENT LENGTH

KonH11ecTeo 6aihoe ,naHHbIX, npHcnaHHbIX nonb3oeareneM. 3zy nepeMeHH)'IO Heo6xo.nnMO aHamnHpoeaTb, ecnH Bbl 3aHHMaeTeCb npHeMOM H o6pa60TKOA POST-cj>opMbI.

nepe.Qa'la napaMeTpoe MeTO.QOM GET TyT ece npocTO. Bee napaMeTpbl nepe.naioTc.11 eAHHOA CYpOKoA (a HMeHHO TO'IHO TaKott )Ke, KaKa.11 6bma 38,llaHa e URL nocne ?) e nepeMeHHoR QUERY_STRING. E.nnttcTBeHHa.11 npo6neMa - ece AaHHble nocrynu URL-KOJJ.HJ>OBaHHbIMH. TaK 11ro HaM noHaJJ.OOHTCll YHKUHll ,neKO.llHJ>OBaHH.11. Ho 3TO OTAeJibHWI TeMa, noKa MbI He 6y.neM ee KacaTbC.11. ,An.11 Toro 'ITOObI Y3HaTb 3Ha11eHHJ1 nonyqeHHbIX nepeMeHHbIX e C, HYJKHO eocnonb30BaTbc.11 yHKUHeH getenv (). B nHCTHHre 3 .2 npnee.neH npnMep cuettapH.11 Ha C, KOTOpblit 3TO 06ecne11neaeT.

#include II 8KJJl(lt.laeM #include II 8KJJl(lt.laeM

~YHKW1l1 ~YHKUl1!0

aaonalablBona getenv()

int main(void) { II TIOJIY'laeM 3Ha4e!fi1e nepeMeHHO~ 01cp:y?Kelfi1R REMOTE_ADDR char •remote_addr = getenv("REMOTE_ADDR"); II ... ~ewe QUERY_STRING char •query_string = getenv("QUERY_STRING"); II Tie4aTaeM saro.noaoK printf("Content-type: textlhtml\n\n"); II Tie4aTaeM noJ1 nonyqnM npnMepHo TaKoH .noKYMelIT:

3.opaBCTByHTe. Mbi sHaeM o Bae ace! Baw IP-anpec: 192.168.1.23 BoT napaMeTpb!, KOTOpble Bbl yicasa.Jllll: a=l&b=2

rn888

3. CGI UJHYfTIPU

81

nepe.qa'fa napaMeTpoe MeTOAOM POST B

OTJIH'iHe OT MeTO){a GET, 3){eCb IlapaMeTpbl nepe){aJOTCJI CQeHapHIO He qepe3 nepeMeHHbie

oKpyxHQHpyeM npeALl)zylIUlit npHMep TIUC, qTOObl OH npHHHMan POST-.AaHHble, a TIUOKe BblBOAHn H GET-HH«i>opMaQHJO, eCJIH OHa ~aHa (JIHCTHHr

3.3).

#include #include int main(void)

II

{

113BJJeKaeM 3Ha4eHl1R nepeMeHHblX oiq>y,y:eH11R

char *remote_addr

~

getenv("REMOTE_ADDR");

char *content_length = getenv("CONTENT_LENGTH"); char *query_string = getenv("QUERY_STRING");

II

BbNJ'1cnReM .ll.!U1HY ,naHHblX - nepeeo.Ill'IM CTPOKY e 411cno

int nurn_bytes = atoi(content_length);

II

BbuteJJReM e ceoOo.nHoi'I nawiTl1 OY'l>ep Hy,t:Horo pa3Mepa

char *data= (char *)malloc(num_bytes + 1);

II

41.1TaeM .naHHble H3 cTaH.napTHoro nOToKa eeo.na

fread(data, 1, num_bytes, stdin);

II II

~OOaBJJReM

ts·?f~~"' ~~~·~..,~ #.".~ '"1'.fk~'Z" -...~~:!::".. "';-t~·y;,,y:-;.:·:;t::,.

1., , :,..· , ~ J~·J
maeTCll Ha 3Ha'leHHe nepeMeHHOit OKp)')KeHHJI QUERY_STRING, KOTOpall pacnOJIOllCeHa B CHCTeMHOtt o6na.CTH naMJlTH, a 3Ha'IHT, JJ.OCT)'IIHa mo!l&KO O!IR 'lmeHUR. B TO )l(e BpeMll 4>YHKUHJI urlDecode ( l, KaK MbI )')Ke 3aMe11anu, noMem.aer pe3yJI1>TilT ceoeR pa6on1 e ry )l(e o6nacn DaMllTH, rJJ,e HaxOJJ.HTCll ee napaMerp, 'ITO H Bbl3bmaeT OWH6Ky. tfro6LI H36aBHThCll OT YK838HHOro HeJJ,OCTilTKa, Mbl H KOilHpyeM 3Ha'leHHe nepeMeHHOit OKp)')KeHIUI QUERY- STRING B o6JiacTh na.MJITH, JJ.OC1)'IIHOit cueHapHJO JJ.JUI 3aIJHCH - uanpuMep, e KaKoit-HH6yJJ.h 6y4>ep, a noToM )')Ke npeo6paJyeM ero. 1ITo n 6wo cJJ,eJiaHO e nocne.nHeM cueHapuu. HecKOJibKO o.nnoo6pa3HO H 3anyTaHHO, ue TaK nH? ,[\a, nollCanytt. Ho, KaK roeopHTCll, 11 3TO JJ.a)l(e xopowo, 'ITO noKa HaM nnoxo" -TeM 6on1>me 6yJJ.eT npH'IHH npe.nno11uraTh PHP JJ.PYrnM ll3bIKaM nporpaMMHpOBaHIDI (T. K. e PHP 3TH npo6neM1>1 H3llCHThI KaK KJiacc).

Cl>OpMbl .no CHX nop HJ Bcex IlOJieit $opMhl Mhl paccMa'I'pHB8JIH TOJlhKO TeKCTOBble IlOIDI H KHOilKH ompaBKH ( THna submit). ,naea.itre renepi. pacCMO'I'pHM, B KaJCOM BH.ZJ;e npHXO,IVIT AaHHhie OT Apyrnx :meMeHTOB 4>opMhl (a HX CyIIIecTByeT AOBOJihHO MHOro). Bee 3JieMeHThl $opMhl no HMeHa.M COOTBeTCTBYJOlllHX HM reroe AeIDITCJl Ha 'I'pH KaTeropHH:

Cl ; Cl ... ; Cl ... ... . KaJIC.Zlhlit HJ 3THX reroe, KOHequo, MO:>KeT HMeTh HMJl. Pauee }')Ke ynoMHH8Jloci., 'ITO napi.1 uwr=3HaKHO 6hlllo onpeAeJIJlTb CJI}11Ce6u1>1e 3JieMeHThI, KOTOpbie ue 6yJl)'T nocbmaThCJl cueHapmo. HanpHMep, B HX 'IHCJIO BXO,WIT KHOilKH (IlOATBep~eHHJl ompaBKH HJIH OObl'IHble, HCilOJlhJyeMble npn nporpaMMHpOBaHHH Ha JavaScript) H T. A.

11TaK. COJAa.ztHM $opMY:

. . . KaKYie-To nom1 ...

Go!

HecMO'I'p.11 Ha TO, 'ITO KHOnKa

$opMaJibHO .11BJI.11e-rc.11 noJieM BBOAa, ee AaHHbie ue 6y~T

nepeAaubI cueuapmo, nocKOJibKY y nee OTCyTcTByeT napa.Me'I'p name. ll~e ece :>Ke 6i.maeT YA06uo AaBaTh HMeHa TaKHM KHOnKaM. HanpHMep, AJI.11 roro 'IT06b1

onpeAeJIHTb, KaKHM o6paJOM 6i.m JanyllleH cueuapnit (HanpHMep, npocro ua6opoM ero

URL B 6payJepe).

Ha:>KaTHeM KHOilKH HJIH KaJC-TO eme

COJAa.ztHM cJieJJYK>IUYJO 4>oPMY=



I1ocne JanycKa raKoit $opM1>1 H Ha:>KaTH.11 e Heit KHonKH

Go!

cueHapmo cpeAH npoqffX napa-

MeTpOB 6yAeT nepeAaua CTpOKa submi t=Go ! • BepH)'BWHCb K npHMepy HJ npeAM.Zzymeit rnaBhl, Mbl Tenepb JierKO CMO)f{eM onpeAeJIHTb, 6i.m JIH cueuapHit BbIIlOJIHeH HJ q,opMbl HJIH :>Ke npocTbtM yKaJaHHeM ero

URL

(AJI.11 3Toro AOCTaTO'IHO npoauaJIHJHpoeaTh KOM~ C'I'pO-

KY cu.euapu11 H onpeAeJIHTh, npHcyTcTeye-r JIH e ueit a'I'pH6yT submit).

B npHHU.Hne, Hwcoeo -

see Tern, Ja HCKJDO'leuneM , c TO'IKH JpeHHJl cu.euapHJl BbffJI.llW!T OAH-

KllK OAHH, OHH reuepHpYJOT CTpOKH BH,ZJ;a

JaAaHHa.11 B a'I'pH6yTe name, a

3Ha'leHUe -

UWl =3Hd'leHUe,

rAe

=-

CTpOKa,

JIHOO TeKCT, eeeAeHHhlil IlOJlbJOBaTeJieM, JIH6o co-

Aep)f{HMOe a'I'pH6YTa value (uanpHMep, TaJC npoHCXOAHT y ueJaBHCHMhIX H JaBHCHMblX nepeKJTlO'laTeJieA, KOTOphle Mbl BCKOpe paCCMO'I'pHM).

87

rnasa 3. CG/ U3Hympu

Ter - pa3J1M'4H1::t1e nonfl aao.Qa C)'llleCTByeT MHoro p83HOBHJUiocrett 3roro rera. OTJIHtUlJOIQHXc.11 napaMeTpOM type. nepequcnuM Ha.H6onee ynoTpe6HTeJILHLie H3 HHX.

B

KB8,ZQJRTHLIX CKo6Kax CTllHeM )'K83LIBaTL He-

o6.111areJILHL1e napaMeTpLI, a TillOICe napaMeTpLI, OTCyrcTBHe KOTOpLIX HHOr,z:ta HMeeT CMLICJl (6y,z:teM C'IHTaTL, 'ITO napaMeTp name .llB.1UleTC.ll 00.lllaTeJlLHLlM, XOT.11 3TO H He TaK B CHJIY BLI· weH3JlOXCeHHLIX paCC)')l(JteHHtt). HH B KOeM cnyqae He Ha6Hpattre 3TH KBa,ZlpaTHLle CK06KH!

.nmr y,z:to6CTBa pacnoJIOXCHM Kaxc,z:tLltt napaMeTp Tera Ha OT,z:teJILHOtt CTpOKe. 11 XOTII CTaH,z:tapT HTML 3TO He 1anpe1llaeT, HacTO.RTCJILHO peKoMemzyeM BaM cTapaTLC.11 B ceoHX cpopMax H3· 6eraTL TaKoro CHHTaKCHca. He p836HBllttTe rem cpopM Ha HeCKOJILKO CTpOK, 3TO 1Ha'IHTeJIL· HO CHIDICaeT qHTa6eJILHOCTL KO,z:ta CTpllHHULI.

TeKcToeoe none (text) KeT 3anycnm. aaw (;L4eHSPMH B o6XOA CTSHASPTHOH ci>opMbl (coAep>Ka~eH "npaaMnbHblH" Ter ) .. 3SA8Tb 6onbWOH o6beM ASHHblX, 'l"T06bl 3TOT 6y¢ep nepenonHMTb - M3BeCTHblH npMeM B3nOMS HeAo6pOCOaeCTHO HSOMCSHHblX nporpaMM. EcnH 3a,z:tllHO 3Ha'ICHHe aTpn6yn value, TO B TCKCTOBOM none 6y,z:teT H3HaqaJihHO OTo6paxceHa )'KalaHHa.11 CTpOKa.

none ee.Qa napom1 (password)

00J1HOCTLIO aHaJIOm'leH Tery , 3a HCKJIIO'ICHHCM TOrO, 'ITO CHMBOJILI, Ha6npaeMLre nonL3oaa1eneM, He 6y.z:tyT 01o6paxcaTLCJ1 Ha 3KpaHe.

3TO y,z:to6eo,

ecJIH HyxcHo

1anpocHTL Kaxolt-ro naponL. Kcram, ecnn 1a,naetc.11 3Ha'ICHHe napaMeTpa value, see 6y,z:teT e nop.11,z:tKe, O.z:tHaJCO, nocMoTpeB ncxo.z:tHLrlt HTML-TeKCT CTpaHHULI B 6paY3epe, MOXCHO ysn.lleTL, 'ITO OH (6pay1ep) YKalaHHOe 3Ha'leHHe He nOK83bIBaeT (Henocpe,z:tCTBeHHO Ha CTpaHHue ). C.nenaHO 3TO, BH.llHMO, H3 coo6paxceHHii 6e1onaCHOCTH; XOTII, KOHC'IHO )ICC, 3JlOYMbJW·

88

4acm& /. 0cH06hl Web-npoapaMMupoeBHUR

neHHHK nenco npeo.nonee-r TilK)'JO 3a.LQHTY, ecnH Bid nonblTaeTecb cKpbITh c ee TO B8;fCHOe.

noMO~IO

ttTo-

CKpb1Toe T&KCToaoe none (hidden) CJI)')l(e6HyIO HHoPMa.I.UOO, .no KOTOpoA nOJib30BaTemo HeT .nena, - HanpHMep, napaMeTpbl HaCYpOAKH. TiycTh, HanpnMep, y Hae HMeerc.11 MHoroueneeoA CGI-cueHapnA, Korop1>IA }'MeeT npHHHMaTu .AaHHhle nonb3oeareJI.11 H ompaBJI.11Th nx KaK nottroBoe coo6meHHe. TiocKon1>KY MbI 61>1 He XOTeJIH HKCHpOBaTh e-mail noJiy11aTeJI.11 )l(ecTKo, HO B TO )Ke BpeM.11 H He CTpeMHMC.11, ttTOObl DOJib30BaTeJib MOr ero MeH.llTb nepe.A ompa&KOA Ke Ill lllMeHOBSHHOH). npaKTlll'·leClKaTb Knae111wy Af1A ompaeKM cpopMbl, a He B03111TbCS!

89

rnaea 3. CG/ 1J3HYmpu

C HaJKaTMeM submit-KHOnKM. np1o1 3TOM, pa3yMeeTCR, A8HHble KHOnKM He nOCblllalOTCR HS cepsep. BoT TOrAa-To Hae M Bblp~MT hidden-none, HanpMMep, c MMeHeM submit: ecn1o1 ero 3Ha'ff!HMe yCTaHOBJleHO, TO Cl.IE!H8pMM nOHMMaeT, 1.11'0 nOJlb30BaTeJlb asen K8KM0-TO AaHHble, a npOTMBHOM cn~ae ~eHapMli1 t5bln 38nyll.leH anepeble nYTeM Ha6opa ero URL 1o1111o1 nepexoAa no rnnepCCblnKe.

He3aBMCMMblM nepe1K1110'laTenb

(checkbox)

3ToT rer reHepnpyer HeJaBHCHMbdt nepeICJIJOqarenb (HJIH ct>nIDKoK), Korop1>1it MmKeT 6b1Tb JIHOO ycTaJJOBJieH, JIH6o c6pomeH (KBa,n.paTHK c ranoqKoit BffYTPH HJIH nycroii COOTBeTCTBeHHO). Ecmt nonb30earenb ycraHOBHJI noT :meMeHT, npe:lK,!l.e 'IeM HIDKaTb KHODKY JJ.OCTaBKH, Cl.leHapmo DOCTYfiHT CTpoKa UMR=SHa'leHUe, B DpOTHBHOM cnyqae He npuiJem HU'le20, 6yJJ.TO Harne none n He cymecTByeT eoece. Ecnn 3a.ZJ.aH arpn6yr checked, TO ct>mm:ox 6yJJ.eT H3Ha'Ia.rn.Ho ycraHoeneHHblM, HHa'Ie - H3Ha'IaJibHO c6poweHHblM. 3aeMCMMblM nepe1K1110'laTenb

(radio)

nIDKKH He C8"3aHl>I JJ.pyr c JJ.pyroM, TO TOJlbKO OJI.Ha pa.ZJ.HOKHODxa H3 rpynnbI MO)l(eT 6b1Tb Bb16patta e TeJCYU\Hit MOMeHT. KoHe'IHO, 'lame ecero onpeJJ.em1lOTCJ1 HeCKOJibKO rpynn pa.n.noKHonoK, HeJaBHCHMbIX JJ.pyr OT JJ.pyra Hama KHonKa 6yJJ.eT JJ.eitCTBOBaTb coo6ma c JJ.PYfHMH, HMelOUlHMH TO )l(e 3HatfeHHe arpn6yTa name - HHblMH CJIOBaMH, TO )l(e HMJI. Orc10J1,a BblTeKaeT, lfTO, B OTJIH'IHe OT ecex JJ.pyrnx 3JleMeHTOB cl>PMbl, JI.Be pa.n.noKHonxn JJ.OBOJibHO 'IacTo HMeJOT OJJ.HHaKOBbie HMeHa Ecnn nOJib30Barenb OTMeTHT xaKoii-To nepeKJllO'laTeJib, cueHapn10 6yJ1.eT nepeJ1.aHa CTpoKa uwr-3Ha'leHUe, npnqeM sHa'leHUe 6yJJ.eT TeM, KOTopoe YKa3aHO B arpn6yre value BbI6paHHOii KHODKH (a ece OCTaJibHble nepeKJDOqaTeJIH nponmopnpYJOTCJI, KaK 6yJJ.To HeycraHoBJieHHbie ct>nIDKKH). Ecnu YKa3aH napaMerp checked, KHODKa 6yJ1.eT H3Ha'laJibHO Bb16paHa, B npoTHBHOM cnyqae- HeT. JU1

c

nPHME'IAHHE 4yaCTByeM, aac YJK8 M~aeT aonpoc: n~My 3Ta wryKa Ha3b1BaeTCR PSAHOKHOOKOH? npH 4eM PSAMO, cnpawMaaeTCR? Bee 04eHb npocro. Aeno a TOM, l.ITO Ha CTapblx PSAMonpMeMHMKax (KaK M Ha MarHMToQ>oHax) t5bll1a rpynna KllaBMW, QAH8 M3 KOTOpblX Moma "3an11naTb", OCB06olKA8R npM 3TOM Apyryio mae1o1wy M3 rpynnbl. HanpMMep, ecn1o1 PSAMO Momo noaHTb 3 CTaHL.IMM, TO y Hero 6blllO 3 KJ18BMWM, M a KOHKpeTHblM MOM8HT epeMeHM TOJlbKO O,QHa M3 HMX Moma 6b1Tb Ha>K&Ta (nonpo6yl1Te cnywaTb cpa3y HecKOllbKO craH1..11o1l'i!). ComacHbl, l.ITO TepMHHOllOrMR O\ff!Hb cnopHa, HO MCTOPMR ecTb MCTOPMR ...

TYT

4acmb I. 0cH08bt Web-npoapaut.tupoeaHUR

90

KHOnKa OTnpaBKM ct>opMbl (submit)

Co3.D.aeT KHOnKy no.nTeep)f()J.eHHJI c HMeHeM name ( ecJJH JTOT 8TpH6YT YK838H) H Ha.3BllHHeM (TeKCTOM, BLIBO.D.HMLIM noeepx KHOnKH), npHceoeHHLIM aTpH6yry value. KaK yxce roeopHJJOCL, ecJJH 33,llllH napaMeTp name, nocne HWK8THJI KHOnKH OTnpaeKH cueHapHIO BMeCTC c .npyfHMH nap8MH 6y.ne-r nepe.naHa H napa

UMR='I'elCCT_lCHOnl01

(eCJJH HWKaTa He .D.llHHWI KHOTIKa,

a .npyrWI, 6y.D.eT nepe.naaa TOJlbKO CTpOKa HWKaTOit KHOilKH). 3To oco6eHHO y.no6HO, Kor.na B cpopMe .D.OJDKHO 6b1Tb HeCKOJlbKO KHOnOK submit, onpe.neJJJllOIJ..tHX pa.3JJHtfHLie .neitCTBHJI (HanpHMep, KHOnKH CoxpanHT1t H Y.D.aJJHTlt e cueHapHH pa6oTh1 c 38DHCLJO KaKoit-To 6aJLI .ZJ.llHHLIX) -

B TaKOM cnyttae ttpe3BbltfaitHO nerKO ycTaHOBHTL, KaKaJI >Ke KHODKa 6blJla HWKa-

Ta, H npe.nnpHHJITb HyxopMbl (reset) TeKCT,

IC07'0p!M (5y.QeT HSHa'la.m.HO O~H B 'I'eKC'l'OBOM

K8K nenco BH.Aen.,

3TOT

nane

Ter HMeeT 38Kplal88IOI~1dt napm.dt IlapaMCTp

DOJUI aao.n:a a CHMBOJIBX. a

rows -

ero awcmy. IlapaMCTp

wrap

cols

38,ZlaCT WHPHHY

onpe,lleJUleT, K8K 6y.n:eT ei.1-

rJU1,Aen. TCKCT B none BBO,lla. OH MOlKeT HMen. O,llHO H3 TpCX 3Ha11eHldt (no YMOJI118HHJO DO,llP83YMCB8CTCJI

none).

[] Virtual -

HaHOOJiee y.n:o6m.dt THO BWBO,lla.

Cnpua OT TCKCTOBOro

DOJUI B&IBO,D,HTCJI no-

JlOC8 DpOKpynCH, H TCKCT, ICOTOpltdt Ha6HpaCT DOJllt3088TCJlb, BHCWHC BltlrJUl,llHT p836HTWM Ha CTpOKH B COOTBCTCTBHH c WHpHHOA DOJUI BBO.ll8, npH11CM nepeHOC OC~CCTBJIJICT­ CJI no CJIOB8M. 0,llH8KO CHMBOJl HOBOA CTpOKH BCT8BJUICTCJI B TCKCT TOJlbKO npH HaJKaTHH ICJl8BHWH

.

C:J Physical [] None -

38BHCHT

OT peBJIH38llHH 6paY3CP8,

OOD111HO 011CHL DOXO>K Ha

TCKCT OT06paxcaeTCJI B TOM BB.AC, B KOTOpOM 38HOCHTCJI.

ECJIH

none.

OH He YMCIUCTCJI

B TCKCTOBOC none, IUCTHBH3HpylOTCJI DOJIOCDI npoxpynCH (a TOM 11HCJIC H ropH30HT8JILH8JI). Ilocne ompaeKH cl>oPMY TCKCT, KOTOpltdt BBCJI DOJllt30B8TCJlL, 6y,lleT, K8K OOD111HO, npe,llCTaBJlCH napoft UMP'I'eKCT, 8H8Jl01'H11HO Tery 0,llHOCTpo11HOro DOJUI BBO,ll8

Tar y

.

cnMCOK .

Hae OCT8JlCJI DOCJle,llHHA Ter -

OH npe,llCT8BJIJleT co6oA BDID8,Zl8lOlllHA (HJIH

pacxplalTidA) CDHCOK. 0,llHOBpeMeHHO MO)l(eT 6LITlt BI.16p8H8 O,llH8 HJIH HCCKOJlbKO CTpOK. Cl>opMaT rera cne~II.tHA:

~xal ~xa2 ~xaN

Mw BH.AHM,

11To H 3TOT Ter HMCCT napm.dt 38KpLIB8IOII.tHA. KpoMe TOro, ero c~ecTeoBaHne

HCMblCJlHMO

6e3 TeroB , KOTOplde H onpeJXCJllllOT COJXCP)l(HMoe CDHCKa.

size paeeH 1, TO cnuB npOTHBHOM cnyqae- 38HHMaCT size CTpOK H HMeeT DOJIOCLI npoxpynCH. EcJIH ~ 8TpH6yY multiple, TO 6y.n:eT p83peWCHO Bld6Hp8Tb Cp83y HCCKOJlbKO 3JlCMeHTOB H3 CDHCKa, a HH811C-TOJILKO O,llHH. KpoMe TOro, 8TpH6yY multiple He HMeeT

IlapaMCTp

size

38,ZlaCT, CKOJILKO CTpOK 6y.lleT 38HHM8TL CDHCOK. EcJIH

COK 6y,lleT BldD8,Zl8JOUlHM,

CMWCJ18 J1)UI

BLm8,ZlaJO~ero

CDHCKa.

K8)1(.11:8JI CTpOKa cnncxa onpe.n:eJIJleTCJI CBOHM TeroM

value,

.

EcnH e HCM 38,ZlaH 8TpH6yY

KaK 3TO qacTO 6blB8eT, TO COOTBCTCTBYJO~ CTpOK8 CDHCK8 6y.n:eT H.Zl:CHTHcliHl.lHp0-

88TLCJI ero 3Ha11eHHeM, a CCJIH He 38,ZlaH, TO CaMHM TCKCTOM 3TOA CTpOKH (C"IHT8eTCJI, 11TO 3HalJeHHe

value paeao CaMOA

CTpoKe). KpoMe TOro,

ecJIH YJC83aH

napaMCTp

selected,

TO JJ.aH-

HaJI CTpOKa 6y.ACT H3H&lJ8JlLHO ew6paaHoA. KcTaTH, "lyY& ae 3a6i.urn: 38Kp&1Ba10mae Tern

92

4acm& I. 0cH08bl Web-npoapaMMupoeaHUR

MO)l(HO onycKaTb, eCJIH ynpomeHHe He C03)laeT KOHJIHKTOB c CHHTIUCCHCOM HTML (B )leHCTBHTeJibHOCTH :no MO)l(HO )leJiaTb noqTH acer.Ila).

,[{ae~Te renepb nocMOYpHM, B KaKOH q,opMe nepecbuuuoTCJI )laHHble cnHcKa cueHapHJO. Hy, co CUHCKOM O)lHHO'IHOro BbI6opa epo)le 6bI HCHO r)le

UMH-

HMH Tera , a

3Hat.JeHUe-

npOCTO nepe)laeTCH napa

1'1WJ-3Hdl.JeHUe,

H.lleHTH«i>HKaTop Bhl6paHHoro 3JieMeHTa (T. e.

JIH60 aYpH6yY value, JIH6o CaMa CTpoKa 3JieMeHTa CilHCKa).

CnMCKM

B

MHO>KecTeeHHoro e1::t16opa (multiple)

KaKOit «PopMe npHXOMT )laHHbie cuenapmo, ecnn 6hm C03.llaH multiple-cnncoK? QqeHb

npocTo: ece npoH30MeT TaK, 6y.llTO ecTb He O)lHH, a HeCKOJlbKO He-multiple-CUHCKOB, ece c O)lHHaKOBbIM HMeHeM, H B Ka)l()lOM H3 KOTOpbIX Bbropauo no O)lHOM}' 3JieMeH'ly. l1HbIMH CJIOBaMH, CTpoKa napaMeYpOB, nopo)l()leHHaH 3THM reroM, 6y)leT BblrJijl)leTb npHMepuo TaK:

KcrnTH, 3TO coeeprneuHo ueyHHKaJibHbIH cnyq~, KOr)la c O.llHHM HMeHeM ceJ1JaHo cpaJy HeCKOJibKO 3HaqeHHil. ,[{eHCTBHTeJibHO, HaM HHKTO ue MeIIJaeT C03)laBaTb H .npyrne rem c H,LleHTH'IHblMH HMeHaMH. 3To qacTO )leJiaeTCJI, HanpHMep, .llJljl nIDKKOB: O.rt11H
,ll,Ba
Tp11
EcnH Tenepb noJihloeaTem. ycTaHoBHT cpaJy ece «PnIDKKH, TO cueHapmo nocrynnT cYpoKa (KoHequo, e URL-KO.llHpoBaHHOM BB.Ile): UMR=0JI11H&UMH=llea &J1MR=Tp11

113 BCero CKa3aHHOro CJie)lyeT ue oqeHb YTelllHTeJibHbIH BbIBO.ll: npH pa36ope crpoKH napaMeYpOB B cueHapHH Mb! He MO)l(eM nomlraTbCH ua TO, qrn Ka)l()lOit nepeMeHHOH COOTBeTCTBy-

eT TOJlbKO O)lHO 3HaqeHHe. HaM npH,LleTCJI yqHTbTBaTb, 'ITO HX MO)l(eT 6bITb He "o.nuo", a "Muoro".

A

3TO oqeHb HenpIDITHO c rnqKH 3peHHH nporpaMMHpoBaHIDI, oco6eHHO Ha

C.

nonyTHO Mb! 06Hap}'iKHJIH, qyo mo6oii multiple-CilHCOK MOlKeT 6bITb npe,ncTaBJieH Ha6opOM

.1~oe (HeJaeHCHMbIX nepeKJUOqareneu), a mo6ou He-multiple pa,nHOKHonoK. TaK 'ITO, eoo6me roeop.ll, Ter -

e BB.Ile ttecKOJibKHX

HeKOTopoe «i>YHKUHOHaJibHOe H3-

nnrnecTBo, H c ToqKH 3peHffjl cuettapIDI BilOJIHe MO)l(eT JaMeHJITbCJI «i>Jia)l(KaMH H pa,nHOKHOnKaMH.

HTML-cyll,\HOCTM ,[{o CHX nop Mbl He JaOCYpMH BHHMaHHe Ha TOM, KaK AOJl)l(Hbl 6bITb npe,ncTaeJieHbl 3Ha'leHHJI wfpH6yYOB -

HHbIMH CJIOBaMH, KaK BblfJIJIAAT CTpOKH B KaBbJqKax, KOTOpbie pacnonaratOTCJI

BffYTPH reroe. HanpnMep, npeACTaBbTe, 'ITO, ecnn B cne.zzy10w;eM rere:

HaM IlOYpe6yeTCJI BCTaBHTb crpoKy He "O.!I11H", a "MHoro", co.nep)l(allzylO KaBbNKH? QqeBH.llHO, Mbl He MO)l(eM HanHCaTb Hanpnty10:

rnasa 3. CG/ U3Hympu

,lVIJI

93

pemeHHA: TaKHX npo6neM CYlllecTByeT cneuuanbHbrii MeTOA KOAHPOBaHHJI AaHHbix.

Kori.a

HeKoropb1e

cneUHanbHble

CHMBOJibl

3aMeHJUOTcA:

JKBHBaJieHTHbIMH

HM

HTML-

CYlllHOCTIIMH (HTML-entities). Bee TaICHe CYlllHOCTH HMelOT O,AHHaICOBblH cpopMaT, HanpuMep: , ", ' H T. A· -

Ha.QeeMcA:, Bbl ynoBHJIH JaKOHOMepHOCTb.

IlpH BCTa.BKe 3Hati:eHHD aTpH6YTOB HeOOXOAHMO o6nareJibHO 3aMeHA:Tb CHMBOJlbl, nepeti:HCJieHHble B Ta6JI.

3. I. Ta6nUl.JIJ 3.1. 0cH06Hbl8 HTML-cyl.l.{Hocmu

l.fTO 38MeHATb

~M 3aM8HA'Tb

l.fTo 38MeHA'Tb

~8M 38M8HRTb

>

>

n

"


HKaTOp 6y,neT O,D;HHM, a 3aBTPa, B03MO:lKHO, COBCeM .l\PyrHM. TaK 11ro RaM npHJleTCll, npe)l(,D;e 11eM aHaJIH3HpoeaTb ,naHH1>1e, cqHTaTi. :rroT u,neHTH«t>uKarop 8 6ycl>ep (HM 6y,neT IlOCJieAOBaTeJlbHOCTb CHMBOJIOB AO nepeoro CHMBOJJa

\n).

BHHMAHHEI CTSHAaPT npoToKons HTTP pernaMeHT111pyeT, l.fTO 111AeHT111¢l111KsTop HCNsns TaK>Ke AOn>KeH 6b1Tb AocryneH "lepe3 OAHY 1113 nepeMeHHblX OKP)'>KeHMA. Ho Mbl He noMHMM 111 He XOTMM 3HSTb ee Ha3&aHMe - ce!Nac o6'bRCHMM, nO"leMy. HeKoTopble 15payaepbl (ocol5eHHO CTSpb1e) nyralOT 3TOT MAetm1¢llllK8Top 111 nplllCblJ"lalOT ero HenpaBMnbHO c ABYMA npeAweCTBYIOL14111Mlll M111HycaM111 (a OCT&nbHble -15e3 HI/IX), TSK l.fTO ~Hapllllll, He paCC"llllTblB8IOIJ.lllle HS TSKOM OOABOX, nepecraHyT pal5oT8Tb. HUKoa08 He llOllaraMTI!Cb HS 3TY nepeM8HHYIO OKpyJKeHl/IA (AaJKe ecn111 Y3HaeTe, KSK OHS H83b1Ba8TCJI)! BMeCTO 3TOro "llllTSMTe nocneAOBaTenbHOCTb ClllMBOOOB AO nepeoro nepeBOAS CTpOKlll Ill aocnplllHlllMSMTe lllMeHHO ee KSK P83AenMTenb .

.naJiee aJiropHTM pa.J6opa ,no.JDKeH 6b1Tb CJie.IzyJOll.UfM: 8 UHKJie Mbl nponycKaeM CHMBOJJbl u,neHTH«i>HKaTOpa H nepeBOAa CTPQKH, H38JleKaeM IlOACTPOKY uwr-"LJTO-TO" (He o6pamag BHHMaHHll Ha Content-Disposition), AO)l(H)laeMCSI

.l\Byx CHMBOJIOB

nepeBOAa CTpoKH H JareM

C'IHTileM 3HaqeHHeM COOTBeTCTB)'IOW:ero nOJIJI ece Te AaHHble, KOTOpbie pa3Mew:eHbl .IlO CTPOKH \nJ-!aeHTUFaTOp (HJIH :lKe AO KOHUa, eCJIH TaKOit CTpoKH 6oJlbwe HeT).

KaK

BH,nHTe, ece

.IlOBOJlbHO npocro.

BHHMAHHEI CTaHASPT

HTTP npeAnlllCblB&eT, "1To6bl nepeBOA CTpoKM COAepJKan CMMBOnbl HOBOM CTpoKM 111 \r\n, s He O,tll/IH \n. KsK Bbl YJKe, HSBepHoe, "IYBCTByeTe, CYIJ.leCTBYIOT

B03BpaTa KSpeTlllll -

6pay3epbl, KOTOpble ol5 3TOM Ill He Aor8Ab188IOTCA Ill nOCblnSIOT TOnbKO eAlllHCTBeHHblM \n. TaK l.fTO, 15yAbTe roTOBbl K TOMy, l.fTC>l5bl npaa111nbHO o6pa68Tb188Tb Ill 3TY CMTy8Wo1IO.

Ter 3arpy3KM aina (file) Tenepi. eepHeMCll K TOM}', c 11ero Haqa.JIH -

K JarPY3Ke ct>aA:noe. CHaqaJia BhUICHHM, KaKOH

rer HMO BCTaBHTb 8 4>opM}', qTOObl B HeA IlOllBHJICSI COOTBeTCTB)'IOW:HH 3JJeMeHT ynpaBJieHHll -

none BBOAa TeKcTa c KHODKoit

HOCTb :

Browse

cnpaea TaKHM TeroM SIBJllleTCSI paJHoeu,n-

96

4acmi:. I. OcHoebl Web-npoapaMMupoeaHuR

DyCTb IlOJib30BaTeJI.b BbI6paJI KatcOit-TO aftJI (CKa:iKeM, C HMeHeM

Kam(1JI02\u.WR_ filename="KaTa11or\11MH_KHMbIM aiina nepe.naeTCJI H ero HMJI e CHCTeMe noJI.bJOBarem1 (napaMeTp filename). Ha 3TOM, no:>KaJI)'H, H JaBeprnHM 0603peHHe BOJMOXCHOCTeH 3arpy3KH cpattJIOB. Ha.neeMCJI, Mbl noceJIJIH B Bae HenpHS13HeHHOe OTHOIIIeHHe K no.no6HblM MeTO.naM: .neACTBHTeJibHO, nporpaMMHpoBaTb 3TO -

He caMoe npH.llTHOe JaHRTHe Ha CBeTe (yKa:iKeM TOJibKO Ha

TO, 'ITO npu.neTCJI HCilOJI.b30BaTb npHeM.bl nporpaMMHOit 6ycpepH3aUHH, 'ITo6bI npaBHJibHO HaitTH pm.ne11Hre11b). BoT eme 0.!1.HH .noeo.n B no11b3Y

PHP,

B KOTopoM He H}')KHO Bbm0.1HJITb

B npHHUHne HHKatcOit pa60T.bl, 'IT06bl C03,naTb IlOJIHOQeHHblil cueHapHH c B03MOXCHOCTblO 3arpy3KH aitJia.

llTO T8KOe cookies M "c

'18M

MX 9AJIT"?

CHa'laJia XOTeJIOC.b 6bI CKa.JaTb napy CJIOB HaC'leT CaMoro TepMHHa

cookies

(3TO MHO:>KeCT-

BeHHoe 1u1c110, npoHJHOCHTCJI KaK "KyKHc" HJIH, 6011ee "pycum.{HpoBaH0 11 , "KyKH"). B 6yxBaJibHOM nepeeo.ne c11oao 3BY'IHT Kate "neqeHbe", H no'leMY KoMnaHHJI Netscape TaK HaJBaJia CBOe H300peTeHHe, He COBCeM RCHO.

A

IlOCKOJibKY IlHCaTb "ne'leHbe 11 HeCKOJibKO Hey.no6Ho,

'IT06b1 He Bbl3blBaTb HecaoeepeMeHHbIX raCTpOHOMH'leCKHX accou:HaUHit, Be3.ne, r.ne MO:>KHO, Mbl 6y.neM npHMeHRTb HMeHHO CJIOBO cookies, BO MHOxcecrneHHOM 'lHCJie H MY:>KCKOro po.na. KcTaTH, a e.nuHcTBeHHOM 'lHCJie 3TO noHRTHe JanHCbIBaeTcx cookie H npou3HOCHTCJI Ha pycCKHil MaHep -

"KyKa".

Ha'IHeM c npHMepa Cxa:iKeM, MbI XOTHM .no6aBHTb BOJMOXCHocTb ocTaBmITb KOMMeHrapHH K CTpaHHQe: IlOJib30BaTeJib BB0.!1.HT ceoe HMJI, e-mail, a.npec .ll.OMaWHeH CTpaHH'lKH (H .npyryio HHopMaQHIO 0 ce6e), HaJCOHeu, TeKCT coo6meHHJI, H nocJie Ha:iKaTHJI KHOIIKH ero MbICJib pmpaBIDleTCJI B nyTeWeCTBHe no npoeo.naM H cepaepaM, 'IT06bl B KOHQe KOHUOB nonaCTb B HeKyio 6my )J,aHHbIX Ha HalIJeM cepeepe H OCTaTbCR TaM Ha BeKH Be'IHbie. M-.na ... Tenepb npe.nno11o:>KHM, 'ITO 3Ta Hama CTpattHua -

.noaoJibHO qacro nocemaeMoe MeCTo,

y Hee ecTb IlOCTOJIHHbie IlOJib30BaTeJIH, KOTOpbie HeCKOJibKO pm Ha .nmo OCTaBJIJllOT TaM CBOH coo6I..QeHHJI. 1-ITo :>Ke HM npH.neTCR Ka:>K,nblit pa3 BBO.ll.HTb caoe HMJI, a.npec 3JieKTpOHHOA no'ITbl H .npyryio HHopMaUHIO B nycTble nom1? KaK 6bl c.ne11aTb TaK, 'lT06bI 3TO ace JanoMHHaJIOCb r.ne-ro, H .na:iKe npH c11e.nyiomeM JanycKe 6pay3epa tty)f(Hbie nom1 cpopMbI HHHU:Ha-

I CHMB0;1

!l:>

OJH3'13eT nepettoc CTpOKH B KHHre, " ero He cne.zzyeT BBO!IHTh npH 11a6ope TCKCTa nporpaMMbl.

rnaea 3. CG/ U3Hympu JIH3HpoBa1IHCb aBTOMaTH'lecKH, pa3yMeeTCJI -

97 y K3JK,ztOro noJib30BaTeJIJI HH,ll.HBH;zyaJibHO,

TeM, 'leM OH 3anOJIHHJl HX paHee? lfro6bl JTOro .n.o6HTbCJI, B npHHUHne cymeCTBYJOT .n.ea MeTOJJ.a. 06a OHH HMelOT Ka.IC .!(OCTOHHCTBa, Ta.IC H He.D.OCTaTKH, H BCKOpe Mbl YBH){HM, B 'leM )f(e OHH 3a.KJDO'IWOTCJI. nepBbdl cnoco6: XpaHHTh Ha cepeepe OT){CllbH)'IO 6a:Jy ){aHHblX, B KOTOpoH ~ K3JK,ztOro noJib30BaTeJIJI no ero IP-~pecy MOJKHO 6&mo 61>1 nonyqHTh nocne.n.HHe HM )Ke BBe,!(eHHbie .!(aHHbie.

B

npHHQHne, 3TO peweHHe .n.oeon&Ho yHHeepcaJibHO, O){HaKO, y Hero ecT& .!(Ba cy-

mecTBeHHbIX He,!(OCTaTKa, KOTOpble CBOAAT Ha HeT Bee npeHM)'mecTea. fJiaBHbIB H3 HHX 60JiblllHHCTBO IlOJlb30BaTeJieH He HMelOT Ke, c TO'IKlll 3peHMSI nporpaMMMCTB xopow1111i1 nonbJOBaTenb - cnenoli1 nonb3oeaTenb?" TorAa Mbl OTeernM: "HeT 111 el.Lie pa3 HeT! TaKoli1 nonb30BBTenb xopow 11111wb AJ1SI A1113Bli1Hepa, AJ1SI nporpaMMMCTa >Ke >KenaTeneH nonb3oeaTenb 6e3PYKll11i1 (Mn111, no Kpali1Heli1 Mepe, 111o1weHHblli1 K11ae111arypbl K MblWK)". BoT 'ITO 03HalfaJOT napaMetpbI cookie.

Ll name BMeCTo Jtol!. CTpoKH H)')KHO 3a.z:i;an, HMJI, 3aKpenJieHHoe 3a cookie.

HMJI

,z:i;oJIJKHo 6bITb

URL-KO,D;HpoBaHHhIM TeKCTOM, T. e. COCTOJITb TOJlhKO H3 an«t>aeHTHO-QH4>pOBblX CHMBO-

rnaea 3. CG/ U3Hympu

99

noe. BnpoYffKUHJO URL-Ko~poBaHHJI (KOTOpWI, KCTilTH, paJa e .nea cno:>KHee, 11eM cf>YHKl..lHJI ,wIJI .neKo~poBaHHJI, T. K. TpC6yeT .nonOJIHHTCJlbHOro e1>meneHHJ1 IlaMJITH). ~

CJ expires Heo6J1JaTCJ1bHWI napa expires=..aaTa 38,llaeT epeMJ1 :>KHJHH Hawero cookie. To11Hee, cookie CaMOyHH11TO:>KHTCJ1, KaK TOJ1bKO HacT}'ITHT YK838HHWI .nara. HanpnMep, ecnn 38,llaTb

expires=Sunday, 2-Feb-17 15:52:00 GMT TO "ne'!eHbe" 6y.nCT ":>KHTb" TOJibKO .no

2 cf>eBPaJJJI 2017 ro.na KcraTH, BOT

BaM H BTopwr

HenpHJITHOCTb: xopowo, CCJIH MDI 3HaeM HaBepHJ11Ca BpeMJI 11 CMePTH" cookie. A CCJIH H&M H)'JKHO ero Bbl'IHCJIJITb Ha OCHOBe TCJCyl.Qero BpeMeHH (HanpHMep, CCJIH Mbl XOTHM, 'IT06bI cookie cymeCTBOB8JI

10

~eA nocne ero yCTaHoBKH, IC8K B no.naemnomeM 6onhWHHCTBe

cnyqaeB H npoHCXOAHT)? Ilp~eTCJI HCilOJlb30BaTb cf>yHKl..lHIO, KOTOpWI cf>opMHpOBaJia 6bI Kane~apH)'IO .nary B YKaJaHHOM BbIWe cf>opMare. KcraTH, ecnn 3TOT napaMCTp He YKaJaH, TO BpeMeHeM :>ICH3HH 6y.neT C'IHT3TbCJI BCJI TeKyl.QaJI CCCCHJI pa60Tbl 6pay3epa .no Toro MOMeHTa, KaK nOJlb30BaTCJlb ero 3aKpOCT.

CJ

domain

napaMCTp domain=UMll_XOC'I'a 38,llaeT HMJI XOCTa, c KOTOporo ycTaHOBHJIH cookie. PaHee Mbl Y'fCe roBOpHJIH npo 3TOT napaMCTp. Taic BOT, OK83blBaeTCJI, ero MO:>ICHO MeIDITb epyqH)'IO, nponncae 3.llCCb HyJKHbdt 8,llpec, H T8lCHM o6p830M "no.napHTh" cookie APyroMy xocTy. TOJibKO B TOM cnyqae, ecJIH napaMCTp He 38,llaH, HMJI XOCTil onpe.neJI.JleTCJI 6pay3epoM aBTOMaTH11eCKH.

CJ path napaMCTp path=nyTI> OObl'IHO onHCbIBaeT KaTaJior (TO'IHee,

URI), B KOTOpoM pacnOJIO)KCH

cueHapnA, yCTilHoBHBWHA cookie. Kax Mbl BH.llHM, 3TOT napaMCTp TaK:>Ke MO)KHO 38,llaTb BpyqH)'IO, JanHC8B B Hero He TOJibKO KaTaJior, a eoo6me ece, 'ITO yrO.llHO. 0.nHaico npH 3TOM CJie~eT noMHHTh: YKaJaB XOCT, OTJIH'IHbIA OT XOCTil CQeHapH.JI, HJIH nyn., OTJIH'IHbIA OT URI KaTaJiora (HJIH poAHTCJibCKOro KaTaJiora) cueHapHJ1, MbI TeM CaMbIM Hmcor.na 6onbme He y&H.llHM Haw cookie B 3TOM'CQeHapHH.

CJ secure

3ToT napaMCTp CBJl38H

c 38JllHUleHHblM npoTOKOJIOM nepe.naqH HTTPS, KOTOpblA e KHHre

He paccM&TPHBaeTCJI. Ecnu Bbl He co6npaerecb nncaTb cQeHapnH ,wIJI npoBe.neHHJI 6aeKOBCKHX onepaQHR c Kpe~blMH KapTO'IKaMH (HJIH HHble, TPCGyiolllHe noBLIWeHHoR 6eJonacHOCTH), BP.Jl.ll JIH CTOHT o6pa.w;aTb Ha Hero BHHMaHHe. Ilocne 3aII)'CKa CQeHapHJI, BblBO.llJIUlero COOTBeTCTByJOUlHA 38.fOJIOBOK (HJIH Ter ), y nopb30B8TCJIJI nOJIBHTCJI cookie c HMeHeM name H 3HatfeHHeM value. Eme p83 HanOMHHaeM: JHa'!eHHJI Bcex napaMeTpoB cookie .noJI:>KHbl 6b1Tb URL-Ko.nupoBaHbl, B npoTHBHOM cnyqae B03MO)KHbl HeO~aHHOCTH.

4acm& I. 0cH08&1 Web-npoapaMMupoeaHUR

100

nony'leHMe cookies M3 6pay3epa nonyqHTb cookies )l)UI cueHapIDI HeCKOJlbKO npo~e: see OHH xpaHRTCJI B nepeMeHHOA OKpy)l(CHIDI H'ITP- COOKIE B TaKOM )ICC opMaQHJO 11)'BH,ZUIT11 TIUOKe H CQeHapHR. KCTaTH, y 3JIOyMbllilnCHHHKa HeT HHKaKHX lllaHCOB IlOJI)"IHTb 3Ha'leHHe cookie IlOCeTHTenJI, IlOTOM)' 'ITO OHO xpaHHTCJI y Hero Ha KOMnbl(Yfepe, a He Ha cepeepe.

c

0IlJITb Mbl HaMeKaeM Ha TO, 'ITO HCilOJlbJOBaHHe Jl3bIICa H Ha 3TOT pa3 )lOBOJibHO 3llTpYAHHTeJibHO. Hey.ao6Ho URL-.lleKOAffPOBaTb H KOAHpoBaTb .llaHHble npu yCTaHOBKe cookies, HaKJia.AHO p836HpaTb HX Ha '18CTH, .lla H eoo6w.e HaIIIa npOCTilJI nporpaMMa IlOJI)"IHJiaCb CJIHIIlKOM AJIHHHoA. He npae.aa nu, npHJITHO 6y.aeT o6Hap}')KHTL, 11ro e PHP see 3TO peanH30BaHo aBTOMaTH11ecKH: ,!l,JIJl pa6oTbI c cookies c~ecTByeT BCero OAHa YffHBepcaJibHWI 4>YHKQHJI setcookie < ) , a nonyqeHHe cookies OT 6pay3epa eoo6w,e He BbDOBeT HHKaKHX npo6neM, noTOM)' 'ITO OHO HH'ICM He OTnH'laeTCJI OT nonyqeHHJI .llaHHblX «l>opMbl. 3TO JIOrH'IHO. B caMOM .aene, KaKWI HaM p83HHUa, KaKHe JlaHHble npHIDJIH H3 «f>opMbl, a KaKHe - H3 cookies? TO'lKH 3peHHJI CQeHapHJI - see paeHO .•.

c

Ho He 6y.aeM 3a6eran. enepe.a. 3a1tMeMCJI noKa Teopueft aeropff3a.llHH.

AyTeHTMMKa'-'MH lfaCTo 6blsatrr H}')KHO, 11T06b1 Ha KaKoit-To URL MOrJIH nonacTL TOJibKO onpe.aeneHHble nonb3osarenu. A HMeHHO TOJibKO re, y KoTOpbIX ecTL 38pemCTpHpOBaHHoe HMJI (login) u naponb (password). MexaHHJM aymeHmuleHlllR AOCTYna K KaKOMy-111116o pecypcy Bb!Ae!lRIOT Alie CT&,AlllH: aymeHnQATBeP*A8H"1e 11"1'4HOCT"1 nO!lb30118Te!lR (HanpHMep, no COOTBeTCT8"1IO napbl "110111H/naponb", COOTB8TCTB"1IO OTKpblTOro Klll048 38KPblTOMy) Ill aemopU381..lUIO - nOATBep>KP,eHllle npae nonb30BaTenR Ha 3anpaw111eaeMbl~ pecypc "111111 Ae~CTBMe.

muclJuKBl..lUIO -

MJ,1 He 6y.IJ.eM 3,neci. paccMa1'pHBan. see B03MO)f(HOCTH JTOro MeXaHH3Ma no 1'peM npHqHHaM. Bo-rrepBbIX, CYUleCTBYeT ,I{OBOJibHO MHOro THIIOB ayreHTHcf>HK8UHH, pa3JIHqaJOIIlHXCJI CTerreHblO 3aIUHllleHHOCTH rrepe.IJ.aeaeMblX .IJ.aHHbIX. Bo-eTOp1>1x, rrpH HaIIHCaHHH o6J.NHhIX CGIcueHapHee .IJ.JI.11 TOro, ~o6bl eKJllOqHn. MexaHH3M ayreHmcf>HKauHH, Heo6xo,nHMo npoeecm HeKOTOpbie MaHHIIyJIJIUHH c Hac1'pORKaMH (cf>aRJiaMH KOHcf>m-ypauHH) cepeepa. nocJie.IJ.H~e MO)l(eT 61>m. 3a1'pY.IJ.HHTeJibHO rrpH OTCyrCTBHH .IJ.OCT)'Ila K KOHcf>HrypauHOHHblM cf>aRJiaM cepeepa. HaKoHeu, B-1'pen.Hx, eec1> MexaHH3M ayreHTHcf>HK8UHH 3HaqHTeJibHO yrrpomae-rc.11 H YffHcf>HuHpye-rc.11 rrpH HcnoJih30BaHHH

PHP,

H BaM He rrpH,ne-rc.11 ffHtlero HcrrpaBJIJITb e JTHX

3JIOIIOJI)"IHbIX HaC1'pOltKax cepeepa. TaK qTO .IJ.aeakre orno:lKHM rrpaKTffqecKoe 3HaKOMCTBO c aeTOpH3auHelt H 3aJtMeMc.11 ee reopHeit PacCKa)l(eM BKpame 0 TOM; KaK ece npoHCXO,nHT Ha HIDKHeM ypoeHe npH O.IJ.HOY. H3 CaMblX rrpocn.ix mnoe aeTOpH3aUHH -

basic-ayme11murf>u1: login H password IIOJib30BaTeJIJI 3aIIOMHHaIOTCJI B CKpblTOM cookie, ")l(HBYW.eM" B Te'leHHe O.IJ.HOH cecCHH pa60Tbl c 6pay3epoM. 3areM, 'ITO 61>1 Mb! HH )J.enanH, 3afOJ10BOK Authorization: Basic 3HaqeHMe cookie

rnasa 3.

CG/ U3H)'mpu

103

6y.lleT DpHClal.llan.cll AIIll mo6oro CQCHapHll (H ,llBXCC AIIll mo6oro ,llOJCYMCHT8) Ha H8lllCM cepBepe. TBKHM 00pa30M, DOCeTHTCJDO, 3apCrHCTJ)HpoeaBWeM)'CJI O.llffS)KJlhl, HeT Heo6XO,!lHMOCTH K~ p83 38HOBO Ha6apan. ceoe HM.II H napom. B Te11CHHe TCK)'lllero ceaHca pa6on.1 c 6paYJCpoM, T. e. DOKa D0.1Ih3088TCJIL ero He 38KpoeT. nocne ycneUIHolt ayreHTHHKBllHH npH Bbl3oee mo6oro cQeHapH.11 6y.lleT ycTBHoeneHa nepeMCHHBll OKpyxK8IU8JI HM.II D0.1Ih30BaTeJIJI. TaK qro B ,llaJlbHeltmeM MO)l(ffO ee 38,lleltCTBOBaTb AIIll onpe,llCJICHHJI, KaKOlt :>Ke nocenrrem. 38perHCTJ>HPOBaJICJI.

Pe310Me B .ABHHOit rnase MLI paccK838JIH o TOM, KaJC "pa6oTaeT" HHTepcl>eAc CGI Ha HHJKOM ypoeHe H paccMOTpeJIH see OCHOBHble cnoco6bl o6pa6oTKH a nepe,Aa11a .llBHHl>IX, npHMeHlleMhle cQeHaMl.I HayqHJIHc& onepupoean. p83Jlfftun.rMH 3JieMeHTaMH cl>opM H JanycKan. cueHapHH, omp8BllJIJI HM BBC,ZJ;eHHble ,ZJ;aHHhle. KpaTKO paccMOTJ)eJIH OCHOBbl pa6on.1 c cookies,

pHllMH.

cl>opMaT multipart-,ZJ;8HHbIX, npe.D;H83H811CHHbllt AIIll nepe,A811H 6HHapHbIX aAJIOB Ha cepeep, a T8.IOICe BODpocbl ayreHTHHKamtH. IlpHXO,ZJ;HTCJI npH3H8Tb, qro rJiaBa co.nep:>KHT MHOf'O MaTepHana, BeCbMa CJ10)1Cff0f'O .llJIJI H811HHaJOIUero. ECJIH Bbl Mano 11TO noHJIJIH nocne ee npo1neHHJ1, He oyqaueaitTecb, a npocro 'IHraltTe ,nanbwe. llepe3 HeKoropoe epeM.11, nonp81CTHKOBaBWHCb e

PHP,

Bbl cMo:llCere eepayTb-

csr K .AaHHoit rnaBe H BJrJIJlffYTb Ha Hee .n;pyraMH rJI838MH. EcrecTBeHHO, e cneJzyK>l.llHX rnaeax KHHrH MHOrHe TeMhl 6y.nyT 38TpoffYTbl BHOBb, HO paCCMOTpeHbl yxce c HOBOH TO'IKH 3peHHJI - c T011KH 3peHHJI PHP.

rnABA 4

BcTpoeHHblM cepeep PHP

JlHCTHHrH .aaHHOH r.laBbl MOlKHO HaITTH

B no,1Kara.1ore buildin.

Kax )')Ke tte0,11,ttoxpaTHo ynoMHHaJIOCh paHee, ,lJ,JUI Toro •no6b1 pa3pa6aThIBaTb H OTJIIDKHBaTb cKpHnTbI, BaM notta,zJ.o6HTCll pa6oTaJOll\HH Web-cepsep. Ha'IHHWI c sepcuu 5.4, ,lJ,HCTJm6yrns PHP ctta6)1(aeTCJI BCTpoeHHblM cepsepoM, KOTOpbIH noJBOnlleT ornruKHBaTh cxpHilTbI noKaJibHO, 6e3 pa3BepTbIBaHHll CTOpOHHero w eb-cepsepa. 3AMEl./AHHE

AnR pa6oTbl HCIA cn0>KHblM npoeKToM 11111111 AJ"IR pa3eepTb1BaH111R pa6oYero Web-cepeepa norpe6yerCR C03,Q8Hllle no11HOL1eHHOro OKpy>t

Ecnn y sac OTCJTCTByJOT npaea a.n:MHHHCYparopa, e nanKe co CKpHnTOM index.php cne.eyeT BbinOJIHHTb KOMWI.ey php

-s

l~st:4000

KoMaH.Zla JanyCTHT Ha 4000-M nopzy Web-cepeep. 06paTHBUIHCb e 6paYJepe no a.npecy http://localbost:4000/, MOXKHO y1m.neTb, 'ITO cepeep caMoCTOJITenbHO 4JopMHpyeT ece Heo6xo.nHMble HTfP-3aronoeKH: curl -I http://localhost:4000 HTTP/1.1 200 OK Host: localhost:4000 Connection: close X-Powered-By: PHP/7.0.0 Content-type: text/html; charset=UTF-8

0cTaHOBHTb cepeep MO)l{HO, Ha)l(a.B KOM6HHaIUfl() KJJaBHW +.

alin hosts B npe.nbuzymeM paJ.nene e Ka'leCTee .noMeHa nepeoro ypoeH.11 Hcnonb3oeancH ){OMeH localhost, KOTOpb1A RBJI)leTCJI ncee.noHHMOM MR IP-a,npeca 127.0.0. l. Ha JJOKMbHOM xocre cooTBeTCTBHe ncee.noHHMa IP-a.npecy nponHcbtBaeTCH B 4>aH1Ie hosts, KOTOpb1A B UNIXno.no6HoA onepauHoHHOH cHcreMe MO:>KHo o6Hap~HTh no rryTH /etc/hosts, a e Windows no nyrn C :\Windows\system32\drivers\etc\hosts. KaK npaeHJJo, B 4>aH1Ie npHcyTCTB)'lOT KaK MHHHMYM .nae 3anHcH, conOCTaBJIJDOI.IJ.He ){OMeHY localhost noKanbHblH IP-a.npec 127.0.0. I e 1Pv4- H IPv6-4JopMaTax: 127.0.0.1 : :1

local host localhost

HMeHHO HMH'IHe 3THX 3anHceil: Il03BOJJHJIO HaM JanyCTHTb BCTpeHHb!il: cepeep c HCnOnb3oBaJrneM B Ka'leCTBe .l{OMeHa localhost. .z:l;uI TOro qro6bl HaCTpOHTb MbTepHaTHBHble nceB){OHHMbl, MO)l{HO ,l{OOaBHTb ,l{OilOJIHHTenbHbie 3anHCH: 127.0.0.1 127.0.0.1 127.0.0.2 127.0.0.2

site.dev www.site.dev project.dev www.project.dev

IP-a.npeca, Ha'IHHaJOW:HecH co 127, npe.l{HaJHa'leHbl MR noKanbHOro Hcnonb1oeaHHJ1, noJTO.l{Jl.11 TeCTHpoBattHH co6cTBeHHblX npoeKToe Bbl MO)f(eTe HaJHa'laTb nio6ble a.npeca H3 3TOro ){HanaJoHa.

MY

Oocne ){OOaBJieHH.11 HOBblX nceB,l{OHHMOB B 4>aAJI hosts HX MO)f(HO HCilOnb30BaTb COBMeCTHO co BCTpOeHHbIM cepeepoM.

Be~aHMe BOBHe OpH Hcnonb30BaHHH noKaJibHbIX IP-a.npecoe H3 .l{HanaJoHa 127 XXX MO)l{HO 6h1Tb yeepeHHblM, qyo HHKTO H3BHe He CMO)l(eT o6paTHTbCJI K cepeepy. 0.nmuco ecnH BaM Hao6opoT Tpe6yeTc.11 npo){eMoHCTpHposaTb pe1ynbTaT pa60TbI sawero npHnO)l{eHHJI, B KaqecTBe xocTa MO:>KHO yKaJaTb IP-a.npec 0.0.0.0: php

-s

0.0.0.0:4000

rnasa 4. BcmpoeHHblii cepeep PHP

111

B noM C.II)"lae MO)l(ffO nOJIY11HTD .noc'I)'D K Web-cepaepy o6paTHBllIHCb no IP-a,npecy xOCTa, r.ne J~eH cepaep, HanpHMep, bttp://192.168.0.1:4000. ,L\nJ1 roro qro6bl MO:lKHO 6bmo o6paTHTbCJI K xocry no iicea,noHHMY, ero npH.IJ.eTCJI JIH6o npoIIHcan. a hosts-ct>aitrre KWK,D,oro KOMilblOTepa, c KOTOporo H)leT o6p~eHHe K cepaepy, JIH6o 3aperHCTPHpoBaTb .IJ.OMeHHoe HMJI H cenan. ero c IP-a.npecoM KOMIILIOTepa, Ha KOTOpoM cepaep Jan}'meH. PaJ)'MeeTCJI, B 3TOM C.II)"lae npH 3811)'CKe B KaqecTBe XOCTil ncrq>e6yen:J1 )'KaJaTD 3TO .IJ.OMeHHoe HMJI. php

-s

axample.ocm:BO

BnpoqeM, ecTPoeHHhlD cepaep npe.IJ.HaJHaqeH nmm. .lUlJI orna,nKH, .IJ.JIJI o6ecne'leHHJI pa6on.1 OOJIHOJ:.(eHHOfO caJfra JIY11We BOCilOJIL30BaTbCJI npoMhlIIIJieHHhlMH cepeepaMH, T8K,HMH KaK Apache HJIH nginx.

KoHMrypMpoeaHMe PHP PHP HMeeT orpoMHoe KOJIHqecTBO paJHoOOPaJHhlX HacTpoeK, KOTOpble cocpe.noroqeHbl B cparuie php.ini. EcJIH Bbl TOJlbKO YCTllHOBHJIH .IJ.HCTPH6yTHB BMecTO ct>aIDia php.ini, TO MO)l(eTe Haitm JIHlllb .ztea $afula:

Cl php.ini-production - peKoMeH.IJ.OBaHHbdt Ha6op napaMeTpoB .lUlJI pa6oqero cepaepa; Cl php.ini-development - peKoMeH.IJ.oBaHHbdt Ha6op napaMeTpoe .IJ.JIJI pa6oqeit cTaHUHH pa3pa60T'IHKa.

,L\nJI JIOKaJibHoit pa3pa6oTICH php.ini-development cJie.D;)'eT nepeHMeHoean. a php.ini. He e.naBWICb ceitqac B co.ne~Moe KOH$HrypaIUIOHHOfO ct>aIDJa php.ini, JaMeTHM, 'ITO COOOIIUfTD o6 ero MecTOilOJIOllCeHHH BCTpoeHHOMY cepeepy MO)l(ff0 npH IlOMOUUf .nupeKTHBbl -c. )1.JIJI Windows KOMaH.IJ.a MO)l(eT BblfIDl.D.eTb cJie.IJ.)'IOUUfM o6paJoM: php -S 127.0.0.1:4000 -c C:\php\php.ini

,L\nJI UNIX-no,no6HoA onepauuoHHoA cucreMbl: . php

-s

127.0.0.1:4000 -c /•tc/php.ini 3AME'4AHHE

6o11ee noAPO(jHo AMpeKTMBbl PHP M¢>opMaT ¢>aMl'la php.ini onMCbl&atOTCA a 211ase 35.

Pe310Me B noi:t rnaee MbI yCTilHOBHJlH PHP, noJHaKOMHJIHCb co BCTpoeHHbIM cepeepoM H JanycTHJIH nepebdt PHP-cKpHDT "Hello world!". HaquHaJI co cJie.IJ.)'1011.leA rnaei.1, MbI npucrynaeM K HJyqeHHIO J1J1>oca PHP.

II

qACTb OcHOBbl Jl3b1Ka

PHP

I

Ir=:- - -·------ - - - · ---- --, I • uaea 5. XapaKTep111CT111Ka !=13b1Ka PHP __J -- 'i

- - - - - - - - - - - - - - - - -"' 1 rnaea 6. j nepeM0HHb10, KOHCT3HTbl, Tlllnbl A3HHblX i

-----1·-- -·-------- --J

I rnaea 8.

-I

rnaea 9.

.

1 rnaea

.

-

~-

I 1

7.

----

Bb1pa>KeH1111=1 111 onepa1.1111111 PHP --·-

- - ----

- - --

-

·--r ------- ---- -·- -----1! Pa60Ta c A3HHb1M111 cpopMbl KOHCTPYKLlllllll !=13b1Ka

- - -------t- - - -

-

--

rnaea 1 o. I Acco1.1111arneHb1e

- - ---

·-+-·- - - --- -- ---- - -

i - ---I rnaea 11.

_t _¢>yHK4111~~ 06~aCT111 ~lllAlllMOC:~

! rnaea ~

--

Macc111eb1

: ----j

__ -

~~HepaTOpbl _______________

1 •

rnABA

5

XapaKTepMCTMKa J13b1Ka

PHP

JlHCTHHrH .lllltfHOA rJJaBbl MOlKHO HMH e no.llKaTa.Jiore

"1CTOpMH

PHP

Kor,lla B KOHl..{e

90-x

phpbasics.

ro,.1108 npoIIIJJoro CTOJieTIUI Mbl 6bmH CBH,llereJIJIMH B3pbmoo6pa.JHOro

pa.JBHTHJI 11HTepHera, BOJHHKan sonpoc: TaK Ha qeM )Ke nHcaTb Web-npHJIO)f(eHHR?

B

TO

BpeMR He 6bmo Y.llOOHblX Jl3bIKOB nporpaMMHpoBaHHJI H sneqarnJUOIIJ)fX cl>peilMsopKoB. Ha Bb16op 6bUJO He TaK MHOro MbTepHaTHB: Java,

c H Perl. C# H nnaTcl>opMa ASP.NET nOJIBJIT-

CJI MHOro noJxce, He roBOpR )')Ke 0 COBpeMeHHblX cl>peRMBOpKax, TaKHX KaK Ruby on Rails H Django COOTBeTCTBeHHO Ha Jl3b1Kax Ruby H Python. nocne,llHHe IlOJIBHJlHCb, KOrJ],a Ha PHP y)f(e C03,llaBanHcb orpoMHbie couHanbHbie cerH spo,lle Facebook HJIH "BKoHTaKTe".

B

Te ,llaneKHe speMeHa HttrepHer ell{e He 6blJI nosce,llHeBHbIM HHCTP)'MeHTOM.

B

Hero He

BKna.zu,manHCb orpoMHble Cpe,llCTBa, npaBHJia IlOBe,lleHHJI BJia,rteJibl.{eB CaHTOB H IlOCeTHTeneR He perynuposanHCb rocy,llapCTBeHHblMH 3aKOHaMH, a caMa CeTb He 6bma ,llocrynHa 6oni.weR qacTH HaceneHHJI CTPaHbl. Cen 6onbwe HanoMHHana KJIY6 3HTY3HaCTos c )J.OXO,llaMH sblille cpe,llHero. B

rmnax

60111.WHHCT&a HHrepHer 6bm cKopee ,lloporoR urpywKoR, )1)U1 caMHX

3HTY3Hacros OH 6blJl 30HOR nocTOJIHHblX 3KcnepHMeHTOB.

B

TaKHX ycnoBHJIX npHJJo)f(eHHR

pe,llKO JKHJIH 6oni.we nonyro,lla, a pe,llHlaHHbl CaHTOB npoBO)J.HJIHCb qyTb JIH He e)f(ero)J.HO. Pa3)'MeeTCJI, He 6blJlO H MHOroMHJUIHOHHblX 61,ll)f(eTOB Ha pa3pa6oTIC)' H KOMaH,ll B HeCKOJibKO ,llecJITKOB qeJIOBeK. OpHJJmKeHHR H)')KHO 61.mo nHcaTb 61.1cTpo, uuaqe KOHKYPHposaTh s cpe)J.e 61.mo DO'ITH HeB03MOJICHO. nucaTb CGl-CKpHDTbl Ha

nucoR

arpeccHsHo«

c 61.mo npaKTil'leCKH HeOCyll{e-

CTBHMOR 3a,rtaqeR: cnumKOM HH3KoyposHesi.r« Jl3bIK, Tpe6yromH« 3HaqHTeJibHblX TPY)J.OJaTPaT H npe,lleni.HoR BHHMaTeni.Hocm.

B

61.1CTPO MeHJUOuteMCJI MHpe HHTepttera 6bmo, ecTb

H 6y,ller 0 'leM DO.ll)'MaTb, KpOMe Bbl,lleJieHHJI, B03BpaJ..QeHHJI IlaMJITH, uyneBbIX yKaJaTenJIX H nepenontteHHJI 6ycl>epa. Oo3TOMY nepsble CGI-cueuapHH 61.mu HanHcaHbl Ha Perl, HaH6onee pacnpoCTPaHeHHOM Ha TOT MOMeHT CKpHDTOBOM Jl3b1Ke. Perl H3BeCTeH CBOHMH peryMpHblMH BblpruKeHHJIMH, KOTOpble OKaJaJIHCb HaCTOJibKO y,lla'lHblMH, qro HCDOJlb3yIOTCJI BO MHO)f(eCTBe .llPyrHX ll3bIKax.

B PHP,

Kcram, MbI To:>Ke c HHMH o6llJaTeni.Ho no3HaKOMHMCJI.

116

l/acmb II.

OcHoeb1 R3b/Ka

PHP

CoJ.aaTeJJb JI3bIKa Perl Jlapp11 Yonn, JJHHrBHCT no o6pa3oBaHHJO, noCTpo11n BOKpyr perynJipHbIX BblpIDKeHHH ,llOBOJlbHO c-rpaHHblH c TO'IKH 3peHHR nporpaMMHCTa .ll3bllotte Jl3blKOBKOHKYpeHTOB. B 3TH ro.nbl Ha pbIHOK noene.noeareJJbHO Bb1xo.nnnu HOBbie HHCTpyMeHTbl Web-pa3pa6onrn, TaKHe KaK C# n m1aTit>opMa .NET Kopnopauun Microsoft, ceepxy.no6Hb1e it>peRMeopKH Ruby on Rails n Django, ceepx6bICTpbIR cepeepHbIH eapHaHT JavaScript Node.js, KoMnnnnpyeMbdi Jl3blK Go OT KOMnaHHH Google. Bb16paTb 6bmo H3 qero, u TaM, r.ne paHbUie 6e3paJ.nenhHO enaCTBOBaJJ PHP, HOBble npoeicThi see '18ll.le n 'lall.le Haqann c03.llaeaTheJI e HeilOJlb30BaHHeM .npyrnx HHCTpyMeHTOB H Jl3bIKOB, OTTJlrHBWI HHTepec pa3pa60T'IHKOB OT PHP.

C .npyroA CTOpoHbl npoH30IIIeJJ paJeopoT e o6naeTH Web-npoeKTupoeaHHJI. BMecTo He6oJJbWHX C~TOB, pa3pa6onca KOTOpblX ee.neTeJI HeCKOJlbKO MeeJ1ues H KOTOpble 3aTeM IOTJITeJI no 500 llITyK Ha eepsepe B paMKax BHpTyaJJbHOro XOCTHHra, npoeKTbl CTaJJH yKpynHJITbeJI, H eaHThl B HaCTOJlll.lHH MOMeHT pa3pa6aTbIBalOTCJl no ro.ny H 60JJbWe, pa3Mell.lalOTCJI Ha HeeKOJlbKHX .necRTKax, a HHor.na H COTHJIX cepeepoe. PHP nponrpbIBaJJ aJJbTepHaTHBHbIM rexHOJJOfHJlM H no npOH3BO)J.HTeJlbHOCTH, H no nOTpe6neHHIO naMJITH. PaJyMeeTCJI, TaKWI orpoMHw.t KOMnaHHJl, KaK Faeebook, He Morna MHpHTbCJl c noJIO)f(eHHeM Kor.na oeHOBHOA 6H3Hee, npHHOCJll.IUiii MHJIJIHap)J.bl .llOJJJiapoe, OCHOBaH Ha J13bIKe, He eoeeeM npe)lHaJHa'leHHOM ,!lJIJI eTOJlb MaCUITa6HblX npoeKTOB, KaK COQHaJJbHaJI CeTb. CTpH TOM, 'ITO C006ll.leCTBO, OTBeTCTBeHHoe 3a Jl3blK, HeCKOJlbKO eTarHauupyeT. 3aMeHa Jl3b1Ka, eepOJlTHO, He npe.neTaBJIJleT Tpyna .llJIJl He60JibWOro CaHTa, HO He .llJIJI orpOMHOR HMnepHH,

.nen,

118

l./acmb

II.

0CH06bl R3btKa

PHP

KOToprur 06CJIY'fCHBaeTC.11 MHO)ICecTBoM ,llaTa-ueHTpOB, a HHTeJIJleKT)'aJibHYJO co6cTBeHHOCTb KOTOpoA BJIO)ICeHO MHmKeCTBO lfeJioaeKO·JleT. ,nm1 rnro lfT06bI oueHHTb Macwra6 TpY.llHOCTeA, c KOTOpblMH CTOJIKttyJICJI Facebook, CJie.ll)'eT 3aMernTb, 'ITO PHP .llO He.rtaaHero apeMeHH ocTaaanc.11 HHrepnpernpyeMblM JIJbIKOM, HecMOT· p.11 Ha TO, 'ITO ero KO.rt nepaoHalfaJibHO TpaHCJIHpyeTCJI a 6aAT·KO.l{ H JaTeM abmOJIHJleTCJI, OH ace paaHo nponrp1>raaeT KOMnHJinpyeMbIM Jl3bIKaM a c1eopocrn a1>monHeHHJ1. PHP-nporpaMMa MO)ICeT ycrynaTb a CKOpOCTH 8 1000 pa3 (TpH nop.11.l{Ka) nporpaMMaM, pa3pa60TaH· HbIM Ha HJlll C++. 3TO He TaK aIDKHO, KOr.rta apeH.ll)'eTCJI He60JiblliaJI lfaCTb cepaepa .ltJl.11 o6c.lly)KHBaHHJI HeCKOJibKHX cOTeH noceTHTeneA a cynrn. Kor.rta .ltJl.11 o6CJI)')KHBaHHJI MHJlJIHOHOB noceTHTeJieA Tpe6YJOTCJI COTHH TbICJllI cepaepoa, TJ'H nOpMKa 03HalfaeT, 'ITO HX MOrJIO 6bITb nycTb He a l 000, HO ace )Ke KaK MHHHMYM a ,lleCJITKH, eCJIH He a COTHlO pa3 MeHbwe.

c

IloJTOMY, 3axBaTua pbIHOK, Facebook non1>1TaJICJ1 yeeJIH11HTb JeKTHBHOCTb pa60T1>1 co6cTaeHHbIX cepeepoe H KOMaH.ltbI pa3pa6oT'IHKOB. B ca.113H c JTHM 61>uro npe.rtnpHH.llTO HeCKOJib· Ko waroe. B nepayio oqepe.llb a 2010 ro.zzy 61>ur HalfaT npoeKT KOMilHJIJITOpa PHP-to-C++, nepeao)J)'lmero PHP-KO.lt a C++, Ha3BaHHbIH HPHPc. ,no6nawnc1> 3HalfHTeJihHoro CHff)!CeHH.11 Harpy3KH Ha co6cTBeHHhre cepeepbI, Facebook CTOJIKffYJICJI c npo6neMoA .llJIHTeJI1>Horo UHKJia pa3pa6oTKH npHJIO)ICeHHJI: KO.rt CHalfaJia pa3pa6aT1>IBaeTcJ1 Ha PHP, noToM nepeeo,llHTCJI Ha C++, .rtanee KOMnmrnpyeTCsi, 3aTeM pa3eopaqnaaeTc.11 Ha cepeepax, H ace JTO Tpe6yeT apeMeHH. IloJToMy a He.ztpax Facebook CTapTOBaJI HOBbIH npoeKT enpryanhHOit MawHHbI, KOToprur 61>1 He rpaTHJia apeMJ1 Ha KOMnHJIJIUHIO, a cpa3y BhlilOJIHJIJla 6aAT-KO.lt PHP c e1>1co1eoA J4>eK· ·THBHOCTblO. Hoehm npoeKT nonyqnn Ha3BaHHe HipHop Virtual Machine HJIH coKpameHHO HHVM. nepBWI peaJIH3aUHJI 61>ma npe.ztcTaBJleHa B 2011 fO.ll)'. HHVM, IlO.l{OOHO Java, KOH· eepTnpyeT PHP-Ko.rt a npoMe~TO'IHhIH 6aAT·KO.lt, KOTOph1it KOMnHJIHpyeTC.11 H omHMH3HpyeTcR JIT-KOMnHJIJ1TopoM a MaWHHHblA KO.rt. HHVM peaJIH3yeT MHO)ICecTBo HHJKoypoeHeBbIX omHMH3aUHH, He .rtocrynH1>1x ua ypoaHe HPHPc. IlpH'leM BHPTYMbHaJI Mawnua HHVM nocTOJIHHO coaepweucTayeTc.11. B Ho.116pe 2012 ro.rta npoH3BO.ltHTeJihHOCTb HHVM npeB3owna HPHPc. HecMOTpJI Ha CJIO)ICHOCTb peann3aunn, HHVM ee.rteT ce6si TO'IHO TaK )Ke, KaK Zend-peanHJauusi .1131>IKa H TaK )Ke .rtocrynHa .ltJl.11 JIJ06oro )ICeJiruomero. no cne.rta.M Facebook nocne.rtoeana n counaJihHaJI ceTb "BKoHTaKTe", xopowo H3BeCTHaJI poccHHCKHM nonb30BaTeJI.11M Yf HTepHeTa. B 2014 ro.zzy pa3pa6onuKH JToA counanhHoA cern npe,llCTaBHJIH -rpaucJI.llTop PHP-Ko.rta e C++, KOTOpbIA Ha3BaJIH KittenPHP HJIH coKpameHHO KPHP. Ecmt .rto JToro MOMeHTa Zend-aapuaHT PHP npe.rtcTaBJI.11JI co6oit KaHoH u CTaH.ltapT Jl3hIKa, ro c IlOJIBJieHHeM MOIUHblX KOMMepqeCKHX HrpOKOB c CHJibHblMH KOMaH.l{aMH, pecypca.MH, a rnaauoe Morn:aauueA ynyqwaTb JeKTHBHOCTb PHP, nepe.rt coo6mecTBOM acTana npo6neMa Henonuott coeMeCTHMOCTH pa3JIH'IHblX peanu3auuA PHP. TaKllM o6pa3oM, cJIO)KffJIHCb .KaK MHHHMYM .rtee 3Ha'IHM1>1e n nonyJIJ1pHb1e aeTBH Jl3bIKa PHP: Zend u HHVM. B llJOHe 2014 ro.rta Facebook ony6nuKoBaJI npoeKT cneun4>HKaUHH .11J1>1Ka PHP, KOTOpbm .rtocryneu Ha GitHub no a.rtpecy bttps://github.com/php/pbp-langspec/tree/ master/spec. 3a ocHoay B3JIT Zend-aapnaHT peanHJaunu Jl3bIKa. Bo3MO)ICHO, a 6y.llyIUeM cneun4>HK4>eKTHBHOCThlO CTaJia pa3pa6oTKa uoaoro .rtnaneKTa PHP - Hack . ..H3bIK .rto6aBJIJleT HOBble crpyKTypb1 n HHTep4>eRc1>1, a Ta!()l(e cTaTH-

rnaea 5. XapaKmepucmuKa R3bJKB PHP

119

11ecicyio THIIHJaQIDO. Co6cTBeHHO Hack -

3TO .z:tpyroA Jl3bl1C, KOTOpbdt pemaeT npo6JieMbI

60JibWOit KOpnopauHH c 60JiblDHM KOJIH'leCTBOM cepeepoe. )J;eTaJibHOe pacCMOTpeHHe Jl3b1Ka Hack BblXO.z:tHT Ja paMKH .z:tWIHoit KHHfH.

Y cnexn

counanLHDIX cereit e o6nacTH cOJ.!lWIHJI aJibTepHaTHBHbIX peanHJauHit PHP no6y.nn-

JIO C006lllecTBO K BbmycKy HOBOA, 6onee 3CJleKTHBHOit eepCHH PHP Zend Engine

3,

peJIH3 KOTOpoit COCTOJIJICJI B .nexa6pe

2015

KOMaH.1\bI pa3pa6oT11HKOB noJBOJIHJIH ycKopHTb .z:tBIDKOK PHP HHJI noTpe6neHHJI oneparneHoA naMJITH.

B

eepcnn PHP

7

7

Ha HOBOM .l{BIDKKe

ro.na. THTWIH'lecxue ycHJIHJI

7

e ,!\Ba paJa, Ha oHe CHH)l(e-

JH&'IHTeJibHO ynyqmeHa noMep)l(-

Ka 64-6HTHbIX onepaunoHHbIX CHCTeM. HMeHHO oceemeHmo 3TOit eepCHH H 6y.z:teT DOCBJimeHa KHHra.

'"ITo Hoeoro e PHP 7? Cpe.z:tH HOBHHOK PHP

0

0

7 cne.zzyeT OTMeTHTh:

HOBble onepaTOpbI H ?? (CM. 2lla8Y

7);

o6pa6oTKa OlDHOOK peanH30BaHa 11epe3 MeXaHH3M HCICJIIO'leHHit, OOMHMO cyllleCTBYJOW,ero KJiacca Exception, eee.z:teH .z:tODOJIHHTeJibHbrit KJiacc Error, xoropbrit IIOJBOJUleT nepexeaTLmaTh CTIUl,Il.apTHbie own6KH PHP

(CJw. 2lla«y 26);

0

aHOHHMHble KJiaCCbI (C.'\f. 2/laBy

0

IIOMep)l(Ka CKaJIJlpHbIX THDOB apryMeHTOB H 803Bpaw.aeMbIX 3Ha'leHHit B YHKilHJIX

23);

(CM. 2/laBy J J);

0

eee.z:teH

cneuuanbHLiit

(CM. 2/laBy

CHHTaxcnc

\u{OOFFI

.zvtll

noMep)l(KH

0

MaCCHBbl·KOHCTaHTbI (CM. 2/laBy

0

.z:tonycKaeTCll HCIIOJib30BaHHe KOHCTpYKUHH list (CM. 2.!WBY

JO)

COBMeCTHO c 061.eKTaMH,

29);

npe.nonpe.z:teJieHHbiit KJiacc IntlChar ,l\JIJI 06ecne11eHHJ1 noMep)l(KH CHMBOJIOB UTF-8 (CM. 2/IQBY

0

CHMBOJIOB

10);

peanH3YJOW,HMH HHTepeAc ArrayAccess (CM. 2/laBy

0

UTF-8

J3);

27);

KJiacc Generator, npe.z:tCTaBJUltoUJ,HA reHepaTOpbt PHP, nony'IHJI MeTo.z:t getReturn (), no3BOJIJltollJ,Hit HJBJie'lb 3Ha'leHHe, 803BpamaeMoe KmO'leBbIM CJIOBOM return (CM. 2.'IQ6)112);

0

.z:tenerupoeaHHe

reHepaTopoe

c

ucnonbJOBaHHeM

KmO'leBbIX

cnoe

yield from

(C.llf. 2/laBy J2);

0

perynJipHble Perl·Bbip8)1(eHHJI ,!\ODOJIHeHbI HOBOit YHKUHeit preg_ replace_callback_

array () (CM. 2/laBy 20);

0 0

HOBble YHKUHH random_int () (CM. 2/laBy

15) H

random_bytes () (CM. ZWBY

13);

napaMerpb1 ceccnn Tenepb MO)l(HO nepe.z:taeaTb 11epe3 Heo6llJaTeJibHbrn apryMeHT YHK· QHH session_start () (c.llf. 2/laBy

34).

00MHMO HOBOBBe.z:teHHit, 'laCTb ycTapeBWHX KOHCTpYKilHA H paCWHpeHHit 6blJIH OKOH'laTeJibHO Y.D.aJieHbt HJ PHP. Cpe.D.H HHX MO)l(HO oTMeTHTb:

0

rem

, a TaK)l(e , KOTOpbte MO)l(HO 6blJIO HCOOJibJOBaTb BMeCTO TefOB ;

aJibTepHaTHBHble

120

l/acm& II. OcHoe&1 R3btKB PHP

Ll perymi:pm.re POSIX-BblpIDKeHHJI (ereg-YHKl..lHH), BMeCTo HHX peKoMeHJzyeTCJI ucnoJibloBaTb perynJ1pHb1e Perl-ebipIDKeHJU (cM. 2!laey 20);

Ll pacrnupeHHe mysql, BMecro Hero cne.zzyeT ncnoJib30BaTb JIH6o o6'beKTHo-opHeHTHpoBaHHoe pacumpeHne mysqli, a Jiyqllle eme 6onee Hosoe pacmHpeHue PDO (CM. 2!laey 37); Ll He .n;onycKaeTCJI HCTIOJlb30BaHHe KOHCTpYKTOpa B CTHJie PHP 4, KOr.n;a HMJI MeTo.n;a coena,D;aeT c HMeHeM KJiacca (c.w. 211aey 22);

npMMep PHP-nporpaMMbl B npe.n;bmymeii rnaee MbI YifCe paccMaTpHBaJIH TpMHl..lHOHffY10 nporpaMMY "Hello, world!". B TIOJIHOM eapHaHTe OHa Bbll'JIJl,1J;HT CJie.zzyK>IUHM o6pa30M:

0,n;HaKO PHP-cueHapnR no csoeR npnpo.n;e HeCKOJibKO omH'laeTc" OT o6bfllHbIX CGIcueHapuee, KOTOpbie Mb! paccMaTpHBaJIH B t1acmu /. Ilpnee.n;eHH)'lO Bbillle nporpaMMY MO)l(HO 3anucaTb aJibTepnaTHBHbIM cnoco6oM. Cne.zzyiomnR npuMep nocTaBHT ece TOttKH HBJJ. "i". Arr" Tex, KTO eme He CTaJIKHBaJlCJI c CHHTilKCHCOM PHP, OH 6y.lleT oco6enno HHTepeceH. BOT KaK BbirJIJl.IJ;HT sropoii npttMep nporpaMMbl Ha PHP:

Hello, world!

.l(a-)la, Bbl ne oum6JIHCb - TYT .n;eHCTBHTeJibHO eoo6me HeT HHKaKHX oneparopoe PHP, H co.n.ep)ICHMoe aiina c "nporpaMMOH" COCTOHT ueJIHKOM H3 CTaTH'leCKOro TeKCTa. lJro )l(e npottcxo.n.HT? IloJIY1.faeTcJ1, 'ITO o6bfllHI>m HTML-reKcT TilK)l(e npaBHJibHO o6pa6aT1>maeTCJ1 PHP? .l(a, 3TO TaK. 0)lHaKo paccMOTpHM 1.fYTb 6onee CJIO)!Cffblii npuMep (nuCTHHr 5.1).

< ! CXX:TYPE html>

ITpocTe"llIJ.11.1 PHP-cuettapi-ri1

3.DJlaBCTByi1Te 1

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;