deepermethod Posted November 14, 2008 Share Posted November 14, 2008 I have a personal website that show the number of users on my site as a whole (like = 12 members online). The site has profile pages so I would like to show if a user is online or offline with an image. I have searched and searched here and haven't found what I was looking for. Can I alter my current file to fit what I want? <?php //Prevent the included file from being called directly. if(basename($_SERVER['PHP_SELF']) == "online.inc.php") { header("Location: /index.php"); exit; } $connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect."); $db = @mysql_select_db($db_name, $connection) or die("Couldn't select database."); $timestamp = time(); $timeout = $timestamp - 180; if(session_is_registered("valid_user")) { $ol_user = "$valid_user"; } else { $ol_user = "guest"; } if(session_is_registered("valid_user")) { $ol_id = "$member_id"; } else { $ol_id = "0"; } //Insert User $insert = mysql_query("INSERT INTO $tbl_online (timestamp, ip, file, ol_user, ol_id) VALUES('$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."','$ol_user','$ol_id')") or die("Error in who's online insert query!"); //Delete Users $delete = mysql_query("DELETE FROM $tbl_online WHERE timestamp<$timeout") or die("Error in who's online delete query!"); //Fetch Users Online $result = mysql_query("SELECT DISTINCT ip FROM $tbl_online") or die("Error in who's online result query!"); $users = mysql_num_rows($result); if($users == 1) { $ol_label = "user"; } else { $ol_label = "users"; } ?> <table align="center" cellpadding="0" cellspacing="0"> <tr> <td class="online"> <p><?php echo "<b>$valid_user</b> $ol_label"; ?> online</p> </td> </tr> </table> Thanks in advanced. Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/ Share on other sites More sharing options...
Maq Posted November 14, 2008 Share Posted November 14, 2008 We would need to see the profile page to help you there. When you display each name you have to look it up against the database and see if they're online. $result = mysql_query("SELECT ip FROM $tbl_online WHERE user_name = $user_name") or die("Error in user online result query!"); if(mysql_num_rows($result) > 0) { //display the online_image } else { //display the offline_image } Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-689880 Share on other sites More sharing options...
deepermethod Posted November 14, 2008 Author Share Posted November 14, 2008 I am getting this error= "Error in who's online insert query!" I am just learning php, so be patient please. Here's the current code: <?php //Prevent the included file from being called directly. if(basename($_SERVER['PHP_SELF']) == "online.inc.php") { header("Location: /index.php"); exit; } $connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect."); $db = @mysql_select_db($db_name, $connection) or die("Couldn't select database."); $timestamp = time(); $timeout = $timestamp - 180; //Insert User $insert = mysql_query("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id) VALUES(\"$_SESSION[member_id]\",'$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."','$ol_user','$ol_id')") or die("Error in who's online insert query!"); //Delete Users $delete = mysql_query("DELETE FROM $tbl_online WHERE timestamp<$timeout") or die("Error in who's online delete query!"); //Fetch Users Online $result = mysql_query("SELECT DISTINCT ip FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!"); if(mysql_num_rows($result) > 0) { echo"<img src='./gr_image/online.jpg'>"; } else { echo"<img src='./gr_image/offline.jpg'>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-689910 Share on other sites More sharing options...
zenag Posted November 14, 2008 Share Posted November 14, 2008 your insert query shows error...try mysql_error() to dispaly what kind of error.. ... $insert = mysql_query("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id) VALUES(\"$_SESSION[member_id]\",'$timestamp','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['PHP_SELF']."','$ol_user','$ol_id')") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-689914 Share on other sites More sharing options...
Maq Posted November 14, 2008 Share Posted November 14, 2008 Try this, it's annoying trying to deal with escaping the strings, I like to just assign them to another variable. Also, like zenag said, add mysql_error() on the end of your die function so it can give you a descriptive error message. $mem_id = $_SESSION['member_id']; $rem_add = $_SERVER['REMOTE_ADDR']; $php_self = $_SERVER['PHP_SELF']; $insert = mysql_query("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id) VALUES('$mem_id', '$timestamp', '$rem_add', '$php_self', '$ol_user', '$ol_id')") or die("Error in who's online insert query!".mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-689922 Share on other sites More sharing options...
deepermethod Posted November 15, 2008 Author Share Posted November 15, 2008 Thanks maq and zenag. I got the online image to show, initially. Now, a new error. When I go to another page and then come back to where the online/offline image is I get this error: "Error in who's online insert query!Duplicate entry '4' for key 1". Any ideas? Is my Delete Users query not working correctly? <?php //Prevent the included file from being called directly. if(basename($_SERVER['PHP_SELF']) == "online.inc.php") { header("Location: /index.php"); exit; } $connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect."); $db = @mysql_select_db($db_name, $connection) or die("Couldn't select database."); $timestamp = time(); $timeout = $timestamp - 180; $mem_id = $_SESSION['member_id']; $rem_add = $_SERVER['REMOTE_ADDR']; $php_self = $_SERVER['PHP_SELF']; //Insert User $insert = mysql_query("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id) VALUES('$mem_id', '$timestamp', '$rem_add', '$php_self', '$ol_user', '$ol_id')") or die("Error in who's online insert query!".mysql_error()); //Delete Users $delete = mysql_query("DELETE FROM $tbl_online WHERE timestamp<$timeout") or die("Error in who's online delete query!"); //Fetch Users Online $result = mysql_query("SELECT DISTINCT ip FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!"); if(mysql_num_rows($result) > 0) { echo"<img src='./gr_image/online.jpg'>"; } else { echo"<img src='./gr_image/offline.jpg'>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-690771 Share on other sites More sharing options...
deepermethod Posted November 15, 2008 Author Share Posted November 15, 2008 Been trying to do a fix all day and can't seem to get it to work. Any help? Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-690875 Share on other sites More sharing options...
Maq Posted November 15, 2008 Share Posted November 15, 2008 You should check to see if that user ID exists in the table already because if they haven't reached the time limit then you're trying to insert into an ID that's already there. So: 1) Check to see if user is in the $tbl_online (by id). 2) If they are then UPDATE their time and w/e else you need to update. 3) If they aren't then you should insert them (proceed with your current code). Hope this helps! Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-690951 Share on other sites More sharing options...
deepermethod Posted November 16, 2008 Author Share Posted November 16, 2008 Okay. I'm stumped. After doing some more searching, here's my current code (unfinished): <?php //Prevent the included file from being called directly. if(basename($_SERVER['PHP_SELF']) == "online.inc.php") { header("Location: /index.php"); exit; } $connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect."); $db = @mysql_select_db($db_name, $connection) or die("Couldn't select database."); $timestamp = time(); $timeout = $timestamp - 180; $mem_id = $_SESSION['member_id']; $rem_add = $_SERVER['REMOTE_ADDR']; $php_self = $_SERVER['PHP_SELF']; $result = mysql_query("SELECT DISTINCT ip FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!"); if(mysql_num_rows($result) > 0) { $update = mysql_query("UPDATE $tbl_online WHERE timestamp<$timeout")or die("Error in who's online update query!".mysql_error()); } else { $insert = mysql_query(("INSERT INTO $tbl_online (id, timestamp, ip, file, ol_user, ol_id) VALUES('$mem_id', '$timestamp', '$rem_add', '$php_self', '$ol_user', '$ol_id')") or die("Error in who's online insert query!".mysql_error()); } //Delete Users $delete = mysql_query("DELETE FROM $tbl_online WHERE timestamp<$timeout") or die("Error in who's online delete query!"); Now, how do I get it to echo/print the online or offline images? Here's how I had it before: //Fetch Users Online $result = mysql_query("SELECT DISTINCT ip FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!"); if(mysql_num_rows($result) > 0) { echo"<img src='./gr_image/online.jpg'>"; } else { echo"<img src='./gr_image/offline.jpg'>"; } Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691368 Share on other sites More sharing options...
deepermethod Posted November 16, 2008 Author Share Posted November 16, 2008 So is this possible or should I just scrap the idea? Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691411 Share on other sites More sharing options...
redarrow Posted November 16, 2008 Share Posted November 16, 2008 modify as needed......... In this tutorial create 1 file 1. user_online.php Step 1. Create table "user_online" in mysql in database "test". 2. Create file user_online.php. database........ CREATE TABLE `user_online` ( `session` char(100) NOT NULL default '', `time` int(11) NOT NULL default '0' ) TYPE=MyISAM; online.php <?php session_start(); $session=session_id(); $time=time(); $time_check=$time-600; //SET TIME 10 Minute $host="localhost"; // Host name $username=""; // 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)VALUES('$session', '$time')"; $result1=mysql_query($sql1); } else { "$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'"; $result2=mysql_query($sql2); } $sql3="SELECT * FROM $tbl_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); mysql_close(); // Open multiple browser page for result ?> Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691416 Share on other sites More sharing options...
deepermethod Posted November 16, 2008 Author Share Posted November 16, 2008 That looks good, but is that to count the number of users online? I was looking to show 2 different images - one if a user was online and another if the user was offline. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691429 Share on other sites More sharing options...
redarrow Posted November 16, 2008 Share Posted November 16, 2008 you need to adapt the code that how your learn everythink on this page does what you want......... have a go it easy the more you pratice the more your learn......... sorry but your a programmer not a copy and past expert i hope....... if your a copy and past exspert then use the freelance on here for paid or free contubuted code sorry...... Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691431 Share on other sites More sharing options...
deepermethod Posted November 16, 2008 Author Share Posted November 16, 2008 Yes, I am in the process of adapting it to my needs. I just wanted to make sure that is what the code did. Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691436 Share on other sites More sharing options...
redarrow Posted November 16, 2008 Share Posted November 16, 2008 Put this where it goes on my provided code....... if the code also needs adapting more tell us what dont work we shall help ok........ <?php if(mysql_num_rows($result3)){ $img="<img src='online.jpg' />"; }else{ $img="<img src='offline.jpg' />"; } echo $img; // if over 10 minute, delete session $sql4="DELETE FROM $tbl_name WHERE time<$time_check"; $result4=mysql_query($sql4); ?> my code full formatted to show a image offline or online <?php session_start(); $session=session_id(); $time=time(); $time_check=$time-600; //SET TIME 10 Minute $host="localhost"; // Host name $username=""; // 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)VALUES('$session', '$time')"; $result1=mysql_query($sql1); } else { "$sql2=UPDATE $tbl_name SET time='$time' WHERE session = '$session'"; $result2=mysql_query($sql2); } $sql3="SELECT * FROM $tbl_name"; $result3=mysql_query($sql3); if(mysql_num_rows($result3)){ $img="<br><img src='online.jpg' /><br>"; }else{ $img="<br><img src='offline.jpg' /><br>"; } echo $img; // if over 10 minute, delete session $sql4="DELETE FROM $tbl_name WHERE time<$time_check"; $result4=mysql_query($sql4); mysql_close(); // Open multiple browser page for result ?> Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691439 Share on other sites More sharing options...
deepermethod Posted November 16, 2008 Author Share Posted November 16, 2008 Well, I adjusted it to what I need and it initially worked (showed online image). When I go to another page and back to the page with the online.php script it shows the offline image. My code: <?php //Prevent the included file from being called directly. if(basename($_SERVER['PHP_SELF']) == "online.inc.php") { header("Location: /index.php"); exit; } $connection = @mysql_connect("$db_host", "$db_user", "$db_pass") or die("Couldn't connect."); $db = @mysql_select_db($db_name, $connection) or die("Couldn't select database."); $timestamp = time(); $timeout = $timestamp - 180; $mem_id = $_SESSION['member_id']; $rem_add = $_SERVER['REMOTE_ADDR']; $php_self = $_SERVER['PHP_SELF']; $sql=("SELECT * FROM $tbl_online WHERE id = $_SESSION[member_id]") or die("Error in user online result query!"); $count=mysql_num_rows($result); if($count=="0"){ $sql1=("INSERT INTO $tbl_online(id, timestamp, ip)VALUES('$mem_id', '$timestamp', '$rem_add')"); $result1=mysql_query($sql1); } else { $sql2=("UPDATE `tbl_online` SET count = count + 1 WHERE id = '$mem_id'"); $result2=mysql_query($sql2); } $sql3=("SELECT * FROM $tbl_online"); $result3=mysql_query($sql3); if(mysql_num_rows($result3)){ echo"<img src='./gr_image/online.jpg'>"; } else { echo"<img src='./gr_image/offline.jpg'>"; } echo $img; // if over 10 minute, delete session $sql4=("DELETE FROM $tbl_online WHERE timestamp<$timeout"); $result4=mysql_query($sql4); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691453 Share on other sites More sharing options...
redarrow Posted November 16, 2008 Share Posted November 16, 2008 session start needs to be on all pages at the top ok........ <?php session_start();?> Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691457 Share on other sites More sharing options...
deepermethod Posted November 16, 2008 Author Share Posted November 16, 2008 session start needs to be on all pages at the top ok........ <?php session_start();?> Even if I am using the online.inc.php as an include? Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691466 Share on other sites More sharing options...
redarrow Posted November 16, 2008 Share Posted November 16, 2008 session_start() needs to be added to every page manualy to work properly ....... Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691469 Share on other sites More sharing options...
deepermethod Posted November 16, 2008 Author Share Posted November 16, 2008 I'll keep looking at it. I did include <?php session_start();?> to the top of both pages and it still isn't working. Some how it isn't getting inserted into my database. Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691471 Share on other sites More sharing options...
deepermethod Posted November 16, 2008 Author Share Posted November 16, 2008 I scrapped my script altogether. I used your script, including creating the new table. All it shows is the offline image. I have the session_start(); at the top of each page and still only the offline image. When I look in the table in my database there is nothing there, it isn't getting inserted some how. Not looking for a script rewrite, just some advice. Quote Link to comment https://forums.phpfreaks.com/topic/132648-show-users-onlineoffline-with-image/#findComment-691499 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.