PHP.nl

Variables From External Sources

Variables From External Sources

HTML Forms (GET and POST)

When a form is submitted to a PHP script, the information from that form is automatically made available to the script. There are few ways to access this information, for example:

**Voorbeeld: A simple HTML form**
<form action="foo.php" method="post">
    Name:  <input type="text" name="username" /><br />
    Email: <input type="text" name="email" /><br />
    <input type="submit" name="submit" value="Submit me!" />
</form>

There are only two ways to access data from HTML forms. Currently available methods are listed below:

**Voorbeeld: Accessing data from a simple POST HTML form**
<?php
echo $_POST['username'];
echo $_REQUEST['username'];
?>
 Using a GET form is similar except the appropriate
 GET predefined variable can be used instead. GET also applies to the
  (the information after the '?' in a URL). So,
 for example, 
 contains GET data which is accessible with .
 See also .
`QUERY_STRING``http://www.example.com/test.php?id=3``$_GET['id']``$_REQUEST`

Opmerking: > Dots and spaces in variable names are converted to underscores. For example becomes . &lt;input name="a.b" /&gt;``$_REQUEST["a_b"]

 PHP also understands arrays in the context of form variables 
 (see the ).
 For example, related variables may be grouped together, or this
 feature may be used to retrieve values from a multiple select input. For
 example, let's post a form to itself and upon submission display 
 the data:
related faq


 
**Voorbeeld: More complex form variables**
<?php
if ($_POST) {
    echo '<pre>';
    echo htmlspecialchars(print_r($_POST, true));
    echo '</pre>';
}
?>
<form action="" 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>
        <option value="guinness">Guinness</option>
        <option value="stuttgarter">Stuttgarter Schwabenbräu</option>
    </select><br />
    <input type="submit" value="submit me!" />
</form>

Opmerking: > If an external variable name begins with a valid array syntax, trailing characters are silently ignored. For example, becomes . &lt;input name="foo[bar]baz"&gt;``$_REQUEST['foo']['bar']

IMAGE SUBMIT variable names

When submitting a form, it is possible to use an image instead of the standard submit button with a tag like:

<input type="image" src="image.gif" name="sub" />
  When the user clicks somewhere on the image, the accompanying
  form will be transmitted to the server with two additional
  variables,  and .
  These contain the coordinates of the
  user click within the image. The experienced may note that the
  actual variable names sent by the browser contains a period
  rather than an underscore, but PHP converts the period to an
  underscore automatically.
 `sub_x``sub_y`

HTTP Cookies

 PHP transparently supports HTTP cookies as defined by . Cookies are a
 mechanism for storing data in the remote browser and thus
 tracking or identifying return users. It is possible to set cookies using
 the  function. Cookies are part of
 the HTTP header, so the SetCookie function must be called before
 any output is sent to the browser. This is the same restriction
 as for the  function. Cookie data
 is then available in the appropriate cookie data arrays, such
 as  as well as in .
 See the  manual page for more details and 
 examples.
RFC 6265`setcookie``header``$_COOKIE``$_REQUEST``setcookie`

Opmerking: > As of PHP 7.2.34, 7.3.23 and 7.4.11, respectively, the of incoming cookies are no longer url-decoded for security reasons. names

If multiple values should be assigned to a single cookie variable, they can be assigned as an array. For example:

<?php
  setcookie("MyCookie[foo]", 'Testing 1', time()+3600);
  setcookie("MyCookie[bar]", 'Testing 2', time()+3600);
?>
 That will create two separate cookies although  will now 
 be a single array in the script. If just one cookie should be set
 with multiple values, consider using  or
  on the value first.
`MyCookie``serialize``explode`

Note that a cookie will replace a previous cookie by the same name in the browser unless the path or domain is different. So, for a shopping cart application a counter may be kept, and passed along. I.e.

Voorbeeld: A example

<?php
if (isset($_COOKIE['count'])) {
    $count = $_COOKIE['count'] + 1;
} else {
    $count = 1;
}
setcookie('count', $count, time()+3600);
setcookie("Cart[$count]", $item, time()+3600);
?>

Dots in incoming variable names

 Typically, PHP does not alter the names of variables when they
 are passed into a script. However, it should be noted that the
 dot (period, full stop) is not a valid character in a PHP
 variable name. For the reason, look at it:
 
 Now, what the parser sees is a variable named
 , 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.
```php

`$varname`

For this reason, it is important to note that PHP will
     automatically replace any dots in incoming variable names with
     underscores.

### Determining variable types


     Because PHP determines the types of variables and converts them
     (generally) as needed, it is not always obvious what type a given
     variable is at any one time. PHP includes several functions
     which find out what type a variable is, such as:
     , ,
     , ,
     , and
     . See also the chapter on
     .
    `gettype``is_array``is_float``is_int``is_object``is_string`Types


     HTTP being a text protocol, most, if not all, content that comes in
     , 
     like  and  will remain
     as strings. PHP will not try to convert values to a specific type.
     In the example below,  will contain the
     string "null" and , the string "123".
     
    Superglobal arrays`$_POST``$_GET``$_GET["var1"]``$_GET["var2"]````php
/index.php?var1=null&var2=123