Example 7-10. More complex form variables
<?php
if (isset($_POST['action']) && $_POST['action']
== 'submitted') {
echo '<pre>';
print_r($_POST);
echo '<a href="'. $_SERVER['PHP_SELF'] .'">Please
try again</a>';
echo '</pre>';
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'];
?>" 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="hidden" name="action"
value="submitted" />
<input type="submit" name="submit"
value="submit me!" />
</form>
<?php
}
?>
In PHP 3, the array form variable usage is limited to single-dimensional
arrays. In PHP 4, no such restriction applies.
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, sub_x and sub_y. 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.
HTTP Cookies
PHP transparently supports HTTP cookies as defined by Netscape's
Spec. Cookies are a mechanism for storing data in the remote
browser and thus tracking or identifying return users. You
can set cookies using the setcookie() 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 header() function. Cookie
data is then available in the appropriate cookie data arrays,
such as $_COOKIE, $HTTP_COOKIE_VARS as well as in $_REQUEST.
See the setcookie() manual page for more details and examples.
If you wish to assign multiple values to a single cookie
variable, you may assign it 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 MyCookie
will now be a single array in your script. If you want to
set just one cookie with multiple values, consider using
serialize() or explode() on the value first.
Note that a cookie will replace a previous cookie by the
same name in your browser unless the path or domain is different.
So, for a shopping cart application you may want to keep
a counter and pass this along. i.e.
Example 7-11. A setcookie() 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: <?php
$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.
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:
gettype(), is_array(), is_float(), is_int(), is_object(),
and is_string(). See also the chapter on Types.