Jump to content

Recommended Posts

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 ;D

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/167913-delete-from/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/167913-delete-from/#findComment-885660
Share on other sites

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.)

Link to comment
https://forums.phpfreaks.com/topic/167913-delete-from/#findComment-885662
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.