PHP mobile book.
(D.Koterov by KMX.RU edition)

 PHP   ,
            Web. 
    99% ,     -
.   1%     Perl (  -
 ).

1. CGI-.

1.1.    GET

  ,   ,      
somestring   <Enter>.     somestring?
, .   .   ,     -
   (   ,      ), 
  Web-   :    -  -
:
GET somestring HTTP/1.0\n
... ...
\n\n
 \n    ,  \n\n     -
 ,      (,  -
 ).      ,     
.
 ,  GET-       , -
   .    .  
  (headers),      .  HTTP
         .
       HTTP.  ,   -
   ,    ,    
,       POST,    .
         -
     .  
    ,   -
 (, -)   .  
    (       -
)      ,   .
     CGI-,     
   .  ,    -
  ,        -
  (     ).
     .

GET
- : GET ? HTTP/1.0
-  : REQUEST_URI;   QUERY_STRING 
 ,   REQUEST_METHOD    GET.

POST
- : POST ? HTTP/1.0
-  : REQUEST_URI;   QUERY_STRING 
 ,   REQUEST_METHOD   POST.
       POST.   -
   ,    ,      GET
,         ,    
 .

Content-type
- : Content-Type: application/x-www-form-urlencoded
- : CONTENT_TYPE
     .    -
  application/x-www-form-urlencoded,   , 
    (  -   -
)   .     , -
   GET  POST.     ,
   multipart/form-data.   ,   
,     .
     ,      -
 ,        .

User-Agent
- : User-Agent: Mozilla/4.5 [en] (Win95; I)
-  : HTTP_USER_AGENT
   (    Netscape Navigator).

Referer
- : Referer: URL_
-  : HTTP_REFERER
 ,       URL , 
       . , 
  ,       
 (,       ), , 
      Referer.

Content-length
- : Content-length: 
-  : CONTENT_LENGTH
  ,     
 ,   POST.    GET,   -
 ,  ,    .

Cookie
- : Cookie: _Cookies
-  : HTTP_COOKIE
   Cookies  URL- ( Cookies    
 ).

Accept
- : Accept: text/html, text/plain, image/gif, image/jpeg
-  : HTTP_ACCEPT
    ,     "". -
   .  ,      -
         */*,  
 .
     (    
 HTTP 1.1),       .

1.2.  POST

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

   ,       -
 .  ,       \n\n, 
    ,    Test!  5    .
 ,      POST- (  ,
     ),    
.     ,   , . .   -
  ,   ?     -
  Content-Length,      . 
     .
   POST?    ,    
. ,     Web (. )   
 .  ,  POST     :
  ,    GET,  , ,  , URL 
    ,  POST-  URL  -
.

2. CGI 

        CGI.    
,        .    -
    CGI- (CGI-),    
?         ,  -
  ?

2.1.   

    :      (
 ) .
      ( ,     -
 ):       
 (    stdout),     -
  .  ,   ,   
 ,       "". (   ,
         
. ,     ""     .)
 ,     ,    . 
,          : 
     ,      
 . , ,  ,  
  GIF-   ?     -
            
    .
 
        ,    -
,     .  ,    (-
  ),        
 ,  .      
       \n\n. , 
,    ,    , 
  .

2.2.   

        ,    -
 .   ,        -
        .      -
   , , :
HTTP/1.1 OK
 :
HTTP/1.1 404 File Not Found
     ,       
 .    ,     
  . ,      ,  
     ,   .
  (   )     -
    ,       . 
,    ,        
 (,    Status,       
).           .
     .

Content-type

- : Content-type: mime_; charset=koi8-r
     .  charset   -
 (    KOI8-R).  mime_   ,
  :
- text/html  HTML-;
- text/plain    ;
- image/gif  GIF-;
- image/jpeg  JPG-;
-     .

Pragma

: Pragma: no-cache
   ,      
     ,      .
   ,   , ,  
.
 Pragma       ( , 
    ),       -
.

Location

: Location: http://www.otherhost.com/somepage.html
    ,     -
    ,      (  
       URL).  , ,  
   Location,     
.

Set-cookie

: Set-cookie: _cookie
 Cookie   .      
,   Cookies     .

Date

: Date: Sat, 08 Jan 2000 11:56:26 GMT
    .

Server

: Server: Apache/1.3.9 (Unix) PHP/3.0.12
          
  .

2.3.   CGI-

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

2.3.1.  

HTTP_ACCEPT
     (  ,    -
) MIME- ,     .   
,        */*,  -
,      .

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

