Counter Online Users and Visitors

Register and show online users and visitors

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 ), as you can see in the examples presented at the bottom of this page; but the server must run PHP.

Читайте также:  Револьвер kwc colt python 6 inch co2 kc 68dhn

Storing online users and visitors in a text file

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 .

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

0) < for($i=0; $i=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 - '. $ar_usron[0]. ''; > > $nrusr = $nruvon - $nrvst; // gets the users (total - visitors) // the HTML code with data to be displayed $reout = '

Online: '. $nruvon. '

Visitors: '. $nrvst. '
Users: '. $nrusr. $usron. '
'; // 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 , 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:

Читайте также:  Java reference object with string

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

  1. Delete the records older than a certain time.
  2. Insert a row with the new user /visitor, or, if it is already inserted, Update the timestamp in its column.
  3. Select the remaining rows.

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(); ?>

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

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 , 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. — After you have created these two php files on your server, run the «create_userson.php» on your browser to create the «userson» table.
4. — Include the «usersmysql.php» file in the php file in which you want to display the number of online users and visitors.

Examples using these scripts

           
       

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

Источник

Webslesson

PHP, MySql, Jquery, AngularJS, Ajax, Codeigniter, Laravel Tutorial

Wednesday, 16 August 2017

Display Online Users using PHP Mysql with Ajax Jquery




How to Show Live Online User using PHP with Ajax Jquery — 2

It is very important for any website to find out who are currently login into our syste or who are online. Because it will increase the value of website into another user who has online to system. This type of feature most we have seen in social media or forum site. So in this post we have discuss topic like display user information who are currently login into our website by using simple PHP Script Mysql database with Ajax JQuery. If you are using internet and you have already seen on many website in which we can seen the number of online users. So displaying online user details may create nice image on the new visitors of websites.

For create this type of feature there are following steps to create this type of feature.

1. Store User Login Information in table
2. Update Last Activity of Login user into his login information
3. Fetch Online User data from table

Store User Login Information in table

First when user has login into our system then we have to save his login information like his id, login time into our login details database. When login into our system by entering correct login details then after we have create one SESSION variable login id in which we have store the id of login details id based on this id we can update his last activity table column data with current time stamp.

Update Last Activity of Login user into his login information

After storing user login information in table and generate one unique login id and store into SESSION variable, so we can access this variable value into accros our system. So when user logged into system after this we will update current time stamp into his last activity table column into login details based on the unique value of login id get from SESSION variable. For update time stamp data on every three second we have use Ajax with JQuery. For update last activity data we have make one jquery function and in that function we have use Ajax request which send request to server for update login user last activity data and by using JQuery setInterval method we have called this function on every three seconds. So this way we have update login user last activity data with current time stamp data by using Ajax JQuer with PHP Sever script.

Fetch Online User data from table

Now We have already discuss how logged in user data insert into login details details table and how we have update that login used login details every three seconds, now we have move to master user login. This master user can view who is currently logged into our website. So for this we have make one jquery function and in this function we have use Ajax request which send request to PHP server script for fetch data from login details table and here we have condition like it fetch only that data whose last activity table column data is current time stamp data minus five second. That means it only fetch that user data whose last activity time stamp data less than of current time stamp minus five. So this query will return only that user data who currently online with our system or use our system. After fetching this data we have use setInterval() method, by using this method we have called this jquery function on every three second. So on every three second it will display live online user data on web page.

So this is simple system in which we have discuss topic like how to display online information by using PHP PDO with Ajax JQuery and Mysql.

Источник

Оцените статью