我如何计算我的网站的唯一访客?

时间:2022-11-29 21:09:44

I am making a visitor counting system for user posts to show the most viewed posts on homepage. I have a visitor counting system now but all it registers a view at every page refresh. I cannot use Google Analytics.

我正在制作一个访问者计数系统,用于显示主页上浏览次数最多的文章。我现在有一个访客计数系统,但它在每次刷新页面时都会注册一个视图。我不能使用谷歌分析。

What I need is a visitor counter which only counts the unique visitors. In my case, unique means one person can only view a post in a day? Even a week might work, I think. Can you write that php code in here? You can give me a link to some good tutorials too if you like too.

我需要的是一个访客计数器,它只计算唯一的访客。在我的例子中,unique意味着一个人一天只能看到一个帖子?我想即使是一周也可以。你能在这里写php代码吗?如果你也喜欢的话,你也可以给我一些好的教程的链接。

This is what the code needs to do (or equivalent):

这就是代码需要做的(或等效的):

  1. Once the page loads, check if the visitor is new or old(I have no idea how to do it .. )
  2. 一旦页面加载,检查访问者是新的还是旧的(我不知道怎么做)
  3. If he is old, ignore him
  4. 如果他老了,别理他
  5. If he is new, in mysql, views = views + 1
  6. 如果他是新的,在mysql中,视图=视图+ 1。

5 个解决方案

#1


17  

http://coursesweb.net/php-mysql/register-show-online-users-visitors_t

http://coursesweb.net/php-mysql/register-show-online-users-visitors_t

Here is a nice tutorial ,is what you need.

这里有一个很好的教程,是你需要的。

Register and show online users and visitors

注册并显示在线用户和访问者

Count Online users and visitors using a MySQL table In this tutorial you can learn how to register, to count, and display in your webpage the number of online users and visitors. The principle is this: each user / visitor is registered in a text file or database. Every time a page of the website is accessed, the php script deletes all records older than a certain time (eg 2 minutes), adds the current user / visitor and takes the number of records left to display.

在本教程中使用MySQL表计算在线用户和访问者的数量,您可以学习如何注册、计数和在网页中显示在线用户和访问者的数量。原则是:每个用户/访问者都注册在一个文本文件或数据库中。每次访问网站页面时,php脚本都会删除比特定时间(如2分钟)以上的所有记录,并添加当前用户/访问者,并记录显示的记录数。

You can store the online users and visitors in a file on the server, or in a MySQL table. In this case, I think that using a text file to add and read the records is faster than storing them into a MySQL table, which requires more requests.

您可以将在线用户和访问者存储在服务器上的文件或MySQL表中。在这种情况下,我认为使用文本文件添加和读取记录比将它们存储到MySQL表要快,这需要更多的请求。

First it's presented the method with recording in a text file on the server, than the method with MySQL table.

首先,它将在服务器上的文本文件中记录方法,而不是使用MySQL表的方法。

To download the files with the scripts presented in this tutorial, click -> Count Online Users and Visitors.

要下载本教程中提供的脚本文件,请点击->在线用户和访问者。

• Both scripts can be included in ".php" files (with include()), or in ".html" files (with ), as you can see in the examples presented at the bottom of this page; but the server must run PHP. Storing online users and visitors in a text file

•两个脚本都可以包含在“。”php“文件(包含())或in”。html“文件”,如你在本页底部的例子中所看到的;但是服务器必须运行PHP。将在线用户和访问者存储在文本文件中

To add records in a file on the server with PHP you must set CHMOD 0766 (or CHMOD 0777) permissions to that file, so the PHP can write data in it.

要使用PHP在服务器上的文件中添加记录,您必须为该文件设置CHMOD 0766(或CHMOD 0777)权限,以便PHP可以在其中写入数据。

1-Create a text file on your server (for example, named "userson.txt") and give it CHMOD 0777 permissions (in your FTP application, right click on that file, choose Properties, then select Read, Write, and Execute options). 2-Create a PHP file (named "usersontxt.php") having the code below, then copy this php file in the same directory as "userson.txt". The code for usersontxt.php

1-在服务器上创建一个文本文件(例如,名为“userson.txt”),并赋予它CHMOD 0777权限(在FTP应用程序中,右键单击该文件,选择Properties,然后选择Read、Write和Execute选项)。创建一个PHP文件(名为“usersontxt.php”),其中包含以下代码,然后将这个PHP文件复制到与“userson.txt”相同的目录中。usersontxt.php的代码

<?php
// Script Online Users and Visitors - http://coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start();        // start Session, if not already started

$filetxt = 'userson.txt';  // the file in which the online users /visitors are stored
$timeon = 120;             // number of secconds to keep a user online
$sep = '^^';               // characters used to separate the user name and date-time
$vst_id = '-vst-';        // an identifier to know that it is a visitor, not logged user