HTTP_USER_AGENT
  .      -
  MSIE,    Internet Explorer,   ,   -
   Mozilla,  Netscape.

HTTP_HOST
  Web-,    .   -
   , ,    , -
    Location,      
( ,     ""    
 ,    ,          -
).

SERVER_PORT
  ( 80),     .  
     Location.

REMOTE_ADDR
    IP- (  )  , 
   .

REMOTE_PORT
,         .

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

REQUEST_METHOD
,       ( 
 GET  POST,     ).  ,  
        , 
  ,      -
,   ,    , ,  
POST. ,  PHP-   .

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

CONTENT_LENGTH
  ,  .   
,       POST-.

2.3.2. 

          3 :
- <input...>
- <textarea...>...</textarea>
- <select...><option...>...</option>...</select>
   , ,   .   ,  
=  ,   ,     -
  &.  ,  ,     ,
      name,   =
  .     ,     
  ,     . ,
     (   ,  
  JavaScript)  . . ,  :

<form action=script.cgi>
... -  ...
<input type=submit value="Go!">
</form>

  ,   Go!    ,    
 ,      name.
        . ,  , 
,           
-  (,    URL  ).  
:

<form action=script.cgi>
<input type=submit name="submit" value="Go!">
</form>

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

 <input>    

    ,   type. -
    .      
 ,   ,    
 ( ,   name  ,      
  ).        
!
         .
   HTML   ,     
    .      
,      .

  (text)

<input type=text
name=
[value=]
[size=]
[maxlen=]
>

       size    -
  maxlen  (       
   ).
    value,       -
  .

   (password)

<input type=password
name=
[value=]
[size=]
[maxlen=]
>

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

   (hidden)

<input type=hidden
name=
value=
>

  () .     
,    -  ,   -
  ,  ,  .
, ,     CGI-,   -
        .  
    E-mail  ,        -
,        ,  -
     :

<form action=/cgi/sendmail.cgi method=post>
<input type=hidden name=email value="admin.microsoft.com.">
<h2>  :</h2>
<input type=text name="text">
<input type=submit name=doSend value="">
</form>

  (checkbox)

<input type=checkbox
name=
value=
[checked]
>

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

  (radio)

<input type=radio
name=
value=
[checked]
>

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

   (submit)

<input type=submit
[name=]
value=_
>

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

   (reset)

<input type=reset
value=_
>

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

    (image)

<input type=image
[name=]
src=
>

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

 <textarea>     

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

<textarea
name=
[width=][height=]
[wrap=]
>

,       </textarea>
  ,     .  width  -
    ,  height   .  wrap , 
     .        ( -
  none).
- Virtual     .     
 ,  ,   ,   -
        ,   -
  .         
 <Enter>.
- Physical     ,     none.
- None      ,   .    -
   ,    (  ,  -
).
   ,   , ,  , -
  =,     
<input type=text>.

 <select>  

      <select>.     (
) .        .
   :

<select name= [size=] [multiple]>
<option [value1=1][selected]>1</option>
<option [value2=2][selected]>2</option>
. . .
<option [valueN=N][selected]>N</option>
</select>

 ,       .  ,  -
    <option>,     .
 size ,     .  size  1,  -
  ,      size    
.    multiple,      -
   ,     .  ,  multiple
     .
      <option>.     
value,    ,      -
  ,    ,      (,
 value   ).  ,    selected,  -
    . ,   :  
</option>  ,       
HTML (      ).
  ,       .
,           
=,      <select>,     -
  ( ,   value,     ).

   (multiple)

     ,    multiple-? 
:   ,    ,   -multiple-,  
 ,         . 
,  ,   ,   
:

=1&=2&...&=N

 ,      ,     
  . ,       
   .   , ,  -
:

