irkevin Posted October 18, 2007 Share Posted October 18, 2007 Hi everyone, someone mind tell me if there are some changes i need to make with this code? <?php include('config.php'); include('functions.php'); mysql_connect($server, $dbusername, $dbpassword); mysql_select_db("$user_online"); $uname = $_SESSION['user_name']; if (!$uname) { $guest = "1"; } else { $guest = "0"; } $ip = $_SERVER['REMOTE_ADDR']; $time = time(); $query = "SELECT * FROM user_online WHERE ip = '$ip'"; $result = mysql_query($query); if (mysql_num_rows($result) == 1) { $query2 = "UPDATE user_online SET uname='$uname',time='$time',guest='$guest' WHERE ip = '$ip'"; $result2 = mysql_query($query2); } else { $query2 = "INSERT INTO user_online (uname,ip,time,guest)VALUES('$uname', '$ip', '$time','$guest')"; $result2 = mysql_query($query2); } $time3 = time() - 300; $query3 = "DELETE FROM user_online WHERE time < $time3"; $result3 = mysql_query($query3); $query4 = "SELECT * FROM user_online WHERE guest = '1'"; $result4 = mysql_query($query4); $guestonline = mysql_num_rows($result4); $query5 = "SELECT * FROM user_online WHERE guest = '0'"; $result5 = mysql_query($query5); $memonline = mysql_num_rows($result5); $totalonline = $guestonline + $memonline; echo "Guests: $guestonline<br /> Members: $memonline<br /> Total: $totalonline<br /><br />"; ?> sometimes, it shows only one user online even if there are more than one user logged in in my site Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/ Share on other sites More sharing options...
kratsg Posted October 18, 2007 Share Posted October 18, 2007 Why do you have this? mysql_select_db("$user_online"); Isn't the database "user_online" and not a variable? That may be your problem. Remove the dollar sign. Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-372078 Share on other sites More sharing options...
darkfreaks Posted October 18, 2007 Share Posted October 18, 2007 it still works its parsed in double quotes Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-372079 Share on other sites More sharing options...
irkevin Posted October 18, 2007 Author Share Posted October 18, 2007 yeps somehow the scripts is working even with the dollar sign but i removed it tho.. Thanks for this remark kratsg and thanks for helping me test the code darkfreaks Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-372082 Share on other sites More sharing options...
darkfreaks Posted October 18, 2007 Share Posted October 18, 2007 np buddy Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-372088 Share on other sites More sharing options...
irkevin Posted October 20, 2007 Author Share Posted October 20, 2007 Hi again with a little problem with this code, it actually shows the user who is online but it removes it after a short period of time even if the user is still logged in.. What should i modify in this code in order that it removes the user only when he logs out? Can someone help me with this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-374292 Share on other sites More sharing options...
darkfreaks Posted October 20, 2007 Share Posted October 20, 2007 try something like <?php if(!isset($_SESSION['loggedin'])) { $query3 = "DELETE FROM user_online";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-374298 Share on other sites More sharing options...
irkevin Posted October 20, 2007 Author Share Posted October 20, 2007 yeps i think it's a good idea.. so i got rid of the time()- 300 and added this part if(!isset($_SESSION['loggedin'])) { $query3 = "DELETE FROM user_online WHERE guest = '0'"; } and now we'll see the results thnx darkfreaks Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-374325 Share on other sites More sharing options...
darkfreaks Posted October 20, 2007 Share Posted October 20, 2007 final code: <?php if(!isset($_SESSION['user_name'])) { $query3 = "DELETE FROM user_online WHERE guest = '0'"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-374333 Share on other sites More sharing options...
irkevin Posted October 20, 2007 Author Share Posted October 20, 2007 yeps and it works.. thanks for ur time Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-374335 Share on other sites More sharing options...
darkfreaks Posted October 20, 2007 Share Posted October 20, 2007 no problem Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-374336 Share on other sites More sharing options...
irkevin Posted October 21, 2007 Author Share Posted October 21, 2007 Sorry to bug you with this again for the logged users, the code works but supposed a guest is looking at the website, basically it will add him as a guest but how do I removed the guest from the database once he close the browser and dont watch the website again.. Is there a way i can do that? Thanks for all your help btw Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-374969 Share on other sites More sharing options...
darkfreaks Posted October 21, 2007 Share Posted October 21, 2007 <?php function session_restart() { if (session_name()=='') { // Session not started yet session_start(); } else { // Session was started, so destroy session_destroy(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-374989 Share on other sites More sharing options...
darkfreaks Posted October 21, 2007 Share Posted October 21, 2007 <?php function session_restart() { if (session_name()=='') { // Session not started yet session_start(); } else { // Session was started, so destroy session_destroy(); } } if($_SESSION['user_name']=="guest"||"javascript:window.close()") { $query="DELETE*FROM user_name WHERE guest=0"; session_restart($_SESSION['user_name']);} ?> Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-375007 Share on other sites More sharing options...
irkevin Posted October 22, 2007 Author Share Posted October 22, 2007 coool but how do i insert this into the actual code ??? i've never used functions before Quote Link to comment https://forums.phpfreaks.com/topic/73742-solved-little-help-with-this-user-online-code/#findComment-375157 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.