我有php脚本确认客户的密码和用户名与数据库中存储的密码和用户名相匹配(用户名和密码存储在用户数据库中)。一旦他们登录,他们将被重定向到他们自己的页面,但在一段时间不活动后,我希望他们被注销,或重定向到login.php。任何人都可以帮助一个久经考验的方法吗?提前谢谢。
<?php
session_start();
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
mysql_connect("localhost", "root","root") or die(mysql_error()); //Connect to server
mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
$query = mysql_query("SELECT * from users WHERE username='$username'"); //Query the users table if there are matching rows equal to $username
$exists = mysql_num_rows($query); //Checks if username exists
$table_users = "";
$table_password = "";
if($exists > 0) //IF there are no returning rows or no existing username
{
while($row = mysql_fetch_assoc($query)) //display all rows from query
{
$table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
$table_password = $row['password']; // the first password row is passed on to $table_users, and so on until the query is finished
$table_id = $row['id'];
$page_id = $row['page'];
}
if(($username == $table_users) && ($password == $table_password)) // checks if there are any matching fields
{
if($password == $table_password)
{
$_SESSION['user'] = $username; //set the username in a session. This serves as a global variable
//echo $table_id;
//echo $page_id;
redirect ($page_id); //take the user to the page specified in the users table
}
else
{
echo "Login Failed";
}
}
else
{
Print '<script>alert("1. Incorrect Password!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
}
else
{
Print '<script>alert("Incorrect Username!");</script>'; //Prompts the user
Print '<script>window.location.assign("login.php");</script>'; // redirects to login.php
}
function redirect($page_id)
{
/* Redirect browser */
header('Location: home.php');
/* Make sure that code below does not get executed when we redirect. */
exit;
}
?>
答案 0 :(得分:0)
将此添加到您的网页:
setcookie(session_name(), $_COOKIE[session_name()], time() + 15 * 60); // 15 is the number of minutes of inactivity before the session gets destroyed
这告诉会话它在死前还有X分钟。
答案 1 :(得分:0)
在页面标题中,添加一个在给定时间后将重定向用户的元
<meta http-equiv="refresh" content="900;url=logout.php" />