我想创建一个系统,其中,当用户在x时间内不活动时,他们应该被注销并返回到'index.php'。我使用的代码,我发现这里 - http://cookbooks.adobe.com/post_Set_a_time_limit_on_a_login_session__PHP_-16701.html,我把它放在一个函数是这样的:无活动和登录系统php
phpfunc/inactivity.php
<?php
function inactivity() {
// create a session
if (!isset($_SESSION)) {
session_start();
}
// store the current time
$now = time();
// get the time the session should have expired
$limit = $now - 60*20;
// check the time of the last activity
if (isset ($_SESSION['last_activity']) && $_SESSION['last_activity'] < $limit) {
// if too old, clear the session array and redirect
$_SESSION = array();
header('../index.php');
exit;
}
else {
// otherwise, set the value to the current time
$_SESSION['last_activity'] = $now;
}
}
?>
然后,我把这样的功能......顶部我保护的页面的
模板/ login_success.php:
<?php
require('/Users/Eamon/Sites/phpfunc/inactivity.php');
inactivity();
if($_SESSION['username'] === null){
header("location: ../index.php");
}
?>
<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
<div id="logoutlinkdiv" >
<a href = "#" >Log out</a>
</div>
我可以登录 - 和所有的活动的东西似乎工作。我通过在这里和那里添加几个echo语句来测试它......并且在登录后我尝试去保护页面 - 就像这个“localhost /〜Eamon/templates/login_success.php”,它让我看到它......之后20分钟我必须再次登录。但是,我有一个用户注销后加载的页面(单击“logoutlinkdiv”链接后) - 并且它上面有一个链接以再次登录。当我点击这个链接时,我收到消息“错误的用户名或密码” - 可以在下面的文件中找到(它会在登录时执行)。再过20分钟后,我可以再次登录而不会出现此错误。
checklogin.php
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="bonjour3"; // Mysql password
$db_name="itit"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "$username", "$password", "$db_name")or die("cannot connect");
// Define $myusername and $mypassword
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql = $mysqli->prepare("SELECT * FROM $tbl_name WHERE username=? and password=?");
$sql->bind_param('ss',$myusername,$mypassword);
$sql->execute();
$sql->store_result();//apply to prepare statement
$numRows = $sql->num_rows;
if($numRows === 1){
$_SESSION['username'] = $myusername;
}
else {
echo "Wrong Username or Password";
session_destroy();
}
?>
这里是上述注销页面(其中用户可以重新登录...麻烦页面!):
模板/ logout.php
<?php
session_start();
session_destroy();
?>
<div id="loginlinkdiv" >
<h2>You have been logged out.</h2>
<a href = "#">Log in</a>
</div>
如果它是相关的......这里是我正在使用的jQuery。我在做一个SPA - 所以jQuery是加载和排空的index.php事情我在 “主” 分区 - 在这里:
的index.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title>it IT</title>
<script src="reqscripts/jquery.js" type="text/javascript"></script>
<script src="js/application.js" type="text/javascript"></script>
</head>
<body>
<div id="main"></div>
</body>
</html>
UPDATE
继建议 - 我尝试这个页面上的第一个答案:
How do I expire a PHP session after 30 minutes?
我改变两个文件。
phpfunc/inactivity.php现在看起来是这样的:
<?php
function inactivity() {
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {
// last request was more than 30 minutes ago
session_unset(); // unset $_SESSION variable for the run-time
session_destroy(); // destroy session data in storage
header("/Users/Eamon/Sites/index.php");
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
}
?>
和模板/ login_success.php看起来是这样的:
<?php
require('/Users/Eamon/Sites/phpfunc/inactivity.php');
session_start();
inactivity();
?>
<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
<div id="logoutlinkdiv" >
<a href = "#" >Log out</a>
</div>
还是同样的问题似乎发生......和我想我发现了一个新的故障。当我点击登录链接(templates/logout.php)时,它将我带到index.php。我尝试登录,但文本框中的信息被重置,似乎没有发生。当我再次尝试时 - 我再次收到“错误的用户名或密码”。此外 - <? echo $_SESSION['username']?>
不再显示在login_success.php页面上。
UPDATE
看来,如果我没有得到签署,因为<? echo $_SESSION['username']?>
仍犯规输出任何东西。
为什么不能你刚才设置它在php.ini或htaccess的:http://www.php.net/manual/en/session.configuration.php#ini.session。 gc-maxlifetime('session.gc-maxlifetime'指令)?轻松一点! – jtheman
http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes可能的副本 –
@jtheman感谢指引我在正确的方向 - 正是我需要。 – ewizard