note 60234 added to language.variables.predefined
| From: | jon at fuck dot org | Date: | Fri, 30 Dec 2005 19:45:23 +0000 |
| Subject: | note 60234 added to language.variables.predefined | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101004@lists.php.net to get a copy of this message | ||
this is a handy suite of functions i wrote which manually register globals with callback functions
for controlling user input.
<?php
/**register a key from a superglobal as a global variable in the current context.
* @param $name string key to superglobal.
* @param $type string typename to cast the value to.
* @param $default mixed default value to set for new global.
* @param $super &array reference to superglobal of choice.
* could be any array with keys, though.
* @param $callbacks array set of functions to modify the data
* from the superglobal after registering.
* @return boolean true if key found in superglobal, false otherwise.
* note that the global is registered in either case.
*/
function register_global($name,
$type = 'string',
$default = null,
&$super,
$callbacks = null)
{
global ${$name};
// set default value
${$name} = $default;
if (isset($super[$name]))
{
// get data from superglobal
${$name} = $super[$name];
// typecast
settype(${$name}, $type);
// normalize with callback functions
if (is_array($callbacks))
foreach($callbacks as $func)
${$name} = call_user_func($func, ${$name});
// default overridden.
return true;
}
// default value was set.
return false;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* example callback functions to be used with register_global().
*/
// use only the first token of the input string; assume the
// string might be urlencoded (a better way to do this is
// obviously a regex).
//
function rg_tok($input)
{
return strtok($input, ' +');
}
// enforce string is lowercase.
//
function rg_lcase($input)
{
return strtolower($input);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* example usage of register_global():
*/
// set $foo to $_GET['foo'] or 42 if not specified.
register_global('foo', 'integer', 42, $_GET);
echo $foo;
?>
----
Manual Page -- http://www.php.net/manual/en/language.variables.predefined.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60234
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60234&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60234&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60234&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60234&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60234&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60234&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60234&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60234&report=yes
Search -- http://master.php.net/manage/user-notes.php