|
Follow @phpsnips |
Database Name: active_sessions
ip session date
+--------+-----------+---------+
NULL | NO | YES | YES |
+--------+-----------+---------+
DEFAULT | | NULL | NULL |
+--------+-----------+---------+
<?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;
?>
|
Rate this Snippet: |
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
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)
2011-06-27 00:56:26