函数文档

wp_magic_quotes()

💡 云策文档标注

概述

wp_magic_quotes() 函数用于为 $_GET、$_POST、$_COOKIE 和 $_SERVER 超级全局变量添加魔术引号,以增强安全性。同时强制 $_REQUEST 为 $_GET 和 $_POST 的合并数组。

关键要点

  • 为 $_GET、$_POST、$_COOKIE 和 $_SERVER 应用 add_magic_quotes() 函数进行转义处理
  • 强制 $_REQUEST 等于 $_GET 和 $_POST 的合并数组,不包含 $_COOKIE、$_SERVER 或 $_ENV
  • 如果需要访问 $_SERVER、$_COOKIE 或 $_ENV,应直接使用这些超级全局变量
  • 该函数自 WordPress 3.0.0 版本引入

代码示例

function wp_magic_quotes() {
    // Escape with wpdb.
    $_GET    = add_magic_quotes( $_GET );
    $_POST   = add_magic_quotes( $_POST );
    $_COOKIE = add_magic_quotes( $_COOKIE );
    $_SERVER = add_magic_quotes( $_SERVER );

    // Force REQUEST to be GET + POST.
    $_REQUEST = array_merge( $_GET, $_POST );
}

注意事项

add_magic_quotes() 函数在 wp-includes/functions.php 中定义,用于遍历数组并清理内容。


📄 原文内容

Adds magic quotes to $_GET, $_POST, $_COOKIE, and $_SERVER.

Description

Also forces $_REQUEST to be $_GET + $_POST. If $_SERVER, $_COOKIE, or $_ENV are needed, use those superglobals directly.

Source

function wp_magic_quotes() {
	// Escape with wpdb.
	$_GET    = add_magic_quotes( $_GET );
	$_POST   = add_magic_quotes( $_POST );
	$_COOKIE = add_magic_quotes( $_COOKIE );
	$_SERVER = add_magic_quotes( $_SERVER );

	// Force REQUEST to be GET + POST.
	$_REQUEST = array_merge( $_GET, $_POST );
}

Changelog

Version Description
3.0.0 Introduced.