Hello my friends, this is my first tutorial here, so I hope you enjoy it .
In this tutorial we will be creating an online visitors counter. You should have knowledge of the following:-
PHP and using sessions
MySQL
---------------------------Let's get started---------------------------
First we need a MySQL table to store our information, I'll be using a table structured of two fields, one is the session_id and the other is the time.
CREATE TABLE online_visitors(
session_id CHAR(100) NOT NULL DEFAULT '',
time INT(11) NOT NULL DEFAULT '0'
);
Now, let's get to PHP and make sure to read the comments
Note: you may use the following code as a function or you may save it in a file and include it, it's up to you. As for me, I will save it in a separate file .
Firstly, I will store my database connection information in a separate file and include it in my counter file. The database connection file is called dbc.php and in contains the following code:-
dbc.php
<?php
//connect to the database
$dbc = mysql_connect("localhost","root","") or die("Couldn't connect to the database server<br />".mysql_error());
//select the database 'test'
$dbs = mysql_select_db("test") or die("Couldn't select to the database<br />".mysql_error());
?>
counter.php
<?php
require("dbc.php"); //Require the database connection
//First we have to start the session only if it has not been started
if(!isset($_SESSION)){
session_start();
}
$session_id = session_id(); //We assign the session id to the variable $session_id
$time = time(); //We assign the current time to the variable $time
$time_limit = $time-600; //We give the session only 10 minutes if it exists
/**
*We need to check the session_id is already stored or not
*/
$num = mysql_num_rows(mysql_query("SELECT * FROM online_visitors WHERE session_id='{$session_id}' LIMIT 1"));
//if it doesn't exist, then we'll store it
//And if does exist, we will update the session's time in the DB
if($num != 1){
$sql = "INSERT INTO online_visitors VALUES('{$session_id}','{$time}')";
$query = mysql_query($sql) or die("Error<br />".mysql_error());
}else{
$sql = "UPDATE online_visitors SET time='{$time}' WHERE session_id='{$session_id}'";
$query = mysql_query($sql) or die("Error<br />".mysql_error());
}
//Now the following code is to get the count of visitors currently online
$noov = mysql_num_rows(mysql_query("SELECT * FROM online_visitors")); //noov = number of online visitors
echo 'Visitors currently online: ',$noov;
//Now, check if the session was stored for more than 10 minutes and delete it if it is.
$delete_session = mysql_query("DELETE FROM online_visitors WHERE time<'{$time_limit}'");
//close the connection
mysql_close();
?>
Now, let's get to the explanation:-
1: We required the dbc.php which contains the database connection information
2: We are checking if the session has been already started, if it's not, then start the session
3: We assign the session id to the variable $session_id
4: We created two variables, one is the current time and the other contains the current time minus 10 minutes
5: We need to check the session_id is already stored or not, so we query the database and see if it doesn't exist, then we'll store it and if it does exist, we will update the session's time in the DB
6:We get the count of the total session in the table and print it.
7: We then check if the session has been in the database for more than 10 minutes, if it is, then we will delete it.
This is it, I hope you enjoyed this easy tutorial.
In this tutorial we will be creating an online visitors counter. You should have knowledge of the following:-
PHP and using sessions
MySQL
---------------------------Let's get started---------------------------
First we need a MySQL table to store our information, I'll be using a table structured of two fields, one is the session_id and the other is the time.
CREATE TABLE online_visitors(
session_id CHAR(100) NOT NULL DEFAULT '',
time INT(11) NOT NULL DEFAULT '0'
);
Now, let's get to PHP and make sure to read the comments
Note: you may use the following code as a function or you may save it in a file and include it, it's up to you. As for me, I will save it in a separate file .
Firstly, I will store my database connection information in a separate file and include it in my counter file. The database connection file is called dbc.php and in contains the following code:-
dbc.php
<?php
//connect to the database
$dbc = mysql_connect("localhost","root","") or die("Couldn't connect to the database server<br />".mysql_error());
//select the database 'test'
$dbs = mysql_select_db("test") or die("Couldn't select to the database<br />".mysql_error());
?>
counter.php
<?php
require("dbc.php"); //Require the database connection
//First we have to start the session only if it has not been started
if(!isset($_SESSION)){
session_start();
}
$session_id = session_id(); //We assign the session id to the variable $session_id
$time = time(); //We assign the current time to the variable $time
$time_limit = $time-600; //We give the session only 10 minutes if it exists
/**
*We need to check the session_id is already stored or not
*/
$num = mysql_num_rows(mysql_query("SELECT * FROM online_visitors WHERE session_id='{$session_id}' LIMIT 1"));
//if it doesn't exist, then we'll store it
//And if does exist, we will update the session's time in the DB
if($num != 1){
$sql = "INSERT INTO online_visitors VALUES('{$session_id}','{$time}')";
$query = mysql_query($sql) or die("Error<br />".mysql_error());
}else{
$sql = "UPDATE online_visitors SET time='{$time}' WHERE session_id='{$session_id}'";
$query = mysql_query($sql) or die("Error<br />".mysql_error());
}
//Now the following code is to get the count of visitors currently online
$noov = mysql_num_rows(mysql_query("SELECT * FROM online_visitors")); //noov = number of online visitors
echo 'Visitors currently online: ',$noov;
//Now, check if the session was stored for more than 10 minutes and delete it if it is.
$delete_session = mysql_query("DELETE FROM online_visitors WHERE time<'{$time_limit}'");
//close the connection
mysql_close();
?>
Now, let's get to the explanation:-
1: We required the dbc.php which contains the database connection information
2: We are checking if the session has been already started, if it's not, then start the session
3: We assign the session id to the variable $session_id
4: We created two variables, one is the current time and the other contains the current time minus 10 minutes
5: We need to check the session_id is already stored or not, so we query the database and see if it doesn't exist, then we'll store it and if it does exist, we will update the session's time in the DB
6:We get the count of the total session in the table and print it.
7: We then check if the session has been in the database for more than 10 minutes, if it is, then we will delete it.
This is it, I hope you enjoyed this easy tutorial.