上次写了一个针对苹果cms(maccms)开源视频CMS内容管理系统的一些优化方针,针对站长在设定分类视频只限定一般会员或VIP会员才可以访问的方法,后来发现,如果后台开启缓存页面功能会导致错乱,原因是苹果cms的缓存机制并没有针对用户组进行个别缓存,它的缓存机制是全部通用,如果要让会员访问速度加快就要进行缓存,但是这样子一般访客就会看到VIP视频的内容。
所以这次要换另外一个写法,不过蜘蛛用户组无法进行缓存,但可以绕过用户组状态查询来决定是否可以浏览视频。
首先要找到下面的文档:
/application/index/controller/Base.php
找到 check_user_popedom 这个 function,在这个 function 之前添加一个 function:
protected function is_spider($user_agent = null)
{
if ($user_agent === null) {
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
}
if (empty($user_agent)) {
return false;
}
$spiders = [
'googlebot','baiduspider','bingbot','yandexbot','sogou',
'360spider','bytespider','petalbot','duckduckbot','slurp',
'ahrefsbot','semrushbot','mj12bot','dotbot','Chrome-Lighthouse'
];
foreach ($spiders as $spider) {
if (stripos($user_agent, $spider) !== false) {
return true;
}
}
return false;
}
然后在 Base.php 的类里面在 check_user_popedom function 里面最前面添加:
if ($this->is_spider()) {
return ['code' => 1, 'msg' => lang('controller/popedom_ok')];
}
找到另一个文档:
/application/common/controller/All.php
在 load_page_cache 的 function 的最开头加入一个判断:
if ($this->is_spider()) {
return; // 蜘蛛直接跳過快取
}
在 function label_fetch 要改的地方:
if ($loadcache == 1 && !$this->is_spider()) {
$this->load_page_cache($tpl, $type);
}
以及
if (defined('ENTRANCE') && ENTRANCE == 'index' && $GLOBALS['config']['app']['cache_page'] == 1 && $GLOBALS['config']['app']['cache_time_page'] && !$this->is_spider()) {
$cach_name = $_SERVER['HTTP_HOST'] . '_' . MAC_MOB . '_' . $GLOBALS['config']['app']['cache_flag'] . '_' . $tpl . '_' . http_build_query(mac_param_url());
Cache::set($cach_name, $html, $GLOBALS['config']['app']['cache_time_page']);
}