最新消息:

PHP函数第30款:转义字符函数_RunMagicQuotes

PHP扩展函数 杨红伟 6384浏览 0评论

一:函数简介

此函数的作用是为所有的 ' (单引号), \" (双引号), \\ (反斜线) and 空字符和以会自动转为含有反斜线的转义字符。

演示地址: http://www.ijquery.cn/phpfunction/_RunMagicQuotes/demo1.php

下载地址: http://www.ijquery.cn/phpfunction/_RunMagicQuotes/_RunMagicQuotes.zip

二:函数源码

该函数的作用指的是:

1、如果没有启用魔术引号 get_magic_quotes_gpc ,才会执行下边对数组和字符串的转义。当然,如果启用了就不使用该函数了。

2、下边遇到数组,遍历数组取值;

3、如果遇到字符串>0并且不含 cfg_|GLOBALS|_GET|_POST|_COOKIE 这些值的话就进行转义。否则退出!

//此函数的作用是为所有的 ' (单引号), \" (双引号), \\ (反斜线) and 空字符和以会自动转为含有反斜线的转义字符。
function _RunMagicQuotes(&$svar)
{
    if(!get_magic_quotes_gpc())
    {
        if( is_array($svar) )
        {
            foreach($svar as $_k => $_v) $svar[$_k] = _RunMagicQuotes($_v);
        }
        else
        {
            if( strlen($svar)>0 && preg_match('#^(cfg_|GLOBALS|_GET|_POST|_COOKIE)#',$svar) )
            {
              exit('Request var not allow!');
            }
            $svar = addslashes($svar);
        }
    }
    return $svar;
}

三:知识点学习

1、 get_magic_quotes_gpc

获取当前 magic_quotes_gpc 的配置选项设置。如果 magic_quotes_gpc 为关闭时返回 0,否则返回 1。在 PHP 5.4.O 起将始终返回 FALSE。

相关学习网址: http://php.net/manual/zh/function.get-magic-quotes-gpc.php

2、 is_array

指的是:当前字符串是否是数组。

相关学习网址: http://php.net/manual/zh/function.is-array.php

3、foreach

指的是:遍历数组

相关学习网址:http://php.net/manual/zh/control-structures.foreach.php

4、strlen

指的是:返回字符串的长度

相关滓网址: http://php.net/manual/zh/function.strlen.php

5、preg_match

指的是:执行一个正则表达式。这里是指只要遇到  cfg_|GLOBALS|_GET|_POST|_COOKIE 他们中的一个,就执行exit

相关学习网址: http://php.net/manual/zh/function.preg-match.php

6、exit

指的是:退出当前脚本

相关学习网址: http://php.net/manual/zh/function.exit.php

7、addslashes

指的是:在单引号、双引号和空字符NULL加上转义字符。

相关学习网址:http://php.net/manual/zh/function.addslashes.php


Warning: Use of undefined constant PRC - assumed 'PRC' (this will throw an Error in a future version of PHP) in /www/wwwroot/www.ijquery.cn/wp-content/themes/d8-3.0/comments.php on line 17
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址