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

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

WBOY
Release: 2016-06-13 12:04:11
Original
945 people have browsed it

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

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

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

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

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

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


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

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

------解决方案--------------------
所以要这样写
$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);
Copy after login
Array<br />(<br />    [a] => a\'b<br />    [b] => Array<br />        (<br />            [0] => C\'d<br />            [1] => 123<br />        )<br /><br />)<br /><br />
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template