现在的位置: 首页 > 综合 > 正文

ThinkPHP 3.2 开启 cache缓存的注意事项,过滤非法字符

2016年07月23日 ⁄ 综合 ⁄ 共 683字 ⁄ 字号 评论关闭

开启缓存的配置文件 /Application/Common/conf/cache.php源码如下:

<?php
return array(
    //'配置项'=>'配置值'
    'LAYOUT_ON'        => true,
    'HTML_CACHE_ON'    => strpos($_SERVER['HTTP_HOST'], '.') !== false, // 开启静态缓存 默认为 true 本地不开启
    'HTML_CACHE_TIME'  => 3600, // 全局静态缓存有效期(秒)
    'HTML_FILE_SUFFIX' => '.shtml', // 设置静态缓存文件后缀
    'HTML_CACHE_RULES' => array(
        '*' => array('{:module}/{:controller}/{:action}/{$_SERVER.REQUEST_URI|md5}', 3600, 'trimSW'),
    )
);

注意:背后的 trimSW是去除所有非 / \w 的字符串,防止输入中文等特殊字符某些系统报错。

函数 trimSW的源码:

/**
 * @author      default7@zbphp.com
 * @description 去除 空格 和非\w 字符串,用于cache 配置
 *
 * @param        $str
 * @param string $emptyValue
 *
 * @return mixed|string
 */
function trimSW($str, $emptyValue = '_empty_')
{
    $str = preg_replace('/([^\w\/]+)/', '-', $str);
    if (empty($str)) {
        $str = $emptyValue;
    }

    return $str;
}

抱歉!评论已关闭.