php
2016-09-03 157 views -1 likes 
-1

我想知道如何讓PHP會話超時?我有這個迄今爲止,或使其所以人們可以用餅乾和登錄..Php會話超時安全

<?php 
include('config.php'); 
session_start(); 
$user_check=$_SESSION['login_user']; 

$ses_sql=mysql_query("select username from admin where username='$user_check' "); 

$row=mysql_fetch_array($ses_sql); 

$login_session=$row['username']; 

if(!isset($login_session)) 
{ 
header("Location: login.php"); 
} 
?> 
+0

http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes檢查出來,應該回答你的問題。 – Kaylined

回答

0

您的代碼將永遠不會超時,因爲$login_session會因此只要用戶在數據庫中仍存在設置。

在會話中存儲到期時間。在包含在每個受保護頁面上的文件中摘錄下面的代碼。

<?php 
if(session_status()===PHP_SESSION_NONE) session_start(); 

//if user supplied login creds: 
if(isset($_POST['username']) && isset($_POST['password'])){ 
    //attempt to login, 
    //... 

    // if login worked save username and expiration time 
    if(...){ 
     $_SESSION['user'] = $row['username']; 
     $_SESSION['exp'] = time() + 600; //expires in 10 minutes 
    } 
} 
//now check access 
if(empty($_SESSION['user'])){ 
    //user is not logged in. show error and exit 
}elseif(empty($_SESSION['exp']) || $_SESSION['exp'] < time()){ 
    //session has expired. show error and exit 
} 

//session is still valid. Extend expiration: 
$_SESSION['exp'] = time() + 600; //expires in 10 minutes 

//show protected content 
+0

我使用的代碼,但我得到這個錯誤[04-Sep-2016 15:40:52歐洲/倫敦] PHP解析錯誤:語法錯誤,意外的'...'(T_ELLIPSIS)在/home/public_html/welcome.php在線12 –

+0

@dazholmes那是因爲我使用了'if(...)'。這不是真正的代碼。它的意思是說'如果登錄成功'。用適合您的應用程序的代碼替換它。 – BeetleJuice

+0

好吧,那麼我該如何得到這個工作?我把它放到我想保護的頁面中,但是就像我說的那樣,我得到了那個錯誤......我在受保護頁面上包含的唯一東西是包含('lock.php');並且lock.php的內部位於我製作的帖子的上方 –

相關問題