Promměné zvenčí PHP

HTML formuláře (GET a POST)

Když se odešle formulář do PHP skriptu , jakékoli proměnné z tohoto formuláře budou automaticky dostupné v tomto skriptu . Je-li zapnuta konfigurační volba track_vars , budou tyto proměnné umístěny v asociativních polích $HTTP_POST_VARS , $HTTP_GET_VARS , a / nebo $HTTP_POST_FILES v závislosti na zdroji proměnných .

Pro více informací o těchto proměnných si laskavě přečtěte Předdefinované proměnné .

Příklad 8-1 . Jednoduchá proměnná formuláře

 
form

 
action="

 
foo.php

 
"

 
method="post

 
"

 
Name

 
:

 
input

 
type="text

 
"

 
name="username

 
"

 
br

 
input

 
type="submit

 
"

 
/

 
form





Když se výše uvedený formulář odešle , hodnota vstupního textu bude dostupná v $HTTP_POST_VARS[ 'username' ] . Je-li zapnuta konfigurační direktiva register_globals , proměnná bude dostupná i jako $username v globálním kontextu .

You may , for example , group related variables together , or use this feature to retrieve values from a multiple select input :

Příklad 8-2 .

 
form

 
action="

 
array.php

 
"

 
method="post

 
"

 
Name

 
:

 
input

 
type="text

 
"

 
name="personal[name]

 
"

 
br

 
Email

 
:

 
input

 
type="text

 
"

 
name="personal[email]

 
"

 
br

 
Beer

 
:

 
br

 
select

 
multiple

 
name="beer[]

 
"

 
option

 
value="warthog

 
"

 
Warthog

 
option

 
value="guinness

 
"

 
Guinness

 
option

 
value="stuttgarter

 
"

 
Stuttgarter

 
Schwabenbräu

 
/

 
select

 
input

 
type="submit

 
"

 
/

 
form





IMAGE SUBMIT variable names

HTTP Cookies

Any cookies sent to you from the client will automatically be turned into a PHP variable just like GET and POST method data .

If you wish to assign multiple values to a single cookie , just add [ ] to the cookie name .

 
setcookie(

 
"MyCookie[]"

 
,

 
"Testing"

 
,

 
time()+3600)

 
;



Příklad 8-3 . SetCookie Example

 
$Count++

 
;

 
setcookie("Count"

 
,

 
$Count

 
,

 
time()+3600)

 
;

 
setcookie("Cart[$Count]"

 
,

 
$item

 
,

 
time()+3600)

 
;



Environment variables

PHP automatically makes environment variables available as normal PHP variables .

 
echo

 
$HOME

 
;

 
/

 
*

 
Shows

 
the

 
HOME

 
environment

 
variable

 
,

 
if

 
set

 
.

 
*

 
/





Since information coming in via GET , POST and Cookie mechanisms also automatically create PHP variables , it is sometimes best to explicitly read a variable from the environment in order to make sure that you are getting the right version . The getenv( ) function can be used for this . You can also set an environment variable with the putenv( ) function .

Dots in incoming variable names

 
$varname.ext

 
;

 
/

 
*

 
invalid

 
variable

 
name

 
*

 
/



Now, what the parser sees is a variable named $varname , followed by the string concatenation operator, followed by the barestring (i.e. unquoted string which doesn't match any known key or reserved words) 'ext'. Obviously, this doesn't have the intended result.

Determining variable types

PHP includes several functions which find out what type a variable is . They are gettype( ) , is_array( ) , is_float( ) , is_int( ) , is_object( ) , and is_string( ) .