我需要保持一个会话存活30分钟,然后销毁它。
当前回答
在这里你可以设定时间
$lifespan = 1800;
ini_set('session.gc_maxlifetime', $lifespan); //default life time
其他回答
使用此课程30分钟
class Session{
public static function init(){
ini_set('session.gc_maxlifetime', 1800) ;
session_start();
}
public static function set($key, $val){
$_SESSION[$key] =$val;
}
public static function get($key){
if(isset($_SESSION[$key])){
return $_SESSION[$key];
} else{
return false;
}
}
public static function checkSession(){
self::init();
if(self::get("adminlogin")==false){
self::destroy();
header("Location:login.php");
}
}
public static function checkLogin(){
self::init();
if(self::get("adminlogin")==true){
header("Location:index.php");
}
}
public static function destroy(){
session_destroy();
header("Location:login.php");
}
}
使用session_set_cookie_params函数来做到这一点。
有必要在session_start()调用之前调用这个函数。
试试这个:
$lifetime = strtotime('+30 minutes', 0);
session_set_cookie_params($lifetime);
session_start();
详见:http://php.net/manual/function.session-set-cookie-params.php
在会话中存储时间戳
<?php
$user = $_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($user), mysql_real_escape_string($pass));
if( mysql_num_rows( $result ) > 0)
{
$array = mysql_fetch_assoc($result);
session_start();
$_SESSION['user_id'] = $user;
$_SESSION['login_time'] = time();
header("Location:loggedin.php");
}
else
{
header("Location:login.php");
}
?>
现在,检查时间戳是否在允许的时间窗口内(1800秒是30分钟)
<?php
session_start();
if( !isset( $_SESSION['user_id'] ) || time() - $_SESSION['login_time'] > 1800)
{
header("Location:login.php");
}
else
{
// uncomment the next line to refresh the session, so it will expire after thirteen minutes of inactivity, and not thirteen minutes after login
//$_SESSION['login_time'] = time();
echo ( "this session is ". $_SESSION['user_id'] );
//show rest of the page and all other content
}
?>
您应该实现自己的会话超时。其他人提到的两个选项(会话。Gc_maxlifetime和session.cookie_lifetime)不可靠。我会解释其中的原因。
第一:
session.gc_maxlifetime 会话。Gc_maxlifetime指定数据被视为“垃圾”并被清理的秒数。在会话启动期间发生垃圾收集。
但是垃圾收集器只在会话概率时启动。gc_概率除以session。gc_除数。如果使用这些选项的默认值(分别为1和100),概率只有1%。
您可以简单地调整这些值,以便更频繁地启动垃圾收集器。但是当垃圾收集器启动时,它将检查每个已注册会话的有效性。这是成本密集型的。
此外,当使用PHP的默认会话时。Save_handler文件,会话数据保存在session.save_path中指定路径下的文件中。使用该会话处理程序,会话数据的年龄是根据文件的最后修改日期计算的,而不是最后访问日期:
注意:如果您正在使用默认的基于文件的会话处理程序,您的文件系统必须跟踪访问时间(atime)。Windows FAT没有,所以如果您被FAT文件系统或任何其他时间跟踪不可用的文件系统所困,您将不得不想出另一种方法来处理会话的垃圾收集。自PHP 4.2.3以来,它使用mtime(修改日期)而不是atime。因此,对于时间跟踪不可用的文件系统,您不会遇到问题。
因此,在删除会话数据文件时,由于会话数据最近没有更新,因此会话本身仍然被认为是有效的。
第二:
session.cookie_lifetime 会话。Cookie_lifetime指定发送到浏览器的cookie的生存期,以秒为单位。[…]
是的,没错。这只会影响cookie的生存期,会话本身可能仍然有效。但是使会话无效是服务器的任务,而不是客户端。所以这没有任何帮助。事实上,正在进行会话。Cookie_lifetime设置为0将使会话的cookie成为真正的会话cookie,它只在浏览器关闭之前有效。
结论/最佳解决方案:
最好的解决方案是实现您自己的会话超时。使用一个简单的时间戳来表示最后一个活动(即请求)的时间,并在每个请求时更新它:
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
}
$_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
为每个请求更新会话数据还会改变会话文件的修改日期,这样会话就不会被垃圾收集器过早地删除。
你也可以使用一个额外的时间戳来定期重新生成会话ID,以避免会话固定等攻击:
if (!isset($_SESSION['CREATED'])) {
$_SESSION['CREATED'] = time();
} else if (time() - $_SESSION['CREATED'] > 1800) {
// session started more than 30 minutes ago
session_regenerate_id(true); // change session ID for the current session and invalidate old session ID
$_SESSION['CREATED'] = time(); // update creation time
}
注:
会话。Gc_maxlifetime应该至少等于这个自定义过期处理程序的生命周期(在本例中为1800); 如果你想让会话在活动30分钟后过期,而不是在启动后30分钟后过期,你还需要使用setcookie的过期时间()+60*30来保持会话cookie的活动状态。
PHP会话在30分钟内到期的简单方法。
注意:如果你想改变时间,只需要改变30和你想要的时间,不要改变* 60:这将给出分钟。
分钟:(30 * 60) (n * 24 * 60 * 60) n = no天
login。
<?php
session_start();
?>
<html>
<form name="form1" method="post">
<table>
<tr>
<td>Username</td>
<td><input type="text" name="text"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td><input type="submit" value="SignIn" name="submit"></td>
</tr>
</table>
</form>
</html>
<?php
if (isset($_POST['submit'])) {
$v1 = "FirstUser";
$v2 = "MyPassword";
$v3 = $_POST['text'];
$v4 = $_POST['pwd'];
if ($v1 == $v3 && $v2 == $v4) {
$_SESSION['luser'] = $v1;
$_SESSION['start'] = time(); // Taking now logged in time.
// Ending a session in 30 minutes from the starting time.
$_SESSION['expire'] = $_SESSION['start'] + (30 * 60);
header('Location: http://localhost/somefolder/homepage.php');
} else {
echo "Please enter the username or password again!";
}
}
?>
HomePage.php
<?php
session_start();
if (!isset($_SESSION['luser'])) {
echo "Please Login again";
echo "<a href='http://localhost/somefolder/login.php'>Click Here to Login</a>";
}
else {
$now = time(); // Checking the time now when home page starts.
if ($now > $_SESSION['expire']) {
session_destroy();
echo "Your session has expired! <a href='http://localhost/somefolder/login.php'>Login here</a>";
}
else { //Starting this else one [else1]
?>
<!-- From here all HTML coding can be done -->
<html>
Welcome
<?php
echo $_SESSION['luser'];
echo "<a href='http://localhost/somefolder/logout.php'>Log out</a>";
?>
</html>
<?php
}
}
?>
LogOut.php
<?php
session_start();
session_destroy();
header('Location: http://localhost/somefolder/login.php');
?>
推荐文章
- 如何将XML转换成PHP数组?
- 如何将对象转换为数组?
- 从IP地址获取位置
- 获取数组值的键名
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- PHP -获取bool值,当为false时返回false
- 在foreach中通过引用传递
- 如何触发命令行PHP脚本的XDebug分析器?
- 如何找出如果你使用HTTPS没有$_SERVER['HTTPS']
- 更好的方法检查变量为null或空字符串?
- 当使用Composer的开发/生产开关时,如何正确部署?
- 自动删除Laravel (Eloquent ORM)中的相关行
- 在取消设置元素后重新设置数组键
- 如何修剪空白的数组值在php
- PHP中的双not(!!)操作符