Home  >  Article  >  Backend Development  >  一种实用的处理表单参数和URL参数的方法,你想到了吗

一种实用的处理表单参数和URL参数的方法,你想到了吗

WBOY
WBOYOriginal
2016-06-13 12:04:11898browse

【分享】一种实用的处理表单参数和URL参数的方法,你想到了吗?

本帖最后由 xjl756425616 于 2014-08-07 11:03:45 编辑 当你正在处理很多参数提交的时候,你可能会碰到数据库查询,插入等等的操作,
这时你需要转义或者实体化
对安全不了解的phper初学者,写查询语句是这样写的
$sql="select * from demo where user='".$_POST['user']."'"";

那么就有sql注入的危险
一般有点经验的人写查询语句是这么写的
$sql="select * from demo where user='".addslashes($_POST['user'])."'"";

这样写没问题,就是
1、显得不专业
2、语句太多,每条都写,繁琐,难免遗漏

以我现在的掌握的知识再去写这条语句的话,我还是会写
$sql="select * from demo where user='".$_POST['user']."'"";

But我不是初学者,
我会在表单处理文件开头加上一句话,
$_POST=array_map("addslashes",$_POST);


实际源码示例
$_POST['user']="demo";
$_POST['content']="Hello,It's a book";
$_POST=array_map("addslashes",$_POST);
echo "
";
var_dump($_POST);
?>

打印出来的结果
array(2) {<br />  ["user"]=><br />  string(4) "demo"<br />  ["content"]=><br />  string(18) "Hello,It\'s a book"<br />}

------解决方案--------------------
所以要这样写
$a = array(<br />  'a' => "a'b",<br />  'b' => array("C'd", 123)<br />);<br />array_walk_recursive($a, function(&$v) { $v = addslashes($v); });<br />print_r($a);
Array<br />(<br />    [a] => a\'b<br />    [b] => Array<br />        (<br />            [0] => C\'d<br />            [1] => 123<br />        )<br /><br />)<br /><br />
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn