在开启 magic_quotes_gpc 的 PHP 环境下使用 Smarty 模板引擎

本文介绍了解决Smarty模板解析问题的方法,特别是在magic_quotes_gpc为ON的情况下。通过覆盖fetch方法,可以实现在运行Smarty时不受到该配置的影响。

尽管不建议设置 magic_quotes_gpc 为 ON,但有些早期的程序已经依赖此环境配置,一时不能关闭它,而默认情况下 Smarty 模板的解析功能又要求必须关闭 magic_quotes_gpc,否则不能正常解析模板文件。
要解决此问题,可以在扩展 Smarty 基类的自己的类里覆盖一下 fetch 方法,如果 PHP 环境是开启 magic_quotes_gpc 的,就临时把此配置关闭一下,待 Smarty 的功能执行完毕再重新打开。那当然如果环境就是关闭的,则原样调用父类本来的 fetch 方法。

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); return parent::fetch($template, $cache_id, $compile_id, $parent, $display); set_magic_quotes_runtime (true); } else { return parent::fetch($template, $cache_id, $compile_id, $parent, $display); } } }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值