Heim  >  Artikel  >  Backend-Entwicklung  >  PHP 转义使用详解_PHP教程

PHP 转义使用详解_PHP教程

WBOY
WBOYOriginal
2016-07-21 15:00:40963Durchsuche

php中数据的魔法引用函数 magic_quotes_gpc  或 magic_quotes_runtime    

设置为on时,为我们引用的数据碰到 单引号' 和 双引号" 以及 反斜线\ 时自动加上反斜线,帮我们自动转译符号,确保数据操作的正确运行
两者的区别:

magic_quotes_gpc
作用范围是:WEB客户服务端;
作用时间:请求开始是,例如当脚本运行时。

magic_quotes_runtime
作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;
作用时间:每次当脚本访问运行状态中产生的数据。

可以看出
magic_quotes_gpc的设定值将会影响通过Get/Post/Cookies获得的数据
magic_quotes_runtime的设定值将会影响从文件中读取的数据或从数据库查询得到的数据

几个想关联的函数:
set_magic_quotes_runtime():
设置magic_quotes_runtime值. 0=关闭.1=打开.默认状态是关闭的.可以通过 echo phpinfo(); 查看magic_quotes_runtime
get_magic_quotes_gpc():
查看magic_quotes_gpc值.0=关闭.1=打开
get_magic_quotes_runtime():
查看magic_quotes_runtime值。0=关闭.1=打开.
注意的是没有 set_magic_quotes_gpc()这个函数,就是不能在程序里面设置magic_quotes_gpc的值。

由于两个值的设置问题,会给编程时造成部分混淆或者会多加一次转义,针对这种情况,需要在程序开始的时候进行设置和判断,或者默认配置
这两个值都关闭。转义部分通过程序来执行。

保证数据插入数据时正常 通常会使用 addslashes 这个来处理, 数据读出时多用 stripslashes 来去掉加的反斜杠

php中类似的字符转换的函数
addslashes                         指定的预定义字符前添加反斜杠
stripslashes                        删除由 addslashes() 函数添加的反斜杠
htmlspecialchars                把一些预定义的字符转换为 HTML 实体
htmlspecialchars_decode  把一些预定义的 HTML 实体转换为字符
html_entity_decode()        把 HTML 实体转换为字符
htmlentities()                     把字符转换为 HTML 实体

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/328042.htmlTechArticlephp中数据的魔法引用函数 magic_quotes_gpc 或 magic_quotes_runtime 设置为on时,为我们引用的数据碰到 单引号' 和 双引号" 以及 反斜线\ 时自动加上...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn