Ashoar Posted April 14, 2009 Share Posted April 14, 2009 Ok i have a users online section for my code. I have a table called online that contains one row, called Username. When a user logs on their name is entered into this database. On the main page i can then fetch all users online and display them. When you log out, your user name is removed from the online table. The problem is, if you sit idle on one page too long or do not logout correctly, your name will continue to stay on the list even if your logged out. When you log back in your name will the be on the list 2 times. It will continue like that untill you properly log out, where any of your names will be removed from the list. I want a way to stop this from happening, so that if you are logged out from being idle to long or closing the window when you leave it will remove you from the online table. I was suggested to add a time stamp but i could not get anything like that working. So could some one show a decent way of completing this? Here is the log in and log out code: Login: <?php session_start(); include 'config.php'; if(isset($_POST['login'])) { $username = trim(addslashes($_POST['username'])); $password = md5(trim($_POST['password'])); $query = mysql_query("SELECT * FROM Users WHERE Username = '$username' AND Password = '$password' LIMIT 1") or die(mysql_error()); $row = mysql_fetch_array($query); // now we check if they are activated if(mysql_num_rows($query) > 0) { if($row['Activated'] > 0) { $_SESSION['s_logged_n'] = 'true'; $_SESSION['s_username'] = $username; $_SESSION['s_name'] = $row['Name']; $insertuser="INSERT INTO online(Username) values('$username')"; mysql_query($insertuser) or die("Could not login insert user"); header("Location: index.php"); } else { echo ' <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="error"><p>Sorry, you must activate your account first. Please check your email for the email.</p> <p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p></div> </body> </html> '; } } else { echo ' <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="error"><p>There was an error processing your login, it appears that your username and/or password was incorrect. Please try again.</p> <p>Didn'."'".'t get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p> </div> </body> </html> '; } } else { ?> <div id="wrapper"> <?php include "header.php"; ?> <html> <head> <title>Login</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <div id="wrapper"> <table class='maintable'> <tr class='headline'><td width=100%>Login</td></tr> <form method="post" action="<?= $_SERVER['PHP_SELF'] ?>"> <tr class='mainrow'><td><p>Username:<br> <input name="username" type="text" class="textBox" id="username"> <tr class='mainrow'><td><p>Password:<br> <input name="password" type="password" class="textBox" id="password"> </p> <p> <input name="login" type="submit" class="textBox" id="login" value="Submit"> </p> </form> <p>Didn't get your validation email? <a href="resend.php">Click here</a> to resend the validation email.</p> <p>Need an account? <a href="register.php">Click here</a> to register, it's completely free! </p> </div> </table> </div> </body> </html> <? } mysql_close($l); ?> <?php include "footer.php"; ?> Logout: <?php include 'config.php'; session_start(); $username = $_SESSION['s_username']; $query = "DELETE FROM online WHERE username = '".mysql_real_escape_string($_SESSION['s_username'])."';"; $result = mysql_query($query); $_SESSION['s_logged_n'] = ''; $_SESSION['s_name'] = ''; $_SESSION['s_username'] = ''; session_destroy(); header("Location: index.php"); ?> <div id="wrapper"> <?php include "header.php"; ?> <?php include "footer.php"; ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted April 14, 2009 Share Posted April 14, 2009 That method is not reliable, the best way is to use a timestamp everytime a user does something, then check the timestamp and if it's within 5 mins, then display them as online. Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 14, 2009 Author Share Posted April 14, 2009 I have checked some tutorials but have been unable to correctly work it. Could you show me the correct method of doing so. Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 14, 2009 Author Share Posted April 14, 2009 Back to first page. Could some one show me exactly how this could be done using timestamps? I can't manage to get anything working using them. Quote Link to comment Share on other sites More sharing options...
revraz Posted April 14, 2009 Share Posted April 14, 2009 Post what you've tried and sample data. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted April 14, 2009 Share Posted April 14, 2009 There are user online scripts posted all over the place - http://www.hotscripts.com/category/php/scripts-programs/counters/real-time/ Perhaps you are having a problem with sessions or with the database that is preventing what you have tried from working. Does your existing login/user code work when going from page to page? Quote Link to comment Share on other sites More sharing options...
Ashoar Posted April 15, 2009 Author Share Posted April 15, 2009 Yes you are still logged in regardless of the page you are on, i have the session start in the header of each page, which is also where i was placing the time stamps. Quote Link to comment 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.