PHP添加一个反斜杠前引号(PHP去除反斜杠)

PHP添加一个反斜杠前引号(PHP去除反斜杠)
The default PHP instruction magic_quotes_gpc for server space provided by the general space quotient is on, that is, open.This time the stripslashes () function can be used to delete the automatically added backslash.The usage is: for example, if the variable that contains the string is $str, then use the stripslashes () function to process the string: stripslashes ($str), and the result of output is to remove the backslash.

如果输出输出包含反斜杠,输出的内容可以进行stripslashes()函数,即,$str = stripslashes(str),和包含在输出内容的反斜杠可以去掉。

但是还有一个问题,因为当地的PHP指令magic_quotes_gpc关闭。如果使用此功能,原有的正常反斜杠也将被删除。这不是我们想要的。

解决的办法是使用功能get_magic_quotes_gpc()检测。如果它是开放的状态,然后删除回车,如果它是一个封闭的状态,然后不去反斜杠。

程序代码如下所示:

复制代码代码如下所示:
$str = $ _post {STR}; / /读给str变量str的内容
如果((get_magic_quotes_gpc)){ / /如果get_magic_quotes_gpc(开)
$str = stripslashes(str); / /字符串处理
}
本文进行了修订,在2012年4月25日10:08:03如下:

这里有三种方法来解决这个问题:

1。修改PHP配置文件php.ini

此方法只适用于它有权管理服务器的情况。如果使用虚拟空间,则只能使用最后两种方法。

在PHP配置文件php.ini,所有magic_quotes_gpc,magic_quotes_runtime,和magic_quotes_sybase设置为关闭。如下图所示:

复制代码代码如下所示:
magic_quotes_gpc =关闭
magic_quotes_runtime =关闭
magic_quotes_sybase =关闭
2使用.htaccess文件

这种方法一般是由当前服务器只有当服务器支持.htaccess。

Add the following sentence in the.Htaccess file under the program directory.

php_flag magic_quotes_gpc关闭

3屏蔽代码

这种方法是最强大的可移植性,不考虑服务器配置,只要PHP支持,就可以使用它。

在所有php文件的开头添加以下代码

复制代码代码如下所示:
如果(get_magic_quotes_gpc()){
功能stripslashes_deep(美元值){
价值= is_array(美元值)array_map('stripslashes_deep ',美元的价值):stripslashes(价值);
返回值;
}
_post美元= array_map('stripslashes_deep,_post美元);
_get美元= array_map('stripslashes_deep,_get美元);
_cookie美元= array_map('stripslashes_deep,_cookie美元);
_request美元= array_map('stripslashes_deep,_request美元);
}

tag:反斜杠引号电脑软件php

相关内容