ThinkPHP 3.2 开放 cache注缓存,过滤非法字符

时间:2023-03-08 23:50:42
ThinkPHP 3.2 开放 cache注缓存,过滤非法字符

打开缓存配置文件 /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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。