Home  >  Article  >  Backend Development  >  php中addslashes函数与sql防注入_PHP

php中addslashes函数与sql防注入_PHP

WBOY
WBOYOriginal
2016-05-31 19:27:47848browse

本文实例讲述了php中addslashes函数与sql防注入。分享给大家供大家参考。具体分析如下:

addslashes可会自动给单引号,双引号增加\\\\\\,这样我们就可以安全的把数据存入数据库中而不黑客利用,参数'a..z'界定所有大小写字母均被转义,代码如下:

代码如下:

echo addcslashes('foo[ ]','a..z'); //输出:foo[ ]
$str="is your name o'reilly?"; //定义字符串,其中包括需要转义的字符
echo addslashes($str);  //输出经过转义的字符串


定义和用法:addslashes() 函数在指定的预定义字符前添加反斜杠.

这些预定义字符是:单引号 ('),双引号 ("),反斜杠 (),null

语法:addslashes(string),当然这个函数更安全,实例代码如下:

代码如下:

$str="test"; //定义包含特殊字符的字符串
$new=htmlspecialchars($str,ent_quotes);  //进行转换操作
echo $new;           //输出转换结果
//不过输出时要用到
$str="jane & 'tarzan'";  //定义html字符串
echo html_entity_decode($str);   //输出转换后的内容
echo "
";
echo html_entity_decode($str,ent_quotes); //有可选参数输出的内容

希望本文所述对大家的PHP程序设计有所帮助。

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