/*
 If you have an user registration script,
 replace $_SESSION['nume'] with the variable in which the user name is stored.
 You can get a free registration script from:  http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/

// get the user name if it is logged, or the visitors IP (and add the identifier)

    $uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i';         // regexp to recognize the line with visitors
$nrvst = 0;                                       // to store the number of visitors

// sets the row with the current user /visitor that must be added in $filetxt (and current timestamp)

    $addrow[] = $uvon. $sep. time();

// check if the file from $filetxt exists and is writable

    if(is_writable($filetxt)) {
      // get into an array the lines added in $filetxt
      $ar_rows = file($filetxt, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
      $nrrows = count($ar_rows);

            // number of rows

  // if there is at least one line, parse the $ar_rows array

      if($nrrows>0) {
        for($i=0; $i<$nrrows; $i++) {
          // get each line and separate the user /visitor and the timestamp
          $ar_line = explode($sep, $ar_rows[$i]);
      // add in $addrow array the records in last $timeon seconds
          if($ar_line[0]!=$uvon && (intval($ar_line[1])+$timeon)>=time()) {
            $addrow[] = $ar_rows[$i];
          }
        }
      }
    }

$nruvon = count($addrow);                   // total online
$usron = '';                                    // to store the name of logged users
// traverse $addrow to get the number of visitors and users
for($i=0; $i<$nruvon; $i++) {
 if(preg_match($rgxvst, $addrow[$i])) $nrvst++;       // increment the visitors
 else {
   // gets and stores the user's name
   $ar_usron = explode($sep, $addrow[$i]);
   $usron .= '<br/> - <i>'. $ar_usron[0]. '</i>';
 }
}
$nrusr = $nruvon - $nrvst;              // gets the users (total - visitors)

// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. $nruvon. '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';

// write data in $filetxt
if(!file_put_contents($filetxt, implode("\n", $addrow))) $reout = 'Error: Recording file not exists, or is not writable';

// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";

echo $reout;             // output /display the result
?>

3-If you want to include the script above in a ".php" file, add the following code in the place you want to show the number of online users and visitors:

3-如果你想在a中包含上面的脚本。php“文件,在你想要显示在线用户和访客数量的地方添加以下代码:

4-To show the number of online visitors /users in a ".html" file, use this code:

4-显示a中的在线访客/用户数量。html“文件,使用此代码:

<script type="text/javascript" src="usersontxt.php?uvon=showon"></script>

This script (and the other presented below) works with $_SESSION. At the beginning of the PHP file in which you use it, you must add: session_start();. Count Online users and visitors using a MySQL table

这个脚本(以及下面的另一个脚本)使用$_SESSION工作。在使用它的PHP文件的开头,必须添加:session_start();使用MySQL表对在线用户和访问者进行计数

To register, count and show the number of online visitors and users in a MySQL table, require to perform three SQL queries: Delete the records older than a certain time. Insert a row with the new user /visitor, or, if it is already inserted, Update the timestamp in its column. Select the remaining rows. Here's the code for a script that uses a MySQL table (named "userson") to store and display the Online Users and Visitors.

要在MySQL表中注册、计数和显示在线访问者和用户的数量,需要执行三个SQL查询:删除超过某个时间的记录。向新用户/访问者插入一行,或者,如果已经插入,则在其列中更新时间戳。选择剩下的行。下面是使用MySQL表(名为“userson”)存储和显示在线用户和访问者的脚本的代码。

1-First we create the "userson" table, with 2 columns (uvon, dt). In the "uvon" column is stored the name of the user (if logged in) or the visitor's IP. In the "dt" column is stored a number with the timestamp (Unix time) when the page is accessed. - Add the following code in a php file (for example, named "create_userson.php"): The code for create_userson.php

首先,我们创建“userson”表,其中有2列(uvon, dt)。在“uvon”列中存储用户名(如果登录)或访问者的IP。在“dt”列中,当访问页面时,用时间戳(Unix时间)存储一个数字。-在php文件中添加以下代码(例如,命名为create_userson.php): create_userson.php的代码。

<?php
header('Content-type: text/html; charset=utf-8');

// HERE add your data for connecting to MySQ database
$host = 'localhost';           // MySQL server address
$user = 'root';                // User name
$pass = 'password';            // User`s password
$dbname = 'database';          // Database name

// connect to the MySQL server
$conn = new mysqli($host, $user, $pass, $dbname);

// check connection
if (mysqli_connect_errno()) exit('Connect failed: '. mysqli_connect_error());

// sql query for CREATE "userson" TABLE
$sql = "CREATE TABLE `userson` (
 `uvon` VARCHAR(32) PRIMARY KEY,
 `dt` INT(10) UNSIGNED NOT NULL
 ) CHARACTER SET utf8 COLLATE utf8_general_ci"; 

// Performs the $sql query on the server to create the table
if ($conn->query($sql) === TRUE) echo 'Table "userson" successfully created';
else echo 'Error: '. $conn->error;

$conn->close();
?>

2. - Now we create the script that Inserts, Deletes, and Selects data in the "userson" table (For explanations about the code, see the comments in script).
- Add the code below in another php file (named "usersmysql.php"):
In both file you must add your personal data for connecting to MySQL database, in the variables: $host, $user, $pass, and $dbname .
The code for usersmysql.php

<?php
// Script Online Users and Visitors - coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start();         // start Session, if not already started

// HERE add your data for connecting to MySQ database
$host = 'localhost';           // MySQL server address
$user = 'root';                // User name
$pass = 'password';            // User`s password
$dbname = 'database';          // Database name

/*
 If you have an user registration script,
 replace $_SESSION['nume'] with the variable in which the user name is stored.
 You can get a free registration script from:  http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/

// get the user name if it is logged, or the visitors IP (and add the identifier)
$vst_id = '-vst-';         // an identifier to know that it is a visitor, not logged user
$uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i';         // regexp to recognize the rows with visitors
$dt = time();                                    // current timestamp
$timeon = 120;             // number of secconds to keep a user online
$nrvst = 0;                                     // to store the number of visitors
$nrusr = 0;                                     // to store the number of usersrs
$usron = '';                                    // to store the name of logged users

// connect to the MySQL server
$conn = new mysqli($host, $user, $pass, $dbname);

// Define and execute the Delete, Insert/Update, and Select queries
$sqldel = "DELETE FROM `userson` WHERE `dt`<". ($dt - $timeon);
$sqliu = "INSERT INTO `userson` (`uvon`, `dt`) VALUES ('$uvon', $dt) ON DUPLICATE KEY UPDATE `dt`=$dt";
$sqlsel = "SELECT * FROM `userson`";

// Execute each query
if(!$conn->query($sqldel)) echo 'Error: '. $conn->error;
if(!$conn->query($sqliu)) echo 'Error: '. $conn->error;
$result = $conn->query($sqlsel);

// if the $result contains at least one row
if ($result->num_rows > 0) {
  // traverse the sets of results and set the number of online visitors and users ($nrvst, $nrusr)
  while($row = $result->fetch_assoc()) {
    if(preg_match($rgxvst, $row['uvon'])) $nrvst++;       // increment the visitors
    else {
      $nrusr++;                   // increment the users
      $usron .= '<br/> - <i>'.$row['uvon']. '</i>';          // stores the user's name
    }
  }
}

$conn->close();                  // close the MySQL connection

// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. ($nrusr+$nrvst). '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';

// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";

echo $reout;             // output /display the result
?>
  1. After you have created these two php files on your server, run the "create_userson.php" on your browser to create the "userson" table.
  2. 在您的服务器上创建了这两个php文件之后,运行“create_userson”。在浏览器上创建“userson”表。
  3. Include the "usersmysql.php" file in the php file in which you want to display the number of online users and visitors.
  4. 包括“usersmysql。在php文件中显示在线用户和访问者数量的文件。
  1. Or, if you want to insert it in a ".html" file, add this code:
  2. 或者,如果你想把它插入a中。html文件,添加以下代码:

Examples using these scripts

使用这些示例脚本

• Including the "usersontxt.php" in a php file:

•包括“usersontxt。php文件中的php:

Counter Online Users and Visitors

• Including the "usersmysql.php" in a html file: Counter Online Users and Visitors

•包括“usersmysql。在html文件中:计数器在线用户和访问者。

Both scripts (with storing data in a text file on the server, or into a MySQL table) will display a result like this: Online: 5

这两个脚本(将数据存储在服务器上的文本文件或MySQL表中)都将显示如下结果:Online: 5

Visitors: 3 Users: 2 - MarPlo - Marius

访客:3位用户:2 - MarPlo - Marius

#2


10  

Unique views is always a hard nut to crack. Checking the IP might work, but an IP can be shared by more than one user. A cookie could be a viable option, but a cookie can expire or be modified by the client.

独特的观点总是一个难题。检查IP可能有用,但是一个IP可以由多个用户共享。cookie可以是一个可行的选项,但是cookie可以过期或由客户端修改。

In your case, it don't seem to be a big issue if the cookie is modified tho, so i would recommend using a cookie in a case like this. When the page is loaded, check if there is a cookie, if there is not, create one and add a +1 to views. If it is set, don't do the +1.

在您的情况下,如果cookie被修改为tho,似乎不是什么大问题,所以我建议在这种情况下使用cookie。加载页面时,检查是否有cookie,如果没有,创建一个,并向视图添加+1。如果它被设置,不要做+1。

Set the cookies expiration date to whatever you want it to be, week or day if that's what you want, and it will expire after that time. After expiration, it will be a unique user again!

将cookie过期日期设置为您想要的日期,如果您想要,可以是周或日,那么它将在此之后过期。过期后,它将再次成为唯一的用户!


Edit:
Thought it might be a good idea to add this notice here...
Since around the end of 2016 a IP address (static or dynamic) is seen as personal data in the EU.
That means that you are only allowed to store a IP address with a good reason (and I'm not sure if tracking views is a good reason). So if you intend to store the IP address of visitors, I would recommend hashing or encrypting it with a algorithm which can not be reversed, to make sure that you are not breaching any law (especially after the GDPR laws have been implemented).

编辑:我想在这里加上这个通知可能是个好主意……由于2016年年底前后,在欧盟,IP地址(静态或动态)被视为个人数据。这意味着您只能存储一个有充分理由的IP地址(我不确定跟踪视图是否是一个好的理由)。因此,如果你想要存储访问者的IP地址,我建议用一个无法撤销的算法来对它进行哈希或加密,以确保你没有违反任何法律(尤其是在GDPR法律实施之后)。

#3


8  

I have edited the "Best answer" code, though I found a useful thing that was missing. This is will also track the ip of a user if they are using a Proxy or simply if the server has nginx installed as a proxy reverser.

我已经编辑了“最佳答案”代码,尽管我发现了一个有用的东西缺失了。如果用户使用的是代理,或者只是服务器有nginx作为代理反向器安装,那么这也将跟踪用户的ip。

I added this code to his script at the top of the function:

我把这段代码添加到他在函数顶部的脚本中:

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
$adresseip = getRealIpAddr();

Afther that I edited his code.

然后我编辑了他的代码。

Find the line that says the following:

找到这样一行:

// get the user name if it is logged, or the visitors IP (and add the identifier)

    $uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

and replace it with this:

把它替换为:

$uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $adresseip. $vst_id;

This will work.

这将工作。

Here is the full code if anything happens:

如果发生什么事情,这里有完整的代码:

<?php

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
$adresseip = getRealIpAddr();

// Script Online Users and Visitors - http://coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start();        // start Session, if not already started

$filetxt = 'userson.txt';  // the file in which the online users /visitors are stored
$timeon = 120;             // number of secconds to keep a user online
$sep = '^^';               // characters used to separate the user name and date-time
$vst_id = '-vst-';        // an identifier to know that it is a visitor, not logged user

/*
 If you have an user registration script,
 replace $_SESSION['nume'] with the variable in which the user name is stored.
 You can get a free registration script from:  http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/

// get the user name if it is logged, or the visitors IP (and add the identifier)

    $uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i';         // regexp to recognize the line with visitors
$nrvst = 0;                                       // to store the number of visitors

// sets the row with the current user /visitor that must be added in $filetxt (and current timestamp)

    $addrow[] = $uvon. $sep. time();

// check if the file from $filetxt exists and is writable

    if(is_writable($filetxt)) {
      // get into an array the lines added in $filetxt
      $ar_rows = file($filetxt, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
      $nrrows = count($ar_rows);

            // number of rows

  // if there is at least one line, parse the $ar_rows array

      if($nrrows>0) {
        for($i=0; $i<$nrrows; $i++) {
          // get each line and separate the user /visitor and the timestamp
          $ar_line = explode($sep, $ar_rows[$i]);
      // add in $addrow array the records in last $timeon seconds
          if($ar_line[0]!=$uvon && (intval($ar_line[1])+$timeon)>=time()) {
            $addrow[] = $ar_rows[$i];
          }
        }
      }
    }

$nruvon = count($addrow);                   // total online
$usron = '';                                    // to store the name of logged users
// traverse $addrow to get the number of visitors and users
for($i=0; $i<$nruvon; $i++) {
 if(preg_match($rgxvst, $addrow[$i])) $nrvst++;       // increment the visitors
 else {
   // gets and stores the user's name
   $ar_usron = explode($sep, $addrow[$i]);
   $usron .= '<br/> - <i>'. $ar_usron[0]. '</i>';
 }
}
$nrusr = $nruvon - $nrvst;              // gets the users (total - visitors)

// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. $nruvon. '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';

// write data in $filetxt
if(!file_put_contents($filetxt, implode("\n", $addrow))) $reout = 'Error: Recording file not exists, or is not writable';

// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";

echo $reout;             // output /display the result

Haven't tested this on the Sql script yet.

还没有在Sql脚本上进行测试。

#4


7  

for finding out that user is new or old , Get user IP .

要知道用户是新的还是旧的,请获取用户IP。

create a table for IPs and their visits timestamp .

为IPs和他们的访问时间戳创建一个表。

check IF IP does not exists OR time()-saved_timestamp > 60*60*24 (for 1 day) ,edit the IP's timestamp to time() (means now) and increase your view one .

检查IP是否不存在或时间()-saved_timestamp > 60*60*24(1天),编辑IP的时间戳到time()(表示现在),并增加您的视图1。

else , do nothing .

否则,什么都不要做。

FYI : user IP is stored in $_SERVER['REMOTE_ADDR'] variable

提示:用户IP存储在$_SERVER['REMOTE_ADDR']变量中

#5


3  

$user_ip=$_SERVER['REMOTE_ADDR'];

$check_ip = mysql_query("select userip from pageview where page='yourpage'  and userip='$user_ip'");
if(mysql_num_rows($check_ip)>=1)
{

}
else
{
  $insertview = mysql_query("insert into pageview values('','yourpage','$user_ip')");

  $updateview = mysql_query("update totalview set totalvisit = totalvisit+1 where page='yourpage' ");
}

code from talkerscode official tutorial if you have any problem http://talkerscode.com/webtricks/create-a-simple-pageviews-counter-using-php-and-mysql.php

如果您有任何问题,请使用talkerscode官方教程http://talkerscode.com/web涓流/创建-简单- pageview-反用-php- mysql.php

#1


17  

http://coursesweb.net/php-mysql/register-show-online-users-visitors_t

http://coursesweb.net/php-mysql/register-show-online-users-visitors_t

Here is a nice tutorial ,is what you need.

这里有一个很好的教程,是你需要的。

Register and show online users and visitors

注册并显示在线用户和访问者

Count Online users and visitors using a MySQL table In this tutorial you can learn how to register, to count, and display in your webpage the number of online users and visitors. The principle is this: each user / visitor is registered in a text file or database. Every time a page of the website is accessed, the php script deletes all records older than a certain time (eg 2 minutes), adds the current user / visitor and takes the number of records left to display.

在本教程中使用MySQL表计算在线用户和访问者的数量,您可以学习如何注册、计数和在网页中显示在线用户和访问者的数量。原则是:每个用户/访问者都注册在一个文本文件或数据库中。每次访问网站页面时,php脚本都会删除比特定时间(如2分钟)以上的所有记录,并添加当前用户/访问者,并记录显示的记录数。

You can store the online users and visitors in a file on the server, or in a MySQL table. In this case, I think that using a text file to add and read the records is faster than storing them into a MySQL table, which requires more requests.

您可以将在线用户和访问者存储在服务器上的文件或MySQL表中。在这种情况下,我认为使用文本文件添加和读取记录比将它们存储到MySQL表要快,这需要更多的请求。

First it's presented the method with recording in a text file on the server, than the method with MySQL table.

首先,它将在服务器上的文本文件中记录方法,而不是使用MySQL表的方法。

To download the files with the scripts presented in this tutorial, click -> Count Online Users and Visitors.

要下载本教程中提供的脚本文件,请点击->在线用户和访问者。

• Both scripts can be included in ".php" files (with include()), or in ".html" files (with ), as you can see in the examples presented at the bottom of this page; but the server must run PHP. Storing online users and visitors in a text file

•两个脚本都可以包含在“。”php“文件(包含())或in”。html“文件”,如你在本页底部的例子中所看到的;但是服务器必须运行PHP。将在线用户和访问者存储在文本文件中

To add records in a file on the server with PHP you must set CHMOD 0766 (or CHMOD 0777) permissions to that file, so the PHP can write data in it.

要使用PHP在服务器上的文件中添加记录,您必须为该文件设置CHMOD 0766(或CHMOD 0777)权限,以便PHP可以在其中写入数据。

1-Create a text file on your server (for example, named "userson.txt") and give it CHMOD 0777 permissions (in your FTP application, right click on that file, choose Properties, then select Read, Write, and Execute options). 2-Create a PHP file (named "usersontxt.php") having the code below, then copy this php file in the same directory as "userson.txt". The code for usersontxt.php

1-在服务器上创建一个文本文件(例如,名为“userson.txt”),并赋予它CHMOD 0777权限(在FTP应用程序中,右键单击该文件,选择Properties,然后选择Read、Write和Execute选项)。创建一个PHP文件(名为“usersontxt.php”),其中包含以下代码,然后将这个PHP文件复制到与“userson.txt”相同的目录中。usersontxt.php的代码

<?php
// Script Online Users and Visitors - http://coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start();        // start Session, if not already started

$filetxt = 'userson.txt';  // the file in which the online users /visitors are stored
$timeon = 120;             // number of secconds to keep a user online
$sep = '^^';               // characters used to separate the user name and date-time
$vst_id = '-vst-';        // an identifier to know that it is a visitor, not logged user

/*
 If you have an user registration script,
 replace $_SESSION['nume'] with the variable in which the user name is stored.
 You can get a free registration script from:  http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/

// get the user name if it is logged, or the visitors IP (and add the identifier)

    $uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i';         // regexp to recognize the line with visitors
$nrvst = 0;                                       // to store the number of visitors

// sets the row with the current user /visitor that must be added in $filetxt (and current timestamp)

    $addrow[] = $uvon. $sep. time();

// check if the file from $filetxt exists and is writable

    if(is_writable($filetxt)) {
      // get into an array the lines added in $filetxt
      $ar_rows = file($filetxt, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
      $nrrows = count($ar_rows);

            // number of rows

  // if there is at least one line, parse the $ar_rows array

      if($nrrows>0) {
        for($i=0; $i<$nrrows; $i++) {
          // get each line and separate the user /visitor and the timestamp
          $ar_line = explode($sep, $ar_rows[$i]);
      // add in $addrow array the records in last $timeon seconds
          if($ar_line[0]!=$uvon && (intval($ar_line[1])+$timeon)>=time()) {
            $addrow[] = $ar_rows[$i];
          }
        }
      }
    }

$nruvon = count($addrow);                   // total online
$usron = '';                                    // to store the name of logged users
// traverse $addrow to get the number of visitors and users
for($i=0; $i<$nruvon; $i++) {
 if(preg_match($rgxvst, $addrow[$i])) $nrvst++;       // increment the visitors
 else {
   // gets and stores the user's name
   $ar_usron = explode($sep, $addrow[$i]);
   $usron .= '<br/> - <i>'. $ar_usron[0]. '</i>';
 }
}
$nrusr = $nruvon - $nrvst;              // gets the users (total - visitors)

// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. $nruvon. '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';

// write data in $filetxt
if(!file_put_contents($filetxt, implode("\n", $addrow))) $reout = 'Error: Recording file not exists, or is not writable';

// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";

echo $reout;             // output /display the result
?>

3-If you want to include the script above in a ".php" file, add the following code in the place you want to show the number of online users and visitors:

3-如果你想在a中包含上面的脚本。php“文件,在你想要显示在线用户和访客数量的地方添加以下代码:

4-To show the number of online visitors /users in a ".html" file, use this code:

4-显示a中的在线访客/用户数量。html“文件,使用此代码:

<script type="text/javascript" src="usersontxt.php?uvon=showon"></script>

This script (and the other presented below) works with $_SESSION. At the beginning of the PHP file in which you use it, you must add: session_start();. Count Online users and visitors using a MySQL table

这个脚本(以及下面的另一个脚本)使用$_SESSION工作。在使用它的PHP文件的开头,必须添加:session_start();使用MySQL表对在线用户和访问者进行计数

To register, count and show the number of online visitors and users in a MySQL table, require to perform three SQL queries: Delete the records older than a certain time. Insert a row with the new user /visitor, or, if it is already inserted, Update the timestamp in its column. Select the remaining rows. Here's the code for a script that uses a MySQL table (named "userson") to store and display the Online Users and Visitors.

要在MySQL表中注册、计数和显示在线访问者和用户的数量,需要执行三个SQL查询:删除超过某个时间的记录。向新用户/访问者插入一行,或者,如果已经插入,则在其列中更新时间戳。选择剩下的行。下面是使用MySQL表(名为“userson”)存储和显示在线用户和访问者的脚本的代码。

1-First we create the "userson" table, with 2 columns (uvon, dt). In the "uvon" column is stored the name of the user (if logged in) or the visitor's IP. In the "dt" column is stored a number with the timestamp (Unix time) when the page is accessed. - Add the following code in a php file (for example, named "create_userson.php"): The code for create_userson.php

首先,我们创建“userson”表,其中有2列(uvon, dt)。在“uvon”列中存储用户名(如果登录)或访问者的IP。在“dt”列中,当访问页面时,用时间戳(Unix时间)存储一个数字。-在php文件中添加以下代码(例如,命名为create_userson.php): create_userson.php的代码。

<?php
header('Content-type: text/html; charset=utf-8');

// HERE add your data for connecting to MySQ database
$host = 'localhost';           // MySQL server address
$user = 'root';                // User name
$pass = 'password';            // User`s password
$dbname = 'database';          // Database name

// connect to the MySQL server
$conn = new mysqli($host, $user, $pass, $dbname);

// check connection
if (mysqli_connect_errno()) exit('Connect failed: '. mysqli_connect_error());

// sql query for CREATE "userson" TABLE
$sql = "CREATE TABLE `userson` (
 `uvon` VARCHAR(32) PRIMARY KEY,
 `dt` INT(10) UNSIGNED NOT NULL
 ) CHARACTER SET utf8 COLLATE utf8_general_ci"; 

// Performs the $sql query on the server to create the table
if ($conn->query($sql) === TRUE) echo 'Table "userson" successfully created';
else echo 'Error: '. $conn->error;

$conn->close();
?>

2. - Now we create the script that Inserts, Deletes, and Selects data in the "userson" table (For explanations about the code, see the comments in script).
- Add the code below in another php file (named "usersmysql.php"):
In both file you must add your personal data for connecting to MySQL database, in the variables: $host, $user, $pass, and $dbname .
The code for usersmysql.php

<?php
// Script Online Users and Visitors - coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start();         // start Session, if not already started

// HERE add your data for connecting to MySQ database
$host = 'localhost';           // MySQL server address
$user = 'root';                // User name
$pass = 'password';            // User`s password
$dbname = 'database';          // Database name

/*
 If you have an user registration script,
 replace $_SESSION['nume'] with the variable in which the user name is stored.
 You can get a free registration script from:  http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/

// get the user name if it is logged, or the visitors IP (and add the identifier)
$vst_id = '-vst-';         // an identifier to know that it is a visitor, not logged user
$uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i';         // regexp to recognize the rows with visitors
$dt = time();                                    // current timestamp
$timeon = 120;             // number of secconds to keep a user online
$nrvst = 0;                                     // to store the number of visitors
$nrusr = 0;                                     // to store the number of usersrs
$usron = '';                                    // to store the name of logged users

// connect to the MySQL server
$conn = new mysqli($host, $user, $pass, $dbname);

// Define and execute the Delete, Insert/Update, and Select queries
$sqldel = "DELETE FROM `userson` WHERE `dt`<". ($dt - $timeon);
$sqliu = "INSERT INTO `userson` (`uvon`, `dt`) VALUES ('$uvon', $dt) ON DUPLICATE KEY UPDATE `dt`=$dt";
$sqlsel = "SELECT * FROM `userson`";

// Execute each query
if(!$conn->query($sqldel)) echo 'Error: '. $conn->error;
if(!$conn->query($sqliu)) echo 'Error: '. $conn->error;
$result = $conn->query($sqlsel);

// if the $result contains at least one row
if ($result->num_rows > 0) {
  // traverse the sets of results and set the number of online visitors and users ($nrvst, $nrusr)
  while($row = $result->fetch_assoc()) {
    if(preg_match($rgxvst, $row['uvon'])) $nrvst++;       // increment the visitors
    else {
      $nrusr++;                   // increment the users
      $usron .= '<br/> - <i>'.$row['uvon']. '</i>';          // stores the user's name
    }
  }
}

$conn->close();                  // close the MySQL connection

// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. ($nrusr+$nrvst). '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';

// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";

echo $reout;             // output /display the result
?>
  1. After you have created these two php files on your server, run the "create_userson.php" on your browser to create the "userson" table.
  2. 在您的服务器上创建了这两个php文件之后,运行“create_userson”。在浏览器上创建“userson”表。
  3. Include the "usersmysql.php" file in the php file in which you want to display the number of online users and visitors.
  4. 包括“usersmysql。在php文件中显示在线用户和访问者数量的文件。
  1. Or, if you want to insert it in a ".html" file, add this code:
  2. 或者,如果你想把它插入a中。html文件,添加以下代码:

Examples using these scripts

使用这些示例脚本

• Including the "usersontxt.php" in a php file:

•包括“usersontxt。php文件中的php:

Counter Online Users and Visitors

• Including the "usersmysql.php" in a html file: Counter Online Users and Visitors

•包括“usersmysql。在html文件中:计数器在线用户和访问者。

Both scripts (with storing data in a text file on the server, or into a MySQL table) will display a result like this: Online: 5

这两个脚本(将数据存储在服务器上的文本文件或MySQL表中)都将显示如下结果:Online: 5

Visitors: 3 Users: 2 - MarPlo - Marius

访客:3位用户:2 - MarPlo - Marius

#2


10  

Unique views is always a hard nut to crack. Checking the IP might work, but an IP can be shared by more than one user. A cookie could be a viable option, but a cookie can expire or be modified by the client.

独特的观点总是一个难题。检查IP可能有用,但是一个IP可以由多个用户共享。cookie可以是一个可行的选项,但是cookie可以过期或由客户端修改。

In your case, it don't seem to be a big issue if the cookie is modified tho, so i would recommend using a cookie in a case like this. When the page is loaded, check if there is a cookie, if there is not, create one and add a +1 to views. If it is set, don't do the +1.

在您的情况下,如果cookie被修改为tho,似乎不是什么大问题,所以我建议在这种情况下使用cookie。加载页面时,检查是否有cookie,如果没有,创建一个,并向视图添加+1。如果它被设置,不要做+1。

Set the cookies expiration date to whatever you want it to be, week or day if that's what you want, and it will expire after that time. After expiration, it will be a unique user again!

将cookie过期日期设置为您想要的日期,如果您想要,可以是周或日,那么它将在此之后过期。过期后,它将再次成为唯一的用户!


Edit:
Thought it might be a good idea to add this notice here...
Since around the end of 2016 a IP address (static or dynamic) is seen as personal data in the EU.
That means that you are only allowed to store a IP address with a good reason (and I'm not sure if tracking views is a good reason). So if you intend to store the IP address of visitors, I would recommend hashing or encrypting it with a algorithm which can not be reversed, to make sure that you are not breaching any law (especially after the GDPR laws have been implemented).

编辑:我想在这里加上这个通知可能是个好主意……由于2016年年底前后,在欧盟,IP地址(静态或动态)被视为个人数据。这意味着您只能存储一个有充分理由的IP地址(我不确定跟踪视图是否是一个好的理由)。因此,如果你想要存储访问者的IP地址,我建议用一个无法撤销的算法来对它进行哈希或加密,以确保你没有违反任何法律(尤其是在GDPR法律实施之后)。

#3


8  

I have edited the "Best answer" code, though I found a useful thing that was missing. This is will also track the ip of a user if they are using a Proxy or simply if the server has nginx installed as a proxy reverser.

我已经编辑了“最佳答案”代码,尽管我发现了一个有用的东西缺失了。如果用户使用的是代理,或者只是服务器有nginx作为代理反向器安装,那么这也将跟踪用户的ip。

I added this code to his script at the top of the function:

我把这段代码添加到他在函数顶部的脚本中:

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
$adresseip = getRealIpAddr();

Afther that I edited his code.

然后我编辑了他的代码。

Find the line that says the following:

找到这样一行:

// get the user name if it is logged, or the visitors IP (and add the identifier)

    $uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

and replace it with this:

把它替换为:

$uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $adresseip. $vst_id;

This will work.

这将工作。

Here is the full code if anything happens:

如果发生什么事情,这里有完整的代码:

<?php

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
$adresseip = getRealIpAddr();

// Script Online Users and Visitors - http://coursesweb.net/php-mysql/
if(!isset($_SESSION)) session_start();        // start Session, if not already started

$filetxt = 'userson.txt';  // the file in which the online users /visitors are stored
$timeon = 120;             // number of secconds to keep a user online
$sep = '^^';               // characters used to separate the user name and date-time
$vst_id = '-vst-';        // an identifier to know that it is a visitor, not logged user

/*
 If you have an user registration script,
 replace $_SESSION['nume'] with the variable in which the user name is stored.
 You can get a free registration script from:  http://coursesweb.net/php-mysql/register-login-script-users-online_s2
*/

// get the user name if it is logged, or the visitors IP (and add the identifier)

    $uvon = isset($_SESSION['nume']) ? $_SESSION['nume'] : $_SERVER['SERVER_ADDR']. $vst_id;

$rgxvst = '/^([0-9\.]*)'. $vst_id. '/i';         // regexp to recognize the line with visitors
$nrvst = 0;                                       // to store the number of visitors

// sets the row with the current user /visitor that must be added in $filetxt (and current timestamp)

    $addrow[] = $uvon. $sep. time();

// check if the file from $filetxt exists and is writable

    if(is_writable($filetxt)) {
      // get into an array the lines added in $filetxt
      $ar_rows = file($filetxt, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
      $nrrows = count($ar_rows);

            // number of rows

  // if there is at least one line, parse the $ar_rows array

      if($nrrows>0) {
        for($i=0; $i<$nrrows; $i++) {
          // get each line and separate the user /visitor and the timestamp
          $ar_line = explode($sep, $ar_rows[$i]);
      // add in $addrow array the records in last $timeon seconds
          if($ar_line[0]!=$uvon && (intval($ar_line[1])+$timeon)>=time()) {
            $addrow[] = $ar_rows[$i];
          }
        }
      }
    }

$nruvon = count($addrow);                   // total online
$usron = '';                                    // to store the name of logged users
// traverse $addrow to get the number of visitors and users
for($i=0; $i<$nruvon; $i++) {
 if(preg_match($rgxvst, $addrow[$i])) $nrvst++;       // increment the visitors
 else {
   // gets and stores the user's name
   $ar_usron = explode($sep, $addrow[$i]);
   $usron .= '<br/> - <i>'. $ar_usron[0]. '</i>';
 }
}
$nrusr = $nruvon - $nrvst;              // gets the users (total - visitors)

// the HTML code with data to be displayed
$reout = '<div id="uvon"><h4>Online: '. $nruvon. '</h4>Visitors: '. $nrvst. '<br/>Users: '. $nrusr. $usron. '</div>';

// write data in $filetxt
if(!file_put_contents($filetxt, implode("\n", $addrow))) $reout = 'Error: Recording file not exists, or is not writable';

// if access from <script>, with GET 'uvon=showon', adds the string to return into a JS statement
// in this way the script can also be included in .html files
if(isset($_GET['uvon']) && $_GET['uvon']=='showon') $reout = "document.write('$reout');";

echo $reout;             // output /display the result

Haven't tested this on the Sql script yet.

还没有在Sql脚本上进行测试。

#4


7  

for finding out that user is new or old , Get user IP .

要知道用户是新的还是旧的,请获取用户IP。

create a table for IPs and their visits timestamp .

为IPs和他们的访问时间戳创建一个表。

check IF IP does not exists OR time()-saved_timestamp > 60*60*24 (for 1 day) ,edit the IP's timestamp to time() (means now) and increase your view one .

检查IP是否不存在或时间()-saved_timestamp > 60*60*24(1天),编辑IP的时间戳到time()(表示现在),并增加您的视图1。

else , do nothing .

否则,什么都不要做。

FYI : user IP is stored in $_SERVER['REMOTE_ADDR'] variable

提示:用户IP存储在$_SERVER['REMOTE_ADDR']变量中

#5


3  

$user_ip=$_SERVER['REMOTE_ADDR'];

$check_ip = mysql_query("select userip from pageview where page='yourpage'  and userip='$user_ip'");
if(mysql_num_rows($check_ip)>=1)
{

}
else
{
  $insertview = mysql_query("insert into pageview values('','yourpage','$user_ip')");

  $updateview = mysql_query("update totalview set totalvisit = totalvisit+1 where page='yourpage' ");
}

code from talkerscode official tutorial if you have any problem http://talkerscode.com/webtricks/create-a-simple-pageviews-counter-using-php-and-mysql.php

如果您有任何问题,请使用talkerscode官方教程http://talkerscode.com/web涓流/创建-简单- pageview-反用-php- mysql.php