Browse > Home / Webmaster Tutorials / Simple PHP Users Online Script

| Subcribe via RSS

Simple PHP Users Online Script

May 30th, 2009 Posted in Webmaster Tutorials

It’s always handy to have a simple script on your website that allows the people who are browsing your site to see how many users that are online. In this post I will be showing you all how to implement such a feature into your website. it takes upt o 5 minutes and then everything is done. The only thing that you need is a mySQL database and PHP enabled on your server (most servers have this enabled by default unless you are having a REALLY crappy host).

First of all you need to create a table in a database by simply running this SQML query (or you can create it manually):

1)Create table

CREATE TABLE `user_online` (
`session` char(100) NOT NULL default ”,
`time` int(11) NOT NULL default ‘0′
) TYPE=MyISAM;

2) Create user_online.php with the following code (Edit database variables!):

session_start();
$session=session_id();
$time=time();
$time_check=$time-600; //SET TIME 10 Minute

$host=”localhost”; // Host name
$username=””; // Mysql username
$password=””; // Mysql password
$db_name=”test”; // Database name
$tbl_name=”user_online”; // Table name

// Connect to server and select databse
mysql_connect(”$host”, “$username”, “$password”)or die(”cannot connect to server”);
mysql_select_db(”$db_name”)or die(”Database error! Visit warezteacher.com!”);

$sql=”SELECT * FROM $tbl_name WHERE session=’$session’”;
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==”0″){
$sql1=”INSERT INTO $tbl_name(session, time)VALUES(’$session’, ‘$time’)”;
$result1=mysql_query($sql1);
}
else {
“$sql2=UPDATE $tbl_name SET time=’$time’ WHERE session = ‘$session’”;
$result2=mysql_query($sql2);
}

$sql3=”SELECT * FROM $tbl_name”;
$result3=mysql_query($sql3);

$count_user_online=mysql_num_rows($result3);

echo “User online : $count_user_online “;

// Delete sessions that are over 10 minutes
$sql4 = “DELETE FROM $tbl_name WHERE time<$time_check”;

$result4 = mysql_query($sql4);
mysql_close();
?>

3) Include user_online.php on the page(s)

You can do this by adding: onto the page where you would like them displayed. This doens’t work on .HTML files because they don’t support the use of PHP code (obvious!)

Related posts:

  1. Simple login system with php Today I will be creating my first post for WarezTeacher...
  2. PHP Image Resize Script This is a little script that is very handy for...

Related posts brought to you by Yet Another Related Posts Plugin.

2 Responses to “Simple PHP Users Online Script”

  1. Simple PHP Users Online Script | Warez Teacher Says:

    [...] the original post: Simple PHP Users Online Script | Warez Teacher Share and [...]


  2. Michael Says:

    amazing stuff thanx :)


Leave a Reply

You must be logged in to post a comment.