Handy for information on session time storing also
Database SQL
1 2 3 4 5 6 |
CREATE TABLE `user_online` ( `session` char(100) NOT NULL default '', `time` int(11) NOT NULL default '0' ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php session_start(); $session=session_id(); $time=time(); $time_check=$time-600; //SET TIME 10 Minute $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="user_online"; // Table name // Connect to server and select databse mysql_connect("$host", "$username", "$password")or die("cannot connect to server"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name WHERE session='$session'"; $result=mysql_query($sql); $count=mysql_num_rows($result); if($count=="0"){ $sql1="INSERT INTO $tbl_name(session, time, name)VALUES('$session', '$time', 'mike' )"; $result1=mysql_query($sql1); } else { "$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'"; $result2=mysql_query($sql2); } $sql3="SELECT * FROM $tbl_name"; while($row = mysql_fetch_array($sql3)) { echo $name = $row['name']; } $result3=mysql_query($sql3); $count_user_online=mysql_num_rows($result3); echo "User online : $count_user_online "; // if over 10 minute, delete session $sql4="DELETE FROM $tbl_name WHERE time<$time_check"; $result4=mysql_query($sql4); // Open multiple browser page for result // Close connection mysql_close(); ?> |