php 仅跟踪唯一身份访问者?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4001973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tracking unique visitors only?
提问by Kyle
Currently I have a file called "hits.php" and on any page I want to track page hits I just use <?php include("hits.php"); ?>
目前我有一个名为“hits.php”的文件,并且在任何页面上我想跟踪我刚使用的页面点击量 <?php include("hits.php"); ?>
How can I track unique visitors only though? My hits are false since it can be refreshed by the same person and hits go up.
我如何才能仅跟踪唯一身份访问者?我的点击数是假的,因为它可以被同一个人刷新并且点击数上升。
Here's my source:
这是我的来源:
<?php
$hits = file_get_contents("./client/hits.txt");
$hits = $hits + 1;
$handle = fopen("./client/hits.txt", "w");
fwrite($handle, $hits);
fclose($handle);
print $hits;
?>
I don't really know how I could do cookie checking... is there a way to check IP's? Or what can I do?
我真的不知道如何进行 cookie 检查……有没有办法检查 IP?或者我能做什么?
Thanks StackO.
感谢 StackO。
回答by
The simplest method would be cookie checking.
最简单的方法是 cookie 检查。
A better way would be to create an SQL database and assign the IP address as the primary key. Then whenever a user visits, you would insert that IP into the database.
更好的方法是创建一个 SQL 数据库并将 IP 地址分配为主键。然后每当用户访问时,您都会将该 IP 插入到数据库中。
- Create a function included on all pages that checks for
$_SESSION['logged']
which you can assign whatever 'flag' you want. - If
$_SESSION['logged']
returns 'false' then insert their IP address into the MySQL database. - Set
$_SESSION['logged']
to 'true' so you don't waste resources logging the IP multiple times.
- 创建一个包含在所有页面上的函数,用于检查
$_SESSION['logged']
您可以指定任何您想要的“标志”。 - 如果
$_SESSION['logged']
返回“false”,则将其 IP 地址插入 MySQL 数据库。 - 设置
$_SESSION['logged']
为“true”,这样您就不会浪费资源多次记录 IP。
Note: When creating the MySQL table, assign the IP address' field as the key.
注意:创建 MySQL 表时,将 IP 地址字段指定为键。
<?php
session_start();
if (!$_SESSION['status']) {
$connection = mysql_connect("localhost", "user", "password");
mysql_select_db("ip_log", $connection);
$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("INSERT INTO `database`.`table` (IP) VALUES ('$ip')");
mysql_close($connection);
$_SESSION['status'] = true;
}
?>
回答by winwaed
There isn't a perfect solution, but the first two methods (IP address and/or cookies) are the most reliable, and a combination might be even better.
没有完美的解决方案,但前两种方法(IP 地址和/或 cookie)最可靠,组合使用可能会更好。
Rather than reinventing the wheel I used an off the shelf solution. For commercial reasons I avoided Google Analytics (I don't want Google to know my web stats - their best interests are not mine). They're probably fine for non-commercial websites, or if you don't use Google for advertising. There are also dozens of alternatives. Eg I use Getclicky.com
我没有重新发明轮子,而是使用了现成的解决方案。出于商业原因,我避免使用 Google Analytics(我不希望 Google 知道我的网络统计信息——他们的最大利益不是我的)。它们可能适用于非商业网站,或者如果您不使用 Google 进行广告宣传。还有几十种选择。例如我使用 Getclicky.com
回答by JAL
- At a basic level, you can get the client's IP address by using the PHP
$_SERVER['REMOTE_ADDR']
property - Consider setting a cookie or using a session, though this can be defeated by deletion of a cookie or cookie rejection. See the PHP setcookiedocs for more info.
- There are other methods for browser fingerprinting - check out all the different data you could conceivably use at https://panopticlick.eff.org/index.php
- 在基本层面上,您可以使用 PHP
$_SERVER['REMOTE_ADDR']
属性获取客户端的 IP 地址 - 考虑设置 cookie 或使用会话,尽管这可以通过删除 cookie 或拒绝 cookie 来解决。有关更多信息,请参阅 PHP setcookie文档。
- 浏览器指纹识别还有其他方法 - 在https://panopticlick.eff.org/index.php查看您可以想象使用的所有不同数据
回答by Matt
How about google analytics if you cant. you could do a database or create another file with the IPs in it, but it could get complicated with a flat file like that.
如果你不能,谷歌分析怎么样。您可以创建一个数据库或创建另一个包含 IP 的文件,但是使用这样的平面文件可能会变得复杂。
回答by Sam Gungormez
I found the solution of very poor quality and just a quick and dirty way of doing it.
我找到了质量很差的解决方案,而且只是一种快速而肮脏的方法。
I too was developing something similar and formulated a quick method which works without redundancy.
我也在开发类似的东西,并制定了一种无需冗余的快速方法。
I needed a counter for every time someone accessed another user's profile.
每次有人访问另一个用户的个人资料时,我都需要一个计数器。
Pseudo:
伪:
- Create a table with viewer's name and viewee's name (daily_views table).
- Check to see if exists the viewer's name with the viewee's name (on the same row).
- If they do not exist, update user counter +1 (in users table).
- Else do nothing.
- Reset entire table values null every 24/12 hours via cron job.
- 创建一个包含查看者姓名和被查看者姓名的表(daily_views 表)。
- 检查是否存在查看者姓名和被查看者姓名(在同一行)。
- 如果它们不存在,则更新用户计数器 +1(在用户表中)。
- 别的什么都不做。
- 通过 cron 作业每 24/12 小时将整个表值重置为 null。
This will deny the same person accessing the same user profile to add 1 to the counter on refresh for the whole day (or 12 hours) whereas the above solution by Glenn Nelson would indeed add 1 to the counter, but deny adding to everyuser's counter at the same time.
这将拒绝同一个人访问相同的用户配置文件以在整日(或 12 小时)刷新时向计数器添加 1,而 Glenn Nelson 的上述解决方案确实会向计数器添加 1,但拒绝向每个用户的计数器添加同时。
Not only this, but if you were to logoff and log back in to the website, then it would simply re-add to the counter in which some cases trolls and haxorz wannabe's will exploit this (as the session is destroyed and started again).
不仅如此,而且如果您要注销并重新登录网站,那么它只会重新添加到计数器中,在某些情况下,巨魔和 haxorz 想要的人会利用这一点(因为会话被破坏并重新启动)。
Here are my sample tables:
这是我的示例表:
users
{
user_id INT(8) auto increment, user_name varchar(32), user_counter INT(12)
};
daily_views
{
view_id INT(8) auto increment, viewer_name VARCHAR(32), viewee_name VARCHAR(32)
};
Here is sample code I've written:
这是我编写的示例代码:
<?php
session_start();
$uname = $_SESSION['username'];
$vieweepage = $_GET['name']; //gets the name of the persons page/profile via previous page/form
$connect = mysql_connect("localhost","user","password") or die("Couldn't connect; check your mysql_connect() settings");
$database = mysql_select_db("database") or die("Could not locate database!");
$query = mysql_query("SELECT user_counter from users");
$query = mysql_fetch_row($query);
$counter = $query[0];
$viewcheck = mysql_query("SELECT viewer_name from daily_views WHERE viewee_name='$vieweepage'");
$viewrow = mysql_num_rows($viewcheck);
$newcounter = $counter + 1;
if($viewrow == 0)
{
$update = mysql_query("UPDATE users SET user_counter='$newcounter' WHERE user_name='$vieweepage'");
$insert = mysql_query("INSERT into daily_views (viewer_name, viewee_name) VALUES ('$uname', '$vieweepage')");
}
?>
回答by Saurabh Chandra Patel
currently i am using remote address
and session ID
for visitor.i think its valid visitor because a single user can visit no of times in a days and counter not depends on refresh its only depends on new session.
目前我正在使用remote address
和session ID
用于访问者。我认为它是有效的访问者,因为单个用户可以在一天内访问次数,并且计数器不依赖于刷新,它仅依赖于新会话。
回答by Sebastian Td
You could save a timestamp to localStoage in javascript. LocalStoage isn't removed by the browser, so you should be save to check against that. I know that it isn't serverside checking, but it may be helpful anyway.
您可以在 javascript 中将时间戳保存到 localStoage。LocalStoage 不会被浏览器删除,因此您应该保存以进行检查。我知道这不是服务器端检查,但无论如何它可能会有所帮助。