<input type=checkbox name= value=""><br>
<input type=checkbox name= value=""><br>
<input type=checkbox name= value=""><br>

      ,    
(,  URL- ):

=&=&=

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

 

         -
      ,    -
  ,       -
  .     HTML   .
  .

2.3.3.  

    ,           -
      ?, &  =.  ,   
  ,   ,     
 . ,     , 
 ,   URL-     "" 
    (,     URL- -
  %00).         .  , -
         
 ,   ,      .
       ?  &.  , ,  
         POST,
   GET.         ,  -
   GET   ...
   ,   -     
,     <form>  
enctype=multipart/form-data. ( ,     , 
  ,   enctype=application/x-www-formurlencoded
       .) 
 ,    ,     
 (    ).     
HTTP- "-",     -
.     (\n,  ,    -
):

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

:

<form action=... enctype=multipart/form-data method=post>
Name: <input type=text name="Name" value=" "><br>
Box: <input type=checkbox name="Box" value=1 checked><br>
Area: <input type=textarea name="Area"> - </textarea><br>
<input type=submit>

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

,      (   
_)   .  ,   -
      ,   . -
,        .     
. ,  ,     ,  -
, ,  .    ,    -
,      (    
  \n).

     :      -
   ,   ="-" (  -
  Content-Disposition),       
      ,   
 \n (   ,     ).  -
,   .

   (file)

   ,       .  , 
    ,       -
       Browse .    -
 <input>:

<input type=file
name=_
[value=_]
>

   -  (,   \
_)    .       
     :

----------------127462537625367\n
Content-Disposition: form-data; name="_";
A filename="\_"\n \n
........
     .
    
   .
........
\n

 ,            
 ( filename).
 , ,      .
,         : -
,          (  
,      ,  
 ).       PHP,     -
    ,      -
  .

2.3.4.   Cookies     

Cookie      ,       (
  , !),    ( , )    -
 . ,     Cookies,   
 ,    ,        
 ,      .    Cookies
 ,          ,    
         (   
  Cookie).  ,    Cookies,  ""
   (   ,    ),   
"".

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

 Cookie

   :      Cookie   -
?    "   ",     .
  :   Cookie      -
 ,   .  ,    
Content-type,        Cookie. -
     (,    , -
    ):
Set-Cookie: name=value; expires=; domain=_; path=; secure
     Cookie    HTML-
<meta>. ,      ,   -
 Cookie.   :

<meta http-equiv="Set-Cookie"
content="name=value; expires=; domain=_; path=; secure"
>

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

    Cookie:

name

     ,   Cookie.   
URL- , . .    - .
,    Cookies   ,   URL-
    .

value

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

expires

  expires=     Cookie. ,
Cookie ,     . ,  
expires=Friday,31-Dec-99 23:59:59 GMT,  ""  ""  
31  1999 . ,     : ,   
  "" Cookie.         -
  (,   ,  Cookie  10   
,       )?  -
 ,        
. ,     ,     
    ,   ,    .

domain

 domain=_   ,    Cookie. 
     .  , ,    ,
   ,    "" Cookie  .
   ,    ,     -
.

path

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

secure

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

 Cookies  

 Cookies    :      -
 HTTP_COOKIE    ,   QUERY_STRING,   &
 ;. ,     Cookies: cookie1=value1 
cookie2=value2,     HTTP_COOKIE  :
cookie1=value1;cookie2=value2.
    ,       
.

2.3.5. 

  ,   - URL    
.  ,  ,      (login) 
 (password).        
  .
           .
-,     ,  
  . -,    CGI-
  ,    ,  
    ( ) , , 
,   (  ,    
  ,      ).
 , -,       -
   PHP,         -
  .        -
    .
   ,          
    basic-. , ,  
    :

WWW-Authenticate: Basic realm="_"
HTTP/1.0 401 Unauthorized"

   ,        
 .    .  _     
 ,   ,     -
   .   CGI-
        
 ()   ,    
(   ),         
.
,  ,    ( ,    
   ,       (. )
 Cancel, . .  ).      : 
     ,   -
  login  password.      ,  -
  ,      ( -
 )   :

Authorization: Basic TG9naW46UGFzcw==

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

Authorization: Basic _Cookie

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