Logo
Number Users Currently Online
Online Now: 0
Snippet Code
This code uses a database to check the number of users that are online within a current time period.

Database Set Up



Database Name:    active_sessions

            ip        session        date
        +--------+-----------+---------+
NULL    |   NO   |    YES    |   YES   |
        +--------+-----------+---------+
DEFAULT |        |    NULL   |   NULL  |
        +--------+-----------+---------+
        

onlineNow.php


<?php
session_start
();
include
"db.php"# Connect To the database
$active_sessions 0;
$minutes 5# period considered active
if($sid session_id()) # if there is an active session
{
    
# DB connect here
    
$ip $_SERVER['REMOTE_ADDR']; # Get Users IP address
    # Delete users from the table if time is greater than $minutes
    
mysql_query("DELETE FROM `active_sessions` WHERE 
    `date` < DATE_SUB(NOW(),INTERVAL 
$minutes MINUTE)")or die(mysql_error()); 
    
    
# Check to see if the current ip is in the table
    
$sql mysql_query("SELECT * FROM active_sessions WHERE ip='$ip'");
    
$row mysql_fetch_array($sql);
    
# If the ip isn't in the table add it.
    
if(!$row){
        
mysql_query("INSERT INTO `active_sessions` (`ip`, `session`, `date`) 
        VALUES ('
$ip', '$sid', NOW()) ON DUPLICATE KEY UPDATE `date` = NOW()")or die(mysql_error());
    }
    
# Get all the session in the table
    
$sessions mysql_query('SELECT * FROM `active_sessions`')or die(mysql_error());
    
# Add up all the rows returned
    
$active_sessions mysql_num_rows($sessions);
}
    
# Print the final result
    
echo'<b>Online Now: </b>'.$active_sessions;
?>
 
Snippet Comments

Add Your Comment

Walt Disney World holidays
2011-06-27 00:56:26
The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post.
acai berry pills
2011-06-24 11:15:15
well I guess nosebleed. I can't relate it.
access tutorial
2011-06-24 11:14:03
yeah I can also help you set up the database.
credit card application
2011-06-22 20:07:46
The code is so good to use. This is so useful when you want to know how many users are online. I definitely use this as an admin.
personal statement writers
2011-06-22 09:19:03
I like your take on this matter. Although there are a few points I diverge on though. But I think that clears up a few issues for me personally.
neha
2011-06-22 09:16:37
Thank you so much for sharing such a wonderful listing.
accutane lawsuit
2011-06-21 19:07:52
There is so much great work that went into this. Keep up the good work on this.
charm bracelets
2011-06-21 03:47:15
Wow his is so happen that I am also doing the same thing too.
thesis writing service
2011-06-14 01:57:27
Thanks for sharing the idea there would be some apprehensions from segment but i am up for it.
soha
2011-06-14 01:54:06
Utilize all the space in your unit. Make a plan as to how you are going to arrange your items. A good plan will save you the time of having to rearrange your unit. Leave an aisle in the center of your unit so that you have a walk way to the back of the unit.
nike air max 24-7
2011-06-07 21:42:33
how are you?
Cheap Bob Seger Tickets
2011-05-23 02:47:29
I didn't know you could do this but you know something new everyday.I wish I could find such informative sites more often. You must be really a great professional like the workers in the ringtones or mp ringtones sites, to make the superior thought. Not any person will.
firefighter tools
2011-05-21 15:19:15
Great example of innovation, I am glad to find it. There are so many developers working on this segment but this is one of the best innovative idea ever. Thanks for sharing it here..
Custom Dissertation Writing
2011-05-16 14:43:37
Das Buch "Mac OS X - Leopard" richtet sich an all jene, die Erfahrung mit anderen Betriebssystemen (wie Windows) haben und neu in die Leopard-Welt eintauchen wollen.
G.N.R.S.U
2011-04-30 04:35:40
it will supply for my pligg
Commercial Van Insurance
2011-04-05 04:47:08
I love the scripts you provide in this website it really helps me in my projects.
Temporary Van Insurance
2011-04-05 04:42:57
I'm looking for this script for quite sometime in the net.
php scripts
2010-09-14 00:26:02
awesome. thank you very much for you scripts.
Anonymous
2009-08-20 00:00:00
As long as we're being grammatically correct, we'd might as well be concise. It is a special case, after all. $out = 'There is 1 user online.';
nukturnal
2008-08-18 00:00:00
This is really sweet! You are a life saver

Add Your Comment

 
Snippet Tools
Rate this Snippet:

Rate the difficulty level:

Request Snippet Update


Suggested Difficulty Level: Beginner
Current Score: 2.91
Total votes: 296
Total Views: 8946

Other top snippets by admin:

1. JPEG to ASCII Converter
2. Add (th, st, nd, rd, th) to the end of a number
3. Dynamic Page Content From Links
4. AJAX Quickie
5. Simple Image CAPTCHA

Search
Search for:







User Panel

User name:

Password:

Register And Post Your Own Snippets

New Snippets

1.  Shorten text and (4 of 1)

2.  file-based simple guest (0 of 0)

3.  adding an entry (0 of 0)

4.  integer to currency (1.33 of 3)

5.  Aggregate IP addresses (0 of 0)

6.  file handling (0 of 0)

7.  testing system with (0 of 0)

8.  Make an php (1 of 1)

9.  allow download in (5 of 1)

10.  redirect in 3 (0 of 0)

11.  live clock with (0 of 0)

12.  visit counter (3.5 of 2)

13.  contact form with (4 of 1)

14.  show a specific (0 of 0)

15.  The & sign (0 of 0)

16.  static variable in (0 of 0)

17.  multiplication table with (0 of 0)

18.  function with multiplicationt (0 of 0)

19.  php generated menu (0 of 0)

20.  drawing a table (0 of 0)

21.  embedded array (one (0 of 0)

22.  checks the week (0 of 0)

23.  put links on (4.5 of 2)

24.  Directory Image Gallery (0 of 0)

25.  TOS (4 of 1)

Home | Forum | Free PHP Web Hosting | Contact | Terms & Conditions |  
Donate
PHPSnips.com - ©2012 Amethyst Creative