note 44902 deleted from function.import-request-variables by tomsommer
| From: | tomsommer@php.net | Date: | Thu, 19 Aug 2004 19:46:09 +0000 |
| Subject: | note 44902 deleted from function.import-request-variables by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74951@lists.php.net to get a copy of this message | ||
Note Submitter: davidb at mnl dot com
----
I missed the parameterized requests from HTML::Mason in perl, so hacked up my own equivalent; then
basically at the top of pretty much every page i just say:
args('blah?', 'username', '=password');
something like that. The docs are in the comments at the top:
## assigns various REQUEST vars to global vars
## arguments are a variable number of strings, each of which will be turned
## into a global variable (e.g. 'foo' => $foo (or $GLOBALS['foo']))
## arguments are assumed to be required to exist, unless they are followed
## by a ? (e.g. 'foo?' is 'foo' optionally (still 'foo' => $foo))
## if the argument is preceeded with an = (e.g. '=foo' or '=foo?'), then
## it will not be trim()'ed before the variable is set
##
## you may also pass all of the arguments as a single array
function args() {
$keys = func_get_args();
if (count($keys) == 1 && is_array($keys[0])) $keys = $keys[0];
foreach ($keys as $key) {
if (substr($key, 0, 1) == '=') {
$key = substr($key, 1);
$trim = false;
} else $trim = true;
if (substr($key, -1) == '?') {
$key = substr($key, 0, strlen($key) - 1);
$GLOBALS[$key] = (array_key_exists($key, $_REQUEST))
? $_REQUEST[$key] : NULL;
} else {
if (!array_key_exists($key, $_REQUEST))
die("Missing required argument for this page: '$key'");
$GLOBALS[$key] = $_REQUEST[$key];
}
if ($trim && !is_array($GLOBALS[$key]) && !is_null($GLOBALS[$key]))
$GLOBALS[$key] = trim($GLOBALS[$key]);
}
}