javascript 我如何收集有关我网站访问者的信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3451073/
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
How do I collect information about my website's visitors?
提问by Richard
Is there some way to store:
有没有办法存储:
- A visitor's IP Address
- What time a visitor visited my site
- How many times they visited
- 访问者的 IP 地址
- 访问者什么时候访问我的网站
- 他们访问了多少次
I know there is a way to do this withoutPHP, just with Javascript and some text files.
我知道有一种方法可以在没有PHP的情况下做到这一点,只需使用 Javascript 和一些文本文件。
How it would I do this in PHP?
我将如何在 PHP 中执行此操作?
回答by pankaj
It's a very easy task.
这是一项非常容易的任务。
You can do this with the help of php. You can get many types of information about client visiting your site. You can get to know about the ip address, date, time, operating system, browser, isp of that ip address and many more things. You will have to use php and mysql !
你可以在 php 的帮助下做到这一点。您可以获得有关访问您网站的客户的多种类型的信息。您可以了解该 IP 地址的 IP 地址、日期、时间、操作系统、浏览器、ISP 等等。你将不得不使用 php 和 mysql !
firstly create a table in mysql.
首先在mysql中创建一个表。
create_table_track.php
create_table_track.php
<?php
$server = "localhost";
$username = "username";
$password = "password";
$database = "database name";
$connId = mysql_connect($server,$username,$password) or die("Cannot connect to server");
$selectDb = mysql_select_db($database,$connId) or die("Cannot connect to database");
$result = "CREATE TABLE track(
`id` int(6) NOT NULL auto_increment,
`tm` varchar(20) NOT NULL default '',
`ref` varchar(250) NOT NULL default '',
`agent` varchar(250) NOT NULL default '',
`ip` varchar(20) NOT NULL default '',
`ip_value` int(11) NOT NULL default '0',
`domain` varchar(20) NOT NULL default '',
`tracking_page_name` varchar(10) NOT NULL default '',
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ";
if (mysql_query($result))
{
print "Success in TABLE creation!......";
}
else
{
die('MSSQL error: ' . mssql_get_last_message());
}
?>'
This is the first part of getting the information about client. This file will create a table named "track" in mysql database. Now to store information in this table, you will have to create another file.
这是获取客户端信息的第一部分。该文件将在 mysql 数据库中创建一个名为“track”的表。现在要将信息存储在此表中,您必须创建另一个文件。
example.php
例子.php
<html>
<?php
// fill in your databasa data here!
$server = "localhost";
$username = "username";
$password = "password";
$database = "database name";
$connId = mysql_connect($server,$username,$password) or die("Cannot connect to server");
$selectDb = mysql_select_db($database,$connId) or die("Cannot connect to database");
$tracking_page_name="example";
$ref=$_SERVER['HTTP_REFERER'];
$agent=$_SERVER['HTTP_USER_AGENT'];
$ip=$_SERVER['REMOTE_ADDR'];
$host_name = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$strSQL = "INSERT INTO track(tm, ref, agent, ip, tracking_page_name, host_name) VALUES(curdate(),'$ref','$agent','$ip','$tracking_page_name','$host_name')";
$test=mysql_query($strSQL);
?>
</html>
Now the above file will extract information from client computer and store it in the database.
现在上述文件将从客户端计算机中提取信息并将其存储在数据库中。
Now to show the information stored in database,, we wil use following file---
现在要显示存储在数据库中的信息,我们将使用以下文件---
show track.php
显示轨道.php
<html>
<body>
<?php
$con = mysql_connect("localhost","user name","password");
mysql_select_db("database name", $con) or die( "Unable to select database");
$query="SELECT * FROM track";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">id</font></th>
<th><font face="Arial, Helvetica, sans-serif">time</font></th>
<th><font face="Arial, Helvetica, sans-serif">http referer</font></th>
<th><font face="Arial, Helvetica, sans-serif">user agent</font></th>
<th><font face="Arial, Helvetica, sans-serif">ip address</font></th>
<th><font face="Arial, Helvetica, sans-serif">ip value</font></th>
<th><font face="Arial, Helvetica, sans-serif">domain</font></th>
<th><font face="Arial, Helvetica, sans-serif">tracking_page_name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Host_name</font></th>
</tr>
<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"tm");
$f3=mysql_result($result,$i,"ref");
$f4=mysql_result($result,$i,"agent");
$f5=mysql_result($result,$i,"ip");
$f6=mysql_result($result,$i,"ip_value");
$f7=mysql_result($result,$i,"domain");
$f8=mysql_result($result,$i,"tracking_page_name");
$f9=mysql_result($result,$i,"host_name");
?>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f7; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f8; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f9; ?></font></td>
</tr>
<?php
$i++;
}
?>
</body>
</html>'
回答by Darin Dimitrov
There's no way to store anything on a client computer using pure javascript. It runs in a sandboxed environment that doesn't allow you to perform such tasks. You could send an AJAX request to a server side script that will perform the necessary steps to persists the data. Another option of course is to use some service like Google Analyticswhich will take care of persisting statistics about your site visits by including a simple script at the end of your pages.
无法使用纯 JavaScript 在客户端计算机上存储任何内容。它在不允许您执行此类任务的沙盒环境中运行。您可以向服务器端脚本发送 AJAX 请求,该脚本将执行必要的步骤来持久化数据。当然,另一种选择是使用诸如Google Analytics 之类的服务,它会通过在页面末尾包含一个简单的脚本来处理有关站点访问的持久统计信息。
回答by user3421973
as he said WITHOUT PHP!! (only textfiles) i would prefer using jsonp with a callback function and a webservice like ip-api.com or ipinfo.io
正如他所说的没有 PHP !(仅限文本文件)我更喜欢将 jsonp 与回调函数和网络服务(如 ip-api.com 或 ipinfo.io)一起使用
function fire() {
var url = "http://ipinfo.io/json?callback=func";
var script = document.createElement('script');
script.src = url;
script.async = true;
document.getElementsByTagName('head')[0].appendChild(script);
}
function func(response) {
alert(response.hostname); // for example hostname
}
document.onload = fire();
instead of response.hostname you can alert everything what the service gives you back like
而不是 response.hostname 你可以提醒所有服务给你的东西
{
"ip": "178.165.128.3",
"hostname": "178.165.128.3.wireless.dyn.drei.com",
"city": null,
"region": null,
"country": "AT",
"loc": "47.3333,13.3333",
"org": "AS12635 Hutchison Drei Austria GmbH"
}
the time you can get by
你可以得到的时间
var d = new Date();
var n = d.getTime();
and the count how often visitor cames i would store in local storage..
以及我将存储在本地存储中的访客来访频率的计数..
回答by ozk
you will need some kind of server side processing if you want to write to files on the server. if you want client-side storage, it is a relativley new feature (HTML5), but doesn't allow access to the filesystem directly.
如果要写入服务器上的文件,则需要某种服务器端处理。如果你想要客户端存储,它是一个相对的新特性 (HTML5),但不允许直接访问文件系统。
回答by ozk
In PHP, If I understand what you are trying to do, what you are trying to accomplish is very easy.
在 PHP 中,如果我理解你想要做什么,那么你想要完成的事情就很容易了。
To get the visitors IP Address, you would simply use the $_SERVER superglobal, like so:
要获取访问者的 IP 地址,您只需使用 $_SERVER 超全局变量,如下所示:
<?php $_SERVER['ip_address']; ?>
Then, to get the time, you would simply use:
然后,要获得时间,您只需使用:
<?php time(); ?>
Now, the slightly more tricky part, how many times they visited. You need to put the above into a database, e.g. mysql.
现在,稍微棘手的部分,他们访问了多少次。您需要将上述内容放入一个数据库中,例如 mysql。
Create a database schema, with the following columns: id, ipAddress, time and numOfVisits. You would need to have some php logic at the start of your webpage to check if the users IP Address is already in the database, and if it is, increment the numOfVisits column. You would obviously need to log the output of time() into the time column also.
创建一个数据库模式,包含以下列:id、ipAddress、time 和 numOfVisits。您需要在网页的开头使用一些 php 逻辑来检查用户 IP 地址是否已经在数据库中,如果是,请增加 numOfVisits 列。您显然还需要将 time() 的输出记录到时间列中。
In essence, that's how you would do what you are asking in PHP. Hope this helped :)
从本质上讲,这就是您在 PHP 中要求的方式。希望这有帮助:)

