监听用户的访问的链接

 需求:

监听一个链接的访问来源,对于这种行为数据的统计,根据运营人员的数据需求可以用不同的方式进行统计

1.存储到数据库  (这种方法最直接,统计的数据字段也可以增加,后期也方便进行多维度数据分析)

2.存储到文件(写入文件),这种方式的效率太低,每次都要读取,写入文件,对服务器的性能消耗比较大,尤其在大量数据访问的时候会出现问题,不建议使用

3. reids 缓存  (如果直接把浏览数据写入到缓存中,由于缓存数据库并不可靠,要有过期时间,在过期时间之前可以通过定时任务来把缓存数据同步到数据库中)

class UrlMsgAnalyze{
    public function checkUrl($url){
        if( empty($url) ){
            return false;
        }
        return true ;
    }
    public function insertUrlMsgAnalyze($url ){
        if(!$this->checkUrl($url )){
            return ;
        }
        $insertData = array(
            'url' => $url ,
            'create_date' => date("Y-m-d H:i:s" , time() ) ,
            'ip' => $this->GetClientIp(),
        );
        $DBHelper = new DBHelper();
        $DBHelper->Save("open_url_log" , $insertData);
        //$this->getLinkCount("svip");
    }
    //获取客户端的ip地址
    public function GetClientIp(){
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        }
        elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }
        elseif(!empty($_SERVER["REMOTE_ADDR"])){
            $ip = $_SERVER["REMOTE_ADDR"];
        }
        else{
            $ip = "Unknown";
        }
        return $ip;
    }

    //统计链接的访问次数
    public function getLinkCount($name){
        $countfile = "/tmp/url/svip.txt";//统计文件的地址
        if (($fp = fopen($countfile, "r+")) == false) { //用读写模式打开文件,若不能打开就退出
            fopen($countfile, "w");
         } else{
          //如果文件能够正常打开,就读入文件中的数据,假设是1
          $count = fread ($fp,10); //读取10位数据
          $count = $count + 1;
          fclose ($fp);//关闭当前文件
          $fp = fopen($countfile, "w+");//以覆盖模式打开文件
          fwrite ($fp,$count);//写入加1后的新数据
          fclose ($fp);//并关闭文件
        }
    }
}
$Analyze = new UrlMsgAnalyze();
$url = isset($_GET['url'])?trim($_GET['url']):'';
$url = urldecode($url);
$Analyze->insertUrlMsgAnalyze($url);
$key = "pj_url_".$url;//当前openid
$cache = RedisClass::getInstance(12);
$count = $cache->GET($key);//永久存储
echo $count;
echo $key;
if (empty($count)) {
    $cache->SET($key,1, 3600*360);//永久存储
}else{
    $cache->SET($key,$count+1 , 3600*360);//永久存储
}
//header("Location:".$url);

class DBHelper{
    protected $conn;
    function __construct()
    {
        $this->InitConn();
    }

    private function InitConn()
    {
        $this->conn = new mysqli(HOST, USER, PASSWORD, DBNAME);
        if ($this->conn->connect_errno) {
            printf("Connect failed: %s
", $this->conn->connect_error);
            exit();
        }
        $this->conn->query("set names utf8");
    }

    public function changeDb($user, $pass, $ip, $db)
    {
        $this->conn = new mysqli($ip, $user, $pass, $db);

        if ($this->conn->connect_errno) {
            printf("Connect failed: %s
", $this->conn->connect_error);
            exit();
        }
        $this->conn->query("set names utf8");
    }
    public function Execute($sql)
    {
        return $this->conn->query($sql);
    }
    public function find($sql)
    {
        $ret = [];
        $list = $this->conn->query($sql);
        while ($row = $list->fetch_array(MYSQLI_ASSOC)) {
            $ret[] = $row;
        }
        return $ret;
    }
    public function findOne($sql)
    {
        $list = $this->conn->query($sql);
        $row = $list->fetch_array(MYSQLI_ASSOC);
        return $row;
    }

    public function Save($table ,$data)
    {
        $sql ="insert into $table set ";
        foreach ($data as $key => $value) {
            $data[$key] = addslashes($value);
        }
        foreach ($data as $key => $value) {
            $sql =$sql ."`".$key."`='". $value ."',";
        }
        $sql = substr($sql,0,count($sql)-2);
        return $this->conn->query($sql);
    }

    public function Close()
    {

        $this->conn->close();

    }

    public function executesql($sql)
    {
        return $this->conn->query($sql);
    }
}

class RedisClass{
  static $_instance; //存储对象
  private $handler ;
  public function __construct($dbindex = 0)
  {
    if (!extension_loaded('redis') ) {
        throw new Exception("REDIS NOT  SUPPORT", 1);
    }
    $this->handler =  new Redis();
    $this->handler->connect(redisHostname,redisPort);
    $this->handler->auth(redisAuth);
    $this->handler->select($dbindex);
  }

    public static function getInstance($dbindex = 0){
        if(!isset(self::$_instance[$dbindex]) or  FALSE == (self::$_instance[$dbindex] instanceof self)){
          self::$_instance[$dbindex] = new self($dbindex);
        }
        return self::$_instance[$dbindex];
    }

    /**key value  get**/
    public  function GET($key)
    {
      return  $this->handler->get($key);
    }
    /**key value  set  过期时间为 $exp**/
    public  function SET($key ,$value ,$exp)
    {
        $this->handler->setex($key ,$exp ,$value );
    }

    /*移除数据$key*/
    public  function REMOVE($key)
    {
        $this->handler->delete($key);

    }

      /*设置数据的过期时间$key*/
    public  function EXPIRE($key ,$exp)
    {
        $this->handler->expire($key ,$exp);
    }

    /**Hash 相关**/

    public  function HGET($domain , $key)
    {
        return $this->handler->hGet($domain , $key);
    }
    public  function HSET ($domain ,$key ,$value )
    {
          $this->handler->hSet($domain , $key);
    }

    public  function HREMOVE($domain ,$key)
    {
        $this->handler->hDel($domain , $key);

    }

     /*插入列表*/
    public  function  PushList($channel,$data)
    {
          $this->handler->lPush($channel,$data);
    }

    /*从列表中获取*/
    public function  POPList($channel)
    {
        return  $this->handler->lPop($channel);
    }

    public function Select($dbnumber)
    {
      return  $this->handler->SELECT($dbnumber);
    }
}
原文地址:https://www.cnblogs.com/xs-yqz/p/7527937.html