Home > php教程 > php手册 > body text

php中addslashes函数与sql防注入,addslashessql

WBOY
Release: 2016-06-13 09:20:56
Original
993 people have browsed it

php中addslashes函数与sql防注入,addslashessql

本文实例讲述了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程序设计有所帮助。

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!