Reputation: 85
I wanted to filter and then put all the values of POST and GET in a variable named after the key so I came up with this code.
foreach($_REQUEST as $key => $value){
$$key = mysql_real_escape_string(htmlspecialchars($value));
}
Then I want to use these variables inside a function? How can I do that?
funciton get_all_posts(){
//return some information from the the new variable
return $username;
return $email;
//return what ever I want to return from POST/GET using the new variables
}
echo get_all_posts();
Upvotes: 0
Views: 113
Reputation: 1435
I'll personal suggest to use predefined array indexed with key
$request_array=array('username'=>'','password'=>'');
as it will be easy to handle with new added variable(which is not planned at first). However you have another option to do this by class.
class stackoverflow{
public $username;
function __construct($mysqli){}
//some function filter and store $this->username=$mysqli->real_escape_string($value);
//some function able to use $this->username;
}
$request=new stackoverflow($mysqli);
$request->filter_request();
$request->get_all_post();
Upvotes: 0
Reputation: 4656
I give you an example
function security ( &$data) {
return is_array( $data ) ? array_map('security', $data) : mysql_real_escape_string(htmlspecialchars( $data, ENT_QUOTES ));
}
$_REQUEST['s'] = '"Hello"';
$_REQUEST['y'] = 'World\'s';
$_REQUEST = security( $_REQUEST );
print_r( $_REQUEST );
function get_all_posts() {
extract($_REQUEST);
//return some information from the the new variable
return $s;
return $y;
//return what ever I want to return from POST/GET using the new variables
}
echo get_all_posts();
Upvotes: 0
Reputation: 4142
no need to pass these information just refine like this and use as it is
foreach($_REQUEST as $key => $value){
$_REQUEST[$keys] = mysql_real_escape_string(htmlspecialchars($value));
}
Upvotes: 3
Reputation: 136
you can directly use.
extract($_REQUEST);
then directly use $username; inside your function
Thanks, Divyang
Upvotes: 0
Reputation: 1792
you can use something like this
$array = array();
foreach($_REQUEST as $key => $value){
$array[$key] = mysql_real_escape_string(htmlspecialchars($value));
}
funciton get_all_posts($arr){
//return some information from the the new variable
// you can use $arr inside function
return $username;
return $email;
//return what ever I want to return from POST/GET using the new variables
}
echo get_all_posts($array);
I hope it will help
Upvotes: 0