如何检查php中会话丢失的原因是什么? [英] How to check what is the reason of session lost in php?

查看:36
本文介绍了如何检查php中会话丢失的原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经保存了用户登录时的用户名,但是我发现它有时会丢失,会话丢失的常见原因是什么?

I have stored the user id when the user login , however, i found it sometime will lost , what is the common reason of session lost?

我已经使用了超时插件(空闲一段时间会警告并帮助您注销)还有一些javascript可以在页面之间传输

I have used the timeout plugin (idle for sometime will warning and help you logout) and there are some javascript to transfer between pages

您已经编辑了列表.<a href='view.php' onClick='window.location.reload()'>返回</a></div>

<input type="button" value="Back" onclick="location.href='add.php'" class="btn" style="width:100px"/> 

并取消设置会话,但应该不是原因?

and unset the session, but it should not be the reason?

$(function(){
  $("#closeTab").click(function() {
            $.post("clear.php",function(data){
             window.parent.$('#tt').tabs('close','Create List'); 
             location.reload();     
      });
  });
});

clear.php

    if (isset($_SESSION['lname']))
unset($_SESSION['lname']);
if (isset($_SESSION['creminder']))
unset($_SESSION['creminder']);
if (isset($_SESSION['subscribe']))
unset($_SESSION['subscribe']);
if (isset($_SESSION['unsubscribe']))
unset($_SESSION['unsubscribe']);

这是用于存储会话

$user=$_SESSION['username'];

谢谢

推荐答案

PHP 处理会话非零到期的方式也有一个缺点;基本上,如果您将会话 cookie 设置为在 15 分钟后过期,它将在会话开始后的 15 分钟内过期...它不会刷新该过期时间.

There's also a foible with the way PHP handles non-zero expiries on sessions; basically if you set the session cookie to expire in 15 minutes, it will expire 15 minutes from the start of the session... it won't refresh that expiry time.

要运行在用户做某事"时刷新的会话,您需要将到期日期存储为会话变量,并在启动会话时检查该变量,并在必要时重新生成会话.

To run a session that refreshes whenever the user "does something" you need to store an expiry date as a session variable and, when booting up the session, check that variable and if necessary respawn the session.

我之前尝试在会话开始时更新会话 cookie 中的到期日期... 它导致了一些有趣的问题.

I've tried to update the expiry date in the session cookie previously, when the session is started... it led to some interesting problems.

这极不可能,但有可能,会话垃圾回收的生命周期也低于 cookie 到期的生命周期.有大量 ini 变量 可以处理其中一些常见会话问题,您可以通过在运行时设置它们来覆盖大部分问题:

It's highly unlikely, but it is possible, the session garbage collection lifetime is also below the lifetime of the cookie expiry. There are a load of ini variables that can deal with some of these common session problems and you can override most of them by setting them at runtime:

ini_set('session.gc_maxlifetime' 900);
ini_set('session.cookie_lifetime' 0); //ALWAYS set this to 0 - so the cookie will only expire when the browser is closed
ini_set('session.cookie_domain', '.domain.ext'); //always start with a "." if you want to cover multiple sub-domains
ini_set('session.cookie_path', '/'); //always use "/" unless you want to limit the cookie to a specific path "/admin" for instance

就个人而言,我会将所有会话处理内容放入一个(单例模式)类中,并在构造函数中处理验证和到期.

Personally, I'd put all the session handling stuff into a (Singleton pattern) class and deal with validation and expiry in the constructor.

这篇关于如何检查php中会话丢失的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