2013-06-18 59 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