首页 > 解决方案 > PHP 5.4+ 替代 set_magic_quotes_runtime

问题描述

我想从 PHP 5.4 迁移到 PHP 7... 所以我检查了 PHP 5 和 PHP 7 之间的兼容性。我发现我的代码使用的函数已被弃用:get_magi_quotes_gpc_runtime()

由于文档不推荐任何替代方案,还有什么替代方案?

标签: php

解决方案


php7 等价物是:

$magic_quotes=false;

不再支持 magic_quotes。这从来都不是一个好主意,应尽可能避免。尽管如此,由于magic_quotes 基本上只是在$_GET / $_POST / $_COOKIE 中的所有内容上运行addlashes(),因此将其移植到现代php 并不是一项艰巨的任务,您可以在wordpress 框架中找到移植到现代PHP 的magic_quotes:https ://github.com/WordPress/WordPress/blob/38676936bac7963a62e23e0d2ec260a1ae9731ea/wp-includes/formatting.php#L5453

/**
 * Add slashes to a string or array of strings.
 *
 * This should be used when preparing data for core API that expects slashed data.
 * This should not be used to escape data going directly into an SQL query.
 *
 * @since 3.6.0
 *
 * @param string|array $value String or array of strings to slash.
 * @return string|array Slashed $value
 */
function wp_slash( $value ) {
    if ( is_array( $value ) ) {
        foreach ( $value as $k => $v ) {
            if ( is_array( $v ) ) {
                $value[ $k ] = wp_slash( $v );
            } else {
                $value[ $k ] = addslashes( $v );
            }
        }
    } else {
        $value = addslashes( $value );
    }

    return $value;
}

耸耸肩


推荐阅读