01 CREATE TABLE `online_counter`(
02 `hash` varchar(50) not null,
03 `remote_addr` varchar(20) not null,
04 `time` int(10) not null)01 <?php
02 // create mysql connection
03 $db_conn = mysql_connect("localhost", "dbuser", "dbpasswd");
04 mysql_select_db("dbname", $db_conn);
05
06 // check and make hash string
07 if(!isset($_SESSION["hash"])){
08 $hash = md5(time());
09 session_register("hash");
10 $_SESSION["hash"] = $hash;
11 }
12
13 // update database
14 $time = time();
15 $query = mysql_query("select hash from `online_counter` where hash='".$_SESSION["hash"]."'");
16 if($rows = mysql_fetch_row($query)){
17 mysql_query("update `online_counter` set `time`='".$time."' where hash='".$_SESSION["hash"]."'");
18 }else{
19 mysql_query("insert into `online_counter` values('".$_SESSION["hash"]."', '".$_SERVER["REMOTE_ADDR"]."', '".$time."')");
20 }
21
22 // get online users
23 $counter_time = $time - 600; // 10 mins
24 $online_query = mysql_query("select count(*) as total from `online_counter` where `time` > '".$counter_time."'");
25 $online_counter = mysql_fetch_array($online_query);
26
27 // delete old record
28 mysql_query("delete from `online_counter` where `time` < '".$counter_time."'");
29 ?>