Entry
How do I pass multiple variable in the URL String?
Dec 13th, 2002 00:53
Philip Olson, Kris Erickson, Corky Williams, http://www.php.net/variables.external
The most common forms of receiving GET information from an URL is as
follows:
http://www.example.com/test.php?fruit=apple&type=sauce&id=402
In this case, the first argument seperator is a '?' followed by a
series of '&' This is configurable, instead of '&' some use a semi-
colon ';', and using '&' isn't uncommon either. See the
arg_seperator setting in your php.ini for more information.
There are a few ways to get this information many of which depend on
your particular servers settings (see php.ini). If test.php in our
example above made a call to the phpinfo() function, we'd see the
information in various formats. For example:
a) If track_vars = on
- ALL php versions since 4.0.3 have this setting on, most
versions before did too.
print $HTTP_GET_VARS['fruit']; // apple
print $HTTP_GET_VARS['type']; // sauce
print $HTTP_GET_VARS['id']; // 402
As of PHP 4.1.0 the preferred Autoglobals / Superglobals
became available and are:
print $_GET['fruit'];
print $_GET['type'];
print $_GET['id'];
Note that the above are available regardless of your
register_globals setting.
b) If register_globals = on
- Is on by default on versions before 4.2.0 and you are
encouraged to NOT rely on or use this method.
http://www.php.net/manual/en/configuration.php#ini.register-globals
http://www.php.net/manual/en/security.registerglobals.php
print $fruit; // apple
print $type; // sauce
print $id; // 402
c) Another predefined variable is QUERY_STRING
// This prints: fruit=apple&type=sauce&id=402
print $_SERVER['QUERY_STRING'];
Those are the most common methods. You may want to encode your data
before passing it to the URL (like if it includes =, &, %, + or
other 'unsafe' characters). For example:
$var = '++ I'll be < %encoded% > okay? ++';
$id = 402;
<?php
$url = '<a href="http://www.example.com/test.php';
$url .= '?var='. urlencode(htmlspecialchars($var));
$url .= '&id='. $id;
$url .= '">Pass a couple variables through GET method!</a>';
print $url;
?>
Dealing with "encoding" is beyond the scope of this FAQ. See the
following for more information:
http://www.php.net/manual/en/function.urlencode.php
http://www.php.net/manual/en/function.htmlspecialchars.php
http://www.jmarshall.com/easy/http/http_footnotes.html#urlencoding
http://www.faqs.org/rfcs/rfc2396.html
Also see:
http://www.php.net/manual/en/language.variables.predefined.php
http://www.php.net/manual/en/language.variables.external.php