2013-06-18 53 views
0

我想创建一个系统,其中,当用户在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']?>仍犯规输出任何东西。

+0

为什么不能你刚才设置它在php.ini或htaccess的:http://www.php.net/manual/en/session.configuration.php#ini.session。 gc-maxlifetime('se​​ssion.gc-maxlifetime'指令)?轻松一点! – jtheman

+0

http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes可能的副本 –

+0

@jtheman感谢指引我在正确的方向 - 正是我需要。 – ewizard

回答

0

选项之一:

使用的session.gc_maxlifetime

ini_set('session.cookie_lifetime', 600); 
session_start(); 

http://php.net/manual/en/function.session-set-cookie-params.php

此值(默认1440秒)定义了多长时间未使用的PHP会话将保持活跃。例如:用户登录,浏览您的应用程序或网站几个小时,几天。没问题。只要他点击的时间不超过1440秒。这是一个超时值。

选项二:

session.cookie_lifetime

此值(默认为0,这意味着直到浏览器的接下来重启)定义了一个会话cookie多长时间(单位:秒)居住。听起来类似于session.gc_maxlifetime,但它是一种完全不同的方法。此值间接定义会话的“绝对”最大生存期,无论用户是否处于活动状态。如果此值设置为60,则每个会话在一分钟后结束。

方案三:

session_start(); 
// set time-out period (in seconds) 
$inactive = 600; 

// check to see if $_SESSION["timeout"] is set 
if (isset($_SESSION["timeout"])) { 
    // calculate the session's "time to live" 
    $sessionTTL = time() - $_SESSION["timeout"]; 

    if ($sessionTTL > $inactive) { 
     session_destroy(); 
     header("Location: /logout.php"); 
    } 
} 

$_SESSION["timeout"] = time(); 
+0

你读过这个:http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes – jtheman

+0

@ user1752647我想你的意思是'ini_set('session ('session.gc-maxlifetime',60 * 30);'not this'ini_set('session.cookie_lifetime',600);' – ewizard

+0

@jtheman在哪里放'ini_set('session.gc-maxlifetime',60 * 30); '? – ewizard