简体   繁体   English

在PHP中跟踪访客IP /点击次数

[英]Tracking visitor IP/Clicks in PHP

I am trying to write a script or more like come up with a simple logic to track clicks or visits. 我正在尝试编写一个脚本或更像是提出一个简单的逻辑来跟踪点击或访问。 I don't need to track each page, just as long as they land on the homepage is where I want to store it as 1 click. 我不需要跟踪每个页面,只要它们落在主页上我想要将其存储为1次点击。

First of all, is it safe to say that tracking by IP is far from accurate because many users can be under the same IP? 首先,可以肯定地说,通过IP进行跟踪远非准确,因为许多用户可以使用相同的IP吗?

Currently my logic to do this is set a cookie on client side with a flag when they land on the homepage for the first time. 目前我做这个的逻辑是在客户端设置一个cookie,当他们第一次登陆主页时带有一个标志。 At that point, I would update the database with 1 unqiue click as unique. 此时,我将使用1 unqiue click更新数据库为唯一。 Then each time this same visitor visits, the homepage would check for the flag and if it exists, update the database with 1 raw click....etc. 然后每当这个访问者访问时,主页将检查该标志,如果它存在,则用1原始点击等更新数据库。

I do know that if they dump their cookies, it would throw off the data but generally, is this how it is done? 我知道,如果他们转储他们的cookie,它会丢弃数据,但一般来说,这是怎么做的?

Do you have a better way? 你有更好的方法吗?

Try this to retrieve the ip of the visitor, it works fine for my statistics : 试试这个来检索访问者的ip,它适用于我的统计数据:

function get_ip()
{
    if($_SERVER){
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
            $adress = $_SERVER['HTTP_X_FORWARDED_FOR'];
        elseif(isset($_SERVER['HTTP_CLIENT_IP']))
            $adress = $_SERVER['HTTP_CLIENT_IP'];
        else
            $adress = $_SERVER['REMOTE_ADDR'];
    } else {
        if(getenv('HTTP_X_FORWARDED_FOR'))
            $adress = getenv('HTTP_X_FORWARDED_FOR');
        elseif(getenv('HTTP_CLIENT_IP'))
            $adress = getenv('HTTP_CLIENT_IP');
        else
            $adress = getenv('REMOTE_ADDR');
    }

    return $adress;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM