you could use the same concept as above only wrap it in an easily useable function, which would be sure to worry if you have register_globals on or off...
this is no more effective than the solution provided above, but is a little more flexible than above, and requires a bit less typing on the top of each page.
to look for a variable in a posted form only you would say..
/// find username in post or set to empty string ''
$username = findVar('username','P','');
/**
* find a variable if it exists in specified location
*
* looks in specified places, in specified order, to find the. return a default if not found in specified places.
* $username = findVar('username','SP','guest');
* the above statement means: look for the varible named username in the Session, then the form POST, if not found in either place then set username to 'guest'
*
* @param $var_name - string, mandatory. The name of the variable to look for. ( for $var1 you send "var1" )
* @param $order - string, optional, default:'ECSPG', restriction: arbitrarily ordered subset of 'ECSPG'.
* The order of places to look for the variable. First Come First Serve.
* NOTE: this works in opposite order as the register_globals setting as defined in your php.ini
* 'ECSPG' = look first for variables set in the local scope, than as environmental varibles if found, return them
* look for varibale in the cookie, if found there return it
* look for variable in the session, if found there returnit
* look for variable in the form POST, if found there return it
* look for variable in the GET query, if found there return it
* if not found yet, return whatever $default is set to
* @param $default - mixed, optional, default:NULL. if not found anywhere with $order, set return this thing.
*
* @return mixed, the found variable or $default.
*/
function findVar( $var_name, $order="ECSPG", $default=NULL )
{
foreach ( range(0,(strlen($order)-1)) as $i ) {
switch ( strtoupper($order{$i}) ) {
case 'E':
if ( isset($GLOBALS) && isset($GLOBALS[$var_name]) ) {
return $GLOBALS[$var_name];
} else if ( $result = getenv($var_name) ) {
return $result;
}
break;
case 'C':
if ( isset($_COOKIE) && isset($_COOKIE[$var_name]) ) {
return $_COOKIE[$var_name];
} else if ( isset($HTTP_COOKIE_VARS) && isset($HTTP_COOKIE_VARS[$var_name]) ) {
return $HTTP_COOKIE_VARS[$var_name];
}
break;
case 'S':
if ( isset($_SESSION) && isset($_SESSION[$var_name]) ) {
return $_SESSION[$var_name];
} else if ( isset($HTTP_SESSION_VARS) && isset($HTTP_SESSION_VARS[$var_name]) ) {
return $HTTP_SESSION_VARS[$var_name];
}
break;
case 'P':
if ( isset($_POST) && isset($_POST[$var_name]) ) {
return $_POST[$var_name];
} else if ( isset($HTTP_POST_VARS) && isset($HTTP_POST_VARS[$var_name]) ) {
return $HTTP_POST_VARS[$var_name];
}
break;
case 'G':
if ( isset($_GET) && isset($_GET[$var_name]) ) {
return $_GET[$var_name];
} else if ( isset($HTTP_GET_VARS) && isset($HTTP_GET_VARS[$var_name]) ) {
return $HTTP_GET_VARS[$var_name];
}
break;
}
}
return $default;
}