Search
Left Quote    By failing to prepare, you are preparing to fail.
- Benjamin Franklin    
Right Quote
 
[login] | [Register]
 

Users Online

by: bs0d
 

Description:


First, you will need to create a table `users_online` in your database (and have access to one). Also, create at least 3 columns (id, ip and time) set as: int(11), varchar(15) and varchar(11).

Next, you need the code to appear in your page_header, you can save the code as a file, and include it; or just paste the code directly in your header.

Finally, you can add alot to this code, and use the table to show "who" is online as well.

Code:
<?

//REMEMBER TO CONNECT TO THE DATABASE!!

$ip =            $_SERVER['REMOTE_ADDR'];
$time =             time();

$ip_check 'SELECT ip FROM `users_online` WHERE ip = \''.$_SERVER['REMOTE_ADDR'].'\'';
$ip_query mysql_query($ip_check);
$num_rows mysql_num_rows($ip_query);

if(!
$num_rows) { //if no rows, meaning no ip's in db matched theirs, we will add them.
   
$insert_new mysql_query("INSERT INTO `users_online` (ip, time) VALUES ('$ip', '$time')");  
//end if NOT THERE

//this means that they're already in there, so we update info.
   
if($num_rows 0) {
      
$update mysql_query('UPDATE `users_online` SET time=\''.time().'\' WHERE ip = \''.$_SERVER['REMOTE_ADDR'].'\'');
       if(!
$update) die(mysql_error());
// end UPDATE

//if time now - start time > 300 then its been 5 minutes, so we delete
   
$delete_old mysql_query("DELETE FROM `users_online` WHERE ((".time()."-time) > 300)");
     if(!
$delete_old) die(mysql_error());

//show the number of people online now.... 
$id_query = @mysql_query("SELECT * FROM `users_online`");
$users_online mysql_num_rows($id_query);

echo(
"<BR>The number of people here are : $users_online<BR>");
//users online is done....
?>


 


Comments:

  gravy
  Subject: "awesome" Date: Jun 10 2007 at 7:52 pm    

Qucik Easy, Effective, nice little added feature to my website took about 3 minutes to do....
  malamute
  Subject: "Online Users" Date: Dec 20 2007 at 12:00 pm    
Finally, you can add alot to this code, and use the table to show "who&quo t; is online as well.

Hi - would love some help on how to add this facility to the above code. Thanks a mil !!!!

Also, I need to add a facility to my page where I can delete users (don't want to use phpmyadmin if possible) - would prefer a script of sorts ! Any help will be greatly appreciated !
  bs0d
  Subject: "Show Who is Online" Date: Dec 20 2007 at 9:25 pm    

You would first need to implement a Members System. Then, once that is in place you can detect if they're signed in like this:
if($_SESSION[' logged_in'] == 1) If they are, insert their name in the users online table, if not - insert "guest&q uot;

Being able to delete users is the same. Build a script that shows all users from your members table. Then, add a check box next to each user. Have that within an HTML form, and when you submit the form, have it call upon a delete_user script that will delete based on the check boxes that were checked from the form.
You Must be signed in or a member to comment.


Code Stats

Code Stats

48,694 Views
3 Total Comments
4.5 Rating of 5 ( Votes)

Options

Code Options

· Login to Rate This Code
· Login to Post a Comment
· Read more by this author
Digg This Code! Del.icio.us: Bookmark This Code Reddit: Bookmark This Code! BlinkList: Blink This Code! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Code Spurl: Mark This Code!

Code Samples

Related    

· Database Connection
· Pagination
· Comments Script
· CAPTCHA Image
· IdealMySQL Class for PHP 5.0





"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank