文章的浏览量redis存储,关闭浏览器算做一次

原文博客地址https://www.xgs888.top/post/view?id=91;

 

需求;打开网站直到关闭浏览器浏览量+1;

使用yii2.0的框架,用redis来存储点击量;使用cookie来区分;

redis;
         $redis->auth('admin');
        $num = $redis->get('xiaoxietotal');
        //第一次打开的时候是没有cookie的
        if (isset($_COOKIE['advanced-frontend'])){
            $cookie = $_COOKIE['advanced-frontend'];
            $key = $redis->get($cookie);
            if (!$key){
                $redis->set($cookie,1);
                //30分钟过期
                $redis->expire($cookie,30*60);
                if ($num)
                {
                  //  $redis->incr('xiaoxietotal');
                }else{
                    $redis->set('xiaoxietotal',1);
                }
                $todayTotal = $redis->get('xiaoxietoday');
                if ($todayTotal)
                {
                   // $redis->incr('xiaoxietoday');
                }else{
                    $redis->set('xiaoxietoday',1);
                    //当天的浏览量设置0点过期
                    $redis->expire('xiaoxietoday',$times);
                }
            }
        }else{
			//echo 123;
			if ($num)
			{
				$redis->incr('xiaoxietotal');
			}else{
				$redis->set('xiaoxietotal',1);
			}
			$todayTotal = $redis->get('xiaoxietoday');
			if ($todayTotal)
			{
				$redis->incr('xiaoxietoday');
			}else{
				$redis->set('xiaoxietoday',1);
				//当天的浏览量设置0点过期
				$redis->expire('xiaoxietoday',$times);
			}
		}


        return true;
    }
}

 

 

你可能感兴趣的:(php,yii,PHP框架)