How do I count unique visitors to my site?

前端 未结 5 643
粉色の甜心
粉色の甜心 2020-11-27 14:09

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 pag

5条回答
  •  甜味超标
    2020-11-27 14:38

    Here is a nice tutorial, it is what you need. (Source: coursesweb.net/php-mysql)

    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.

    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.

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

    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

    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

    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.

    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:

    query($sql) === TRUE) echo 'Table "userson" successfully created';
    else echo 'Error: '. $conn->error;
    
    $conn->close();
    ?>
    
    1. 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:

    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 .= '
    - '.$row['uvon']. ''; // stores the user's name } } } $conn->close(); // close the MySQL connection // the HTML code with data to be displayed $reout = '

    Online: '. ($nrusr+$nrvst). '

    Visitors: '. $nrvst. '
    Users: '. $nrusr. $usron. '
    '; // if access from

    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

    Visitors: 3 Users: 2

    • MarPlo
    • Marius

提交回复
热议问题