博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

PHP magic_quotes

Posted on 2013-02-19 18:31  PHP-张工  阅读(438)  评论(0编辑  收藏  举报

PHP 对客户端输入进行输入转义

// 对 用户提交的数据 ' " \ 进行转义
if ( get_magic_quotes_gpc() )
{
    function del_magic_quotes($value)
    {
        $value = is_array($value) ? array_map('del_magic_quotes', $value) : stripslashes($value);

        return $value;
    }	

    $_POST    = del_magic_quotes( $_POST );
    $_GET     = del_magic_quotes( $_GET );
    $_COOKIE  = del_magic_quotes( $_COOKIE );
    $_REQUEST = del_magic_quotes( $_REQUEST );
}

function add_magic_quotes( $array ) {
	foreach ( (array) $array as $k => $v ) {
		if ( is_array( $v ) ) {
			$array[$k] = add_magic_quotes( $v );
		} else {
			$array[$k] = addslashes( $v );
		}
	}
	return $array;
}

$_GET     = add_magic_quotes( $_GET );
$_POST    = add_magic_quotes( $_POST );
$_COOKIE  = add_magic_quotes( $_COOKIE );
$_REQUEST = add_magic_quotes( $_REQUEST );