7

我正在使用以下技术...

login.php表单帖子到check.php我执行此操作的页面

<?php    
$uzer = $_POST['user_name'];
$pass = $_POST['user_pass'];

require ('DB_connection.php');

$result = mysql_query("SELECT * FROM accounts WHERE user_Name='$uzer' AND user_Pass='$pass'");

if( mysql_num_rows( $result ) > 0)
{
    $array = mysql_fetch_assoc($result);    

    session_start();
    $_SESSION['user_id'] = $uzer;
    header("Location:loggedin.php");            
}
else
{
    header("Location:login.php");
}
?>

loggedin.php页面上我做的第一件事是

<?php
session_start();
if( !isset( $_SESSION['user_id'] ) )
{
    header("Location:login.php");
}
else
{
    echo ( "this session is ". $_SESSION['user_id'] );
    //show rest of the page and all
}
?>

但是一旦登录,当我直接输入网址localhost\myProject\loggedin.php时,它就会显示页面......这非常有意义,因为会话已经开始

我想要实现的是

  • 直接 URL \ 会话在会话终止\过期\超时后工作 10 分钟,然后使用必须再次登录并可能获得相同的会话 ID,但 10 分钟后使用将无法使用相同的会话浏览

我需要做什么或学习什么

4

3 回答 3

10

在会话中存储时间戳:

<?php    
$uzer = $_POST['user_name'];
$pass = $_POST['user_pass'];

require ('DB_connection.php');

// Hey, always escape input if necessary!
$result = mysql_query(sprintf("SELECT * FROM accounts WHERE user_Name='%s' AND user_Pass='%s'", mysql_real_escape_string($uzer), mysql_real_escape_string($pass));

if( mysql_num_rows( $result ) > 0)
{
    $array = mysql_fetch_assoc($result);    

    session_start();
    $_SESSION['user_id'] = $uzer;
    $_SESSION['login_time'] = time();
    header("Location:loggedin.php");            
}
else
{
    header("Location:login.php");
}
?>

检查时间戳是否在允许的时间窗口内(600 秒为 10 分钟):

<?php
session_start();
if( !isset( $_SESSION['user_id'] ) || time() - $_SESSION['login_time'] > 600)
{
    header("Location:login.php");
}
else
{
    // uncomment the next line to refresh the session, so it will expire after ten minutes of inactivity, and not 10 minutes after login
    //$_SESSION['login_time'] = time();
    echo ( "this session is ". $_SESSION['user_id'] );
    //show rest of the page and all
}
?>
于 2010-09-22T14:31:22.970 回答
1

我会看看session_set_cookie_paramsini_set("session.gc_maxlifetime", "18000");

于 2010-09-22T14:31:12.273 回答
1

在您将开始会话的 php 文件中使用会话设置 cookie 函数,它将在定义 x 分钟后过期。

session_set_cookie_params(600);

如上所述,10 分钟会话到期后。

于 2021-06-01T09:49:20.820 回答