[PHP & MySQL] Realtime users online script
Introduction
I have been searching the internet for guide how to make a script to show how many users are connected. However, i didn't manage to find one, so i made it myself.
I am creating this guide for people who also aren't really experienced with PHP and is searching for something like this. This script may be a mess, but it works. :)
Creating a column in database
You have to create a new column in table (preferably at the end of a table). I used column named "online", so i will do the same here.
Important: Don't forget to edit your register.php and add one more ,' ' in registration code...
http://img715.imageshack.us/img715/4602/online1g.jpg
Editing login.php (it is going to change online column value from 0 to 1)
Since i think, that you already created database connection in login script, i think that this will be enough to show you. Thus, you should add it before you echo that login was successful.
Code:
// inserting online state
mysql_query("
UPDATE yourtable SET online='1' WHERE username='$username'"
) or die(mysql_error());
Editing logout.php (from 1 to 0)
I think that you haven't made any connections to database in this file, so i will include the whole code.
Code:
<?php
session_start();
//File which connects to database and selects table
include 'db_connect.php';
//Since my session is based on persons username... This code selects the user which is doing this proccess... (i think you already know that)
$user = $_SESSION['username'];
$query = mysql_query("SELECT * FROM yourtable WHERE username='$user'");
$numrows = mysql_num_rows($query);
// Inserting offline state
mysql_query("
UPDATE yourtable SET online='0' WHERE username='$user'"
) or die(mysql_error());
session_destroy();
echo "You have been logged out.";
?>
Users_online.php script
Checks how many rows of "online" column is set to 1 and gives the answer. Answer is the count of online (logged-in) users. :)
Code:
<?php
session_start();
include 'db_connect.php';
$query = mysql_query("SELECT * FROM yourtable WHERE online='1'");
$numrows = mysql_num_rows($query);
echo $numrows;
?>
Sorry for my bad english, if you think that it is THAT bad ;DD
And hope this will save some time for newbies at PHP. :)
Re: [PHP & MySQL] Realtime users online script
And after member login to your site and presses [X] it will leave online column to 1
Re: [PHP & MySQL] Realtime users online script
dang, u rather should work with an INT column that contains the timestamp and check the online users of the last 5 minutes. on every site action the user must update the timestamp with the current timestamp in the database to tell it he/she is online (active) again. on logout the users timestamp simply can be set to 0, so it wont be tracked in any case.
your online user php script simply counts all entries with a timestamp BIGGER than the current timestamp decreases by 300 to get all active users within the last 5 minutes.
cheers.
Re: [PHP & MySQL] Realtime users online script
It's unnecessary to use the Sql database for this, alternatively you can use the PHP file IO function instead - store the data on a txt file.
Spread the workload, in this instance use PHP only, reserve the SQL priority to other database related tasks.
Re: [PHP & MySQL] Realtime users online script
Quote:
Originally Posted by
VibeTribe
dang, u rather should work with an INT column that contains the timestamp and check the online users of the last 5 minutes. on every site action the user must update the timestamp with the current timestamp in the database to tell it he/she is online (active) again. on logout the users timestamp simply can be set to 0, so it wont be tracked in any case.
your online user php script simply counts all entries with a timestamp BIGGER than the current timestamp decreases by 360 to get all active users within the last 5 minutes.
cheers.
I did like that. I didn't even knew how other people does it before I tried it, but it works.
For thread.
It can be done with $currTime = time(); and $Time = time()+300;
$Time is posted to database and site checks:
PHP Code:
$q = mysql_query("SELECT * FROM users WHERE online <= '$currTime'");
echo mysql_num_rows($q);
Re: [PHP & MySQL] Realtime users online script
Well,
PHP User online tutorial
Is actually a good solution to this :P...
Re: [PHP & MySQL] Realtime users online script
Quote:
Originally Posted by
Justei
Yeps. Not bad at all, but I think it's much easier to explain what it need to do in theory instead of reading full already made script.
(IMO)
Re: [PHP & MySQL] Realtime users online script
Quote:
Originally Posted by
getty
It's unnecessary to use the Sql database for this, alternatively you can use the PHP file IO function instead - store the data on a txt file.
Spread the workload, in this instance use PHP only, reserve the SQL priority to other database related tasks.
Are you sure? File I/O isn't very fast... Besides, a connection to a SQL database is likely already established if the application is using a user database in the first place.
Re: [PHP & MySQL] Realtime users online script
Re: [PHP & MySQL] Realtime users online script
Do you drive a monster truck? because you're a gravedigger.
Re: [PHP & MySQL] Realtime users online script
Quote:
$query = mysql_query("SELECT * FROM yourtable WHERE online='1'");
Just a tip. When you do querys where you don't pass any variables inside of them, you should not use "", use ' instead. It's all about performance. In cases of big applications this things can do major inpact on the over-all performance.
Re: [PHP & MySQL] Realtime users online script
Quote:
Originally Posted by
Macbeth
Just a tip. When you do querys where you don't pass any variables inside of them, you should not use "", use ' instead. It's all about performance. In cases of big applications this things can do major inpact on the over-all performance.
there is no matter what to choose, since " or ' is the same result. tho an integer variable shouldnt have any ' in the statement + this topic is outdated, why u bump it up with such unnecessary things?
Re: [PHP & MySQL] Realtime users online script
Quote:
Originally Posted by
Macbeth
Just a tip. When you do querys where you don't pass any variables inside of them, you should not use "", use ' instead. It's all about performance. In cases of big applications this things can do major inpact on the over-all performance.
Some professionals believe that premature optimization is the root of all evil. The problem with "s is they are inconsistent. Given the choice, you should ALWAYS use single quotes because of the inconsistencies found in "s.
Quote:
Originally Posted by
VibeTribe
there is no matter what to choose, since " or ' is the same result. tho an integer variable shouldnt have any ' in the statement + this topic is outdated, why u bump it up with such unnecessary things?
" and ' is not the same result in PHP. Macbeth correctly notes that you can pass variables inside of double quotes, and not single quotes. Double quotes may result in silent unexpected results, making them dangerous to use. In fact, I find most features in PHP are dangerous to use due to inconsistencies, so I try very hard not to use the language.
Mk, now my turn to post.
I don't see what makes this Realtime... Clients are needed to display up-to-date content on-the-fly, you just supplied server-side code..
Re: [PHP & MySQL] Realtime users online script
Quote:
Originally Posted by
VibeTribe
there is no matter what to choose, since " or ' is the same result. tho an integer variable shouldnt have any ' in the statement + this topic is outdated, why u bump it up with such unnecessary things?
It doesn't matter if it's outdated or not, if someone has got some valuable content to post to the thread then why shouldn't it be posted. Just like now, you learned something new with these "'s which you obviously didn't know before.
Regarding if there's any matter or not to the use of the "'s I refer you to s-p-n's quote as there is no need to re-write it.
Re: [PHP & MySQL] Realtime users online script
I wrote in the rules that this Tutorial's section in particular (of Coder's Paradise) is never outdated. If you find something useful to add, add it! This section is not for the thread starter, it's for passer-by's searching via google or coming here to find a specific tutorial. Most threads will go out-dated in two weeks, tutorials are always useful- but do get outdated. If a tutorial gets outdated, we should reply to improve it.