Php 过滤所有get/post并使用一些信息
我想过滤,然后将POST的所有值放入一个以键命名的变量中,所以我产生了这段代码Php 过滤所有get/post并使用一些信息,php,function,Php,Function,我想过滤,然后将POST的所有值放入一个以键命名的变量中,所以我产生了这段代码 foreach($_REQUEST as $key => $value){ $$key = mysql_real_escape_string(htmlspecialchars($value)); } 那么我想在函数中使用这些变量?我该怎么做 funciton get_all_posts(){ //return some information from the the new variable
foreach($_REQUEST as $key => $value){
$$key = mysql_real_escape_string(htmlspecialchars($value));
}
那么我想在函数中使用这些变量?我该怎么做
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();
你可以用这样的东西
$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);
我希望这会有帮助,你可以用这样的东西
$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);
我希望它能帮助你直接使用
提取($\u请求)
然后直接使用$username;在你的职能范围内
谢谢,
Divyang您可以直接使用
提取($\u请求)
然后直接使用$username;在你的职能范围内
谢谢,
Divyang无需传递这些信息,只需像这样进行优化并按原样使用即可
foreach($_REQUEST as $key => $value){
$_REQUEST[$keys] = mysql_real_escape_string(htmlspecialchars($value));
}
无需传递这些信息,只需像这样细化并按原样使用即可
foreach($_REQUEST as $key => $value){
$_REQUEST[$keys] = mysql_real_escape_string(htmlspecialchars($value));
}
我给你举个例子
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();
我给你举个例子
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();
我个人建议使用带有索引键的预定义数组
$request_array=array('username'=>'','password'=>'');
因为它将很容易处理新添加的变量(这不是最初计划的)。但是,您还有另一个选项可以按类执行此操作
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();
我个人建议使用带有索引键的预定义数组
$request_array=array('username'=>'','password'=>'');
因为它将很容易处理新添加的变量(这不是最初计划的)。但是,您还有另一个选项可以按类执行此操作
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();
为什么不直接使用数组呢?现在你只是在复制相同的VAR?仅供参考,如果你确切知道自己在做什么,中央卫生功能是可以的,但很有可能你正在以不必要的方式破坏数据。相关:在函数中使用参数,比如get_all_posts($array),为什么不按原样使用数组呢?现在你只是在复制相同的VAR?仅供参考,如果你确切知道自己在做什么,中央卫生功能是可以的,但很有可能你正在以不必要的方式破坏数据。相关:在函数中使用参数,比如get_all_posts($array),这太傻了。。正在创建新数组以将其他数组的数据存储在。。。浪费资源imho@DarkBee您可以使用直接请求,比如get_all_posts($_request)@DarkBee我们使用了另一个数组,因为Keith使用了mysql\u real\u escape\u string这太傻了。。正在创建新数组以将其他数组的数据存储在。。。浪费资源imho@DarkBee你可以使用直接请求,比如get_all_posts($_request)@DarkBee我们使用了另一个数组,因为Keith使用了mysql\u real\u escape\u stringhehe你不能在函数中使用直接变量过滤器会发生什么?hehe你不能在函数中使用直接变量过滤器会发生什么?