Find number of users online - Php

Users Online

Snippet Code


  
Rate this page :
  [ 0 votes]

This script is used to find the number of users currently online.

<?php session_start(); include"database.php"; $active_sessions = 0; $minutes = 5; if($sid = session_id()) { $ip = $_SERVER['REMOTE_ADDR']; mysql_query("DELETE FROM `active_sessions` WHERE `date` < DATE_SUB(NOW(),INTERVAL $minutes MINUTE)")or die(mysql_error()); $sql = mysql_query("SELECT * FROM active_sessions WHERE ip='$ip'"); $row = mysql_fetch_array($sql); 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()); } $sessions = mysql_query('SELECT * FROM `active_sessions`')or die(mysql_error()); $active_sessions = mysql_num_rows($sessions); } echo'<b>Online Now: </b>'.$active_sessions; ?> /***************database******************/ Database Name: active_sessions ip session date -------- ----------- --------- NULL | NO | YES | YES | -------- ----------- --------- DEFAULT | | NULL | NULL | -------- ----------- ---------

Tags


Ask Questions

Ask Question