解决方案
要解决此问题,可以在扩展 Smarty 基类的自己的类里覆盖一下 fetch 方法,如果 PHP 环境是开启 magic_quotes_gpc 的,就临时把此配置关闭一下,待 Smarty 的功能执行完毕再重新打开。那当然如果环境就是关闭的,则原样调用父类本来的 fetch 方法。
<?php
class MySmarty extends Smarty
{
/**
* My own method overriding default method. To make it work under PHP environment with magic_quotes_gpc ON.
* We can simply comment out this method if one day PHP environment is changed to magic_quotes_gpc Off.
* @see Smarty::fetch()
*/
function fetch($template, $cache_id = null, $compile_id = null, $parent = null, $display = false)
{
if (get_magic_quotes_gpc())
{
set_magic_quotes_runtime(false);
$result = parent::fetch($template, $cache_id, $compile_id, $parent, $display);
set_magic_quotes_runtime(true);
return $result;
}
else
{
return parent::fetch($template, $cache_id, $compile_id, $parent, $display);
}
}
}
原文链接: https://www.snowpeak.fun/cn/article/detail/use_smarty_with_magic_quotes_gpc_on/