search
Homephp教程php手册php addslashes 函数详细分析说明_php基础

语法: string addslashes(string str);
内容说明
本函数使需要让数据库处理的字符串中引号的部份加上斜线,以供数据库查询 (query) 能顺利运作。这些会被改的字符包括单引号 (')、双引号 (")、反斜线 backslash (\) 以及空字符 NUL (the null byte)。
================================================================
1,表单提交中addslashes的表现。
首先要看get_magic_quotes_gpc()的值,一般为 1 。这时候从

echo get_magic_quotes_gpc().
" A ".$_POST['message'].
" B ".stripslashes($_POST['message']);
?>


输入:include('/home/me/myfile');
输出:1 A include(\'/home/me/myfile\'); B include('/home/me/myfile');
总结:get_magic_quotes_gpc()等于1的情况下,如果不输入数据库,那你得到的结果是加了斜线的。
2,提交输入数据库时addslashes的表现。
例子:
PHP代码:
复制代码 代码如下:

test




require_once('includes/common.php');
$db->query("INSERT INTO `testtable` ( id , content ) VALUES ('1' , '".$_POST['message']."')");
$query=$db->query("select * from `testtable` where `id`= 1;");
$Result=$db->fetch_array($query);
echo get_magic_quotes_gpc().
" A ".$_POST['message'].
" B ".$Result['content'];
?>


输入:include('/home/me/myfile');
输出:1 A include(\'/home/me/myfile\'); B include('/home/me/myfile');
总结:get_magic_quotes_gpc()等于1的情况下,如果输入数据库后,再从数据库直接读取的时候,你不做任何修改就可以得到输入的字符串。
3, get_magic_quotes_gpc()
get_magic_quotes_gpc()在服务器是的设置是不能runtime修改的,也就是说,你必须在你的网页代码中预先考虑好不同的情况,不然,当你提交数据的时候,你还不知道服务器给你加了斜线没有。以下两个网上流行的函数可能是大家需要的,个人喜欢第二个:
PHP代码:
复制代码 代码如下:

function my_addslashes( $message ){
if(get_magic_quotes_gpc()== 1 ){
return $message;
}else{
if(is_array($message)==true){
while(list($key,$value)=each($message)){
$message[$key]=my_addslashes($value);
}
return $message;
}else{
return addslashes($message);
}
}
}

PHP代码: 
复制代码 代码如下:

function my_addslashes($data){
if(!get_magic_quotes_gpc()) {
return is_array($data)?array_map('AddSlashes',$data):addslashes($data);
} else {
Return $data;
}
}

简单的解释就是,如果get_magic_quotes_gpc()等于 1 (服务器默认设置为 1 ),那我们的字符串是可以直接入库的,不修改。不然,我们才用addslashes函数。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function