dongwei2882 2013-04-27 16:56
浏览 40

如何只在MySql中插入一次访问者详细信息

I want to enter only one detail in table per user visit. Irrespective of which page he lands on, his visit counts. So i inserted this in header.php //Header file included in every php file.

 //$retuser is not defined in veryfirst visit

if(!isset($retuser)){ 

    $ip=getRealIpAddr();
    if(isset($_SESSION['user_id'])) {
        $uid=$_SESSION['user_id'];
    } else {$uid=0;
    }
    $query="insert into visitors (vistime,visip,visiden) values(now(),'{$ip}','{$uid}')";
    $result = mysql_query($query,$connection);
    if (!$result) {
        echo "DB Error, could not insert comments
";
        echo 'MySQL Error: ' . mysql_error();
        exit;
    }
    $retuser=1;
}

Now this is inserting user entry on every page reloading. How to solve this?

UPDATE :

I want $retuser to work or live until user closes his tab. Upon closing of tab $retuser must be destroyed!

  • 写回答

1条回答 默认 最新

  • doucuo9126 2013-04-27 17:36
    关注

    A few things to keep in mind when you're trying to track visitors, luckily you're using a user_id so you don't have to worry about a lot of the issues that arise with tracking casual browsers (non-authenticated):

    1. SESSIONS *should* and usually do close when the user closes their tab or window in their browser. A user can keep a session open by leaving a tab open in the browser for an extended period of time.
    2. You're setting a user_id, but you need to store the session_id. This will help you determine if they're using the same browser or if they're closing their browser on every page (looks this way with bots like cURL). Do you want them to have two open logged in sessions between two different browsers? How secure does the site need to be?
    3. Users can appear to come from multiple IPs even though they haven't closed their browsers. This happens by design on cellular networks (mobile visitors) and with internet proxy pools. Same user coming from two different towers can switch IPs multiple times during a single session on a mobile device. Bot nets also behave this way, but they're not technically the users you want to track unless you're trying to stop them.
    4. More than one user can come from an IP address. This happens on local networks. Two people on the local network with different local IP addresses, one public web IP address (usually something like a gateway, a router, or a modem). It is technically possible to try and capture their MAC Address on their local machine's NIC, but it doesn't work in all circumstances (and it would appear invasive if they receive a warning about browser permissions or an insecure site). I have 5 machines running on my local network right now and I will frequently open a page with all 5 for testing. This makes it look like much more traffic even though I'm the same user. If a site blocks my multiple sessions I will typically create new accounts for each machine. (I have to do a bit of "testing" on sites I'm responsible for.)
    5. A user may have more than one session open at a time if they're browsing with more than one browser on the same machine.

    You can pull a query to see if the user has been on the site within a certain amount of time. The problem however is if a user is legitimate, leaves their browser open because they went to lunch or to the restroom and comes back 10 minutes later, that SESSION is still active. They've not gone anywhere, in fact if they hit another page, this is the type of person you want to keep. If you flush their session because of the delay you're shorting yourself of the potential track.

    You can use Javascript components to track when they last moved their mouse or moved away from your page with focus, but it may make your site appear slower if the listeners are greedy. Additionally someone can disable Javascript or block your script and still legitimately browse your site.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?