phpwiz Posted July 29, 2009 Share Posted July 29, 2009 Ok, i have made like a simple Online users thing where when they login it inserts there id, and username into a table called Online but i need it so it automaticly deletes from the table over an ammount of time and when they go to logout.php it deletes from the table also i will post both codes below login2.php <?php $username = $_POST['username']; $password = $_POST['password']; if ($username) { $connect = mysql_connect("******","***********","**********") or die("Could Not connect!"); mysql_select_db("******") or die("Could Not find DB"); $query = mysql_query("SELECT * FROM Users WHERE username='$username'"); $numrows = mysql_num_rows($query); if ($numrows!=0) { while ($row = mysql_fetch_assoc($query)) { $dbusername = $row['username']; $dbpassword = $row['password']; } // check to see if they match if ($username==$dbusername&&$password==$dbpassword) { echo "You have sucessfully been logged in! <a href='mem.php'>Click here!</a>"; $_SESSION['username']=$username; $do = mysql_query("INSERT INTO Online VALUES ('','$username')"); } else echo "Incorrect password!"; } else die("That user Does NOT exist"); } else die("Please enter a username and a password!"); ?> and logout.php <?php session_destroy(); echo "You have successfully been logged out. <a href='login.php'>Click here</a> to log back in!"; ?> can you please help me with this thanks Quote Link to comment https://forums.phpfreaks.com/topic/167913-delete-from/ Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 what is the issue, is it not working? are you getting any errors? Quote Link to comment https://forums.phpfreaks.com/topic/167913-delete-from/#findComment-885654 Share on other sites More sharing options...
phpwiz Posted July 29, 2009 Author Share Posted July 29, 2009 i was wondering HOW to make it delete in a certin ammount of time or delete when they logout Quote Link to comment https://forums.phpfreaks.com/topic/167913-delete-from/#findComment-885656 Share on other sites More sharing options...
Psycho Posted July 29, 2009 Share Posted July 29, 2009 Well, if by logout you mean a specific button/link on the page that the user will click then, yes, you can do that. But, users will not "log out" of a web application. They will simply close the browser window. And, there is no way for PHP or the server to know if the user has closed their browser window. Instead of a table where you are constantly adding/deleting records, simply have a table with a record for every user (you can use the user table or another one). Each record will record the "last activity" of each user. On each page load you will update the value. Then when getting a list of "active" users, only get the ones whose last active timestamp is within a certain amount of time. Quote Link to comment https://forums.phpfreaks.com/topic/167913-delete-from/#findComment-885660 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 when they log out, just delete the record from the table according to the session username is set as. something like... mysql_query("delete from Online where Username = '$_SESSION[username]'"); to delete after a certain amount of time has passed could possibly require some type of ajax implementation, that runs another php script that periodically checks how long the user has been logged in, and act accordingly... edit: and as mjdamato pointed out... you aren't always able to check how long they have been logged in, cause they may not be there any more and running scripts (ie. closed browser window.) Quote Link to comment https://forums.phpfreaks.com/topic/167913-delete-from/#findComment-885662 Share on other sites More sharing options...
phpwiz Posted July 29, 2009 Author Share Posted July 29, 2009 Ok, that will work but i have an id field also how would i include that in the sql query. Quote Link to comment https://forums.phpfreaks.com/topic/167913-delete-from/#findComment-885663 Share on other sites More sharing options...
fooDigi Posted July 29, 2009 Share Posted July 29, 2009 i don't see an id being used anywhere in your code, so i'm really not sure... but you should only need one unique identifier to remove a record, be it a user id or username... as long as it is unique. Quote Link to comment https://forums.phpfreaks.com/topic/167913-delete-from/#findComment-885668 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.