PHP - 使会话在 X 分钟后过期
我正在使用以下技术...
从 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< /code> 页面我做的第一件事是,
<?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
}
?>
但是一旦登录,当我直接输入 url localhost\myProject\loggedin.php
时,它会显示该页面...这非常有意义,因为会话已经开始
我想要实现的是
- 直接 URL \ 会话在会话终止\过期\超时后工作 10 分钟,然后使用必须再次登录并可能获得相同的会话 ID,但 10 分钟后使用将无法使用同一会话进行浏览
我需要做什么或学习
i am using the following technique...
From the login.php
the form posts to the page check.php
where i do this
<?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");
}
?>
and on loggedin.php
page the first thing i do is
<?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
}
?>
but once logged in when i directly type the url localhost\myProject\loggedin.php
it displays the page...which makes perfect sense because the session has started
what i want to implement is
- The direct URL \ session works for 10 minutes after that the session is terminated\expired\timed out and then use must login again and may get the same session id but after 10 minutes use won't be able to browse with the same session
WHAT DO I NEED TO DO OR LEARN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在会话中存储时间戳:
检查时间戳是否在允许的时间窗口内(600秒是10分钟):
Store a timestamp in the session:
Check if the timestamp is within the allowed time window (600 seconds is 10 minutes):
我会看看 session_set_cookie_params 和
ini_set("session.gc_maxlifetime", "18000");
I would look at session_set_cookie_params and
ini_set("session.gc_maxlifetime", "18000");
在您将启动会话的 php 文件中使用会话设置 cookie 功能,它将在定义的 x 分钟后过期。
如上所述,10 分钟后会话到期。
Use session set cookie function in your php file where you will start session, it will expire after as per define x minutes.
As per above after 10 minutes session is expire.