gpt4 book ai didi

php - 如何计算网站的唯一身份访问者?

转载 作者:IT老高 更新时间:2023-10-28 23:46:38 28 4
gpt4 key购买 nike

我正在为用户帖子创建一个访客计数系统,以显示首页上访问量最高的帖子。我现在有一个访客计数系统,但是所有页面刷新时都会注册一个 View 。我无法使用Google Analytics(分析)。

我需要的是一个访客计数器,只计算不重复访客的数量。就我而言,独特意味着一个人一天只能查看一个帖子?我认为甚至一个星期都可以。你可以在这里写那个php代码吗?如果您也喜欢的话,也可以给我链接一些优秀的教程。

这是代码需要执行的操作(或等效操作):

  • 页面加载后,检查访问者是新的还是老的(我不知道该怎么做..)
  • 如果他老了,请忽略他
  • 如果他是新手,在mysql中,views = views + 1
  • 最佳答案

    这是一个不错的教程,这是您所需要的。
    (来源:coursesweb.net/php-mysql)
    注册并显示在线用户和访客
    使用MySQL表统计在线用户和访客
    在本教程中,您可以学习如何注册,计数和在您的网页中显示在线用户和访问者的数量。
    原理是这样的:每个用户/访问者都在文本文件或数据库中注册。每次访问网站页面时,php脚本都会删除所有超过特定时间(例如2分钟)的记录,添加当前用户/访问者,并显示剩余的记录数。
    您可以将在线用户和访问者存储在服务器上的文件或MySQL表中。
    在这种情况下,我认为使用文本文件来添加和读取记录要比将它们存储到需要更多请求的MySQL表中更快。
    首先,它介绍了在服务器上以文本文件记录的方法,而不是使用MySQL表的方法。
    要使用本教程中介绍的脚本下载文件,请单击-> Count Online Users and Visitors
    •这两个脚本都可以包含在“.php”文件(带有include())中,也可以包含在“.html”文件(带有<script>)中,如您在本页底部提供的示例中所见;但是服务器必须运行PHP。
    将在线用户和访客存储在文本文件中
    要使用PHP在服务器上的文件中添加记录,必须为该文件设置CHMOD 0766(或CHMOD 0777)权限,以便PHP可以在其中写入数据。

  • 在服务器上创建一个文本文件(例如,名为userson.txt)并为其授予CHMOD 0777权限(在FTP应用程序中,右键单击该文件,选择“属性”,然后选择ReadWriteExecute选项)。
  • 创建一个具有以下代码的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
    ?>
  • 如果要将上面的脚本包含在“.php”文件中,请在要显示在线用户和访问者数量的位置添加以下代码:

  • 4.要显示“.html”文件中的在线访问者/用户数,请使用以下代码:
    <script type="text/javascript" src="usersontxt.php?uvon=showon"></script>
    该脚本(以及下面提供的其他脚本)与$ _SESSION一起使用。在使用它的PHP文件的开头,必须添加:session_start();。
    使用MySQL表统计在线用户和访客
    要在MySQL表中注册,计数和显示在线访问者和用户数,需要执行三个SQL查询:
    删除早于特定时间的记录。
    使用新用户/visitor插入一行,或者如果已插入新用户/visitor,则在其列中更新时间戳。
    选择其余的行。
    这是使用MySQL表(名为“userson”)存储和显示在线用户和访问者的脚本的代码。
  • 首先,我们创建带有两列(uvon,dt)的“userson”表。在“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();
    ?>
  • 现在,我们在userson表中创建用于插入,删除和选择数据的脚本(有关代码的说明,请参见脚本中的注释)。
  • 在另一个php文件(名为usersmysql.php)中添加以下代码:
    在这两个文件中,您都必须在变量$host$user$pass$dbname中添加用于连接MySQL数据库的个人数据。

  • 用户的代码mysql.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
    ?>
  • 在服务器上创建这两个php文件之后,在浏览器上运行“create_userson.php”以创建“userson”表。
  • 在要显示在线用户和访问者数量的php文件中包括usersmysql.php文件。

  • 或者,如果要将其插入“.html”文件中,请添加以下代码:

  • 使用这些脚本的示例
    •在php文件中包含“usersontxt.php”:




    对抗在线用户和访客





    •在HTML文件中包含“usersmysql.php”:
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>Counter Online Users and Visitors</title>
    <meta name="description" content="PHP script to count and show the number of online users and visitors" />
    <meta name="keywords" content="online users, online visitors" />
    </head>
    <body>

    <!-- Includes the script ("usersontxt.php", or "usersmysql.php") -->
    <script type="text/javascript" src="usersmysql.php?uvon=showon"></script>

    </body>
    </html>
    这两个脚本(将数据存储在服务器上的文本文件中或MySQL表中)将显示如下结果:
    在线人数:5
    参观人数:3
    用户数:2
  • MarPlo
  • 马里乌斯
  • 关于php - 如何计算网站的唯一身份访问者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18799808/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com