监听用户的访问的链接

Posted 满血复活的回来写代码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监听用户的访问的链接相关的知识,希望对你有一定的参考价值。

 需求:

监听一个链接的访问来源

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\n", $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\n", $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);
    }
}

 

以上是关于监听用户的访问的链接的主要内容,如果未能解决你的问题,请参考以下文章

多个用户访问同一段代码

跨域访问方法介绍--使用片段识别符传值

在Google Maps android上放置点击监听器

markdown 链接,片段,提醒,收集点作为重新访问WordPress插件开发期间的笔记#WordPress #Plugin #PHP #Refere

微信小程序代码片段

session的使用