我正在使用以下技术......
从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
时登录后,它会显示页面...这非常有意义,因为会话已经开始
我想要实现的是
我需要做什么或学习
答案 0 :(得分: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
}
?>
答案 1 :(得分:1)
我会查看session_set_cookie_params和ini_set("session.gc_maxlifetime", "18000");
答案 2 :(得分:1)
在您将开始会话的 php 文件中使用会话设置 cookie 功能,它将在按照定义 x 分钟后过期。
session_set_cookie_params(600);
在 10 分钟会话到期后,如上所述。