Online User counters for PHP

Source: Internet
Author: User
Tags mysql tutorial php tutorial sessions set time

Now I'm going to show you how many users are browsing your site. There are many ways to show it, but I try to keep it simple and clean.

First, we must create two tables for your database tutorial.

CREATE TABLE ' Uonline ' (
' Session ' varchar (m) not NULL,
' Time ' int (5) is not NULL default ' 0 '
) Engine=myisam DEFAULT charset=latin1;

This will create two tables. One is the meeting and the other is the time. They will store our information.

Now it's time for PHP. Let's create our main variables together.

<?php Tutorial
Session_Start ();
$ses = session_id ();
$time = time ();
$timech = $time-300;
Session_Start (); -This Wills start a session
$ses = session_id (); -This'll get my session ' s ID
$time = time (); -This'll get the time
$timech = $time-300; -This'll set our time to 5min

Now we have to connect to the database:


Declare SQL Login Info
$host = "localhost";
$username = "";
$password = "";
$dbname = "";

Now we are going to connect to our database
MySQL tutorial _connect ("$host", "$username", "$password") or Die ("<font style= ' color:red ' ><b>can not connect: </b> ". Mysql_error ()." </font> ");
mysql_select_db ("$db _name") or Die (' <font style= ' color:red ' ><b>can ' not select the Database:</b> '). Mysql_error (). " </font> ");

$host = "localhost"; -Your SQL ' s host name, usually it ' localhost '
$username = ""; -Your MySQL username
$password = ""; -Your MySQL Password
$dbname = ""; -Your database ' s name

Now we have to look for existing sessions and get the number of sessions.


$result = mysql_query ("select * from Uonline WHERE session= ' $ses '");
$num = mysql_num_rows ($result);

$result = mysql_query ("select * from Uonline WHERE session= ' $ses '"); -this would select all info from session column where it's value is "$ses"
$num = mysql_num_rows ($result); -It'll tell us to many active records are in session column


if ($num = = "0") {
$result 1 = mysql_query ("INSERT into Uonline (sessions, Time) VALUES (' $ses ', ' $time ')");
}else{
$result 2 = mysql_query ("UPDATE uonline SET time= ' $time ' WHERE session = ' $ses '");
}

if ($num = = "0") {-If there ' s no records in session column then we must insert some records
$result 1 = mysql_query ("INSERT into Uonline (sessions, Time) VALUES (' $ses ', ' $time ')"); -Inserts session ' s ID and time to database
}else{-But If there is more records than 0, let ' s update their records
$result 2 = mysql_query ("UPDATE uonline SET time= ' $time ' WHERE session = ' $ses '"); -Updates Existing Records
}-Ends If statement

Now let's find we info again from the columns:


$result 3 = mysql_query ("SELECT * from Uonline");

$result 3 = mysql_query ("SELECT * from Uonline"); -this'll get all info from Uonline table

It ' s time to showing how many users are looking your site:


$usersonline = mysql_num_rows ($result 3);
echo "There are: <b>". $usersonline. " </b> users online ";

$usersonline = mysql_num_rows ($result 3); -It'll get the number of records in columns
echo "There are: <b>". $usersonline. " </b> users online "; -This'll show how to many users are on your site

When the users have left, you are must delete their records from database.


mysql_query ("DELETE from Uonline WHERE time< $timech");
?>

mysql_query ("DELETE from Uonline WHERE time< $timech"); -Deletes records from database when 5min has been thru.

And here's the full code:


<?php
Session_Start ();
$ses = session_id ();
$time = time ();
$timech = $time-300;

$host = "localhost";
$username = "";
$password = "";
$dbname = "";

Mysql_connect ("$host", "$username", "$password") or Die ("<font style= ' color:red ' ><b>can not connect:</ B> ". Mysql_error ()." </font> ");
mysql_select_db ("$db _name") or Die (' <font style= ' color:red ' ><b>can ' not select the Database:</b> '). Mysql_error (). " </font> ");

$result = mysql_query ("select * from Uonline WHERE session= ' $ses '");
$num = mysql_num_rows ($result);

if ($num = = "0") {
$result 1 = mysql_query ("INSERT into Uonline (sessions, Time) VALUES (' $ses ', ' $time ')");
}else{
$result 2 = mysql_query ("UPDATE uonline SET time= ' $time ' WHERE session = ' $ses '");
}

$result 3 = mysql_query ("SELECT * from Uonline");

$usersonline = mysql_num_rows ($result 3);
echo "There are: <b>". $usersonline. " </b> users online ";

mysql_query ("DELETE from Uonline WHERE time< $timech");
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.