简体   繁体   English

如何检查会话是否过期或从未在php中设置

[英]How to check if a session is expired or never was set in php

How can I check if a session was ever set or was set but expired? 如何检查会话是否已设置或已设置但已过期?

I am using this code and I want to make an echo if the session has expired like this: Session has expired. Please login again 我正在使用此代码,如果会话已过期,我想做个回应: Session has expired. Please login again Session has expired. Please login again

This is my code: 这是我的代码:

session_start();
if(!isset($_SESSION['sfmlogin'])){
?>
<a href="login.php">Login again</a>
<?php
exit();
}

So for users who visit the page for the first time, they should not get an echo like session expired because they did never login before. 因此,对于首次访问该页面的用户,他们不应收到像session expired类的回声,因为他们之前从未登录过。

The information you are looking for is in the session meta-data - but this is not exposed via the API. 您正在寻找的信息在会话元数据中-但是不会通过API公开。 The default handler uses files - so if the file exists, then there was a session. 默认处理程序使用文件-因此,如果文件存在,则存在一个会话。 but you should not attempt to bypass the handler to get session metadata! 但您不应尝试绕过处理程序以获取会话元数据! That would be a very bad idea. 那将是一个非常糟糕的主意。 Really, it sounds like you should be writing your own session handler which also exposes the metadata. 确实,听起来您应该编写自己的会话处理程序 ,该处理程序还应公开元数据。

Session has expired 会话已过期

This is a very complicated proposition: with the default handler (leaving aside the problem of bypassing the handler to get the meta-data) there is no session expiration - there is only garbage collection of session data which hasn't been touched for some time. 这是一个非常复杂的命题:使用默认处理程序(除了绕过处理程序以获取元数据的问题),没有会话到期-只有一段时间未触及的会话数据的垃圾收集。 On some systems this is handled by a batch process. 在某些系统上,这是通过批处理来处理的。 On some it is a stochastic event. 在某些情况下,这是一个随机事件。 So in effect, and in this context, there is no difference between an expired session and no session from the server's point of view. 因此,实际上,在这种情况下,从服务器的角度来看,过期的会话与没有会话之间没有区别。

You could infer from the cookies if the browser thinks it should have a session: 如果浏览器认为应该进行会话,则可以从cookie推断:

session_start();
if ($_COOKIE(session_name() && !count($_SESSION)) {
    print "session has expired";
    exit;
} elsif (!$_COOKIE(session_name()) {
    print "You need to login first";
    exit;
} else {
    print "welcome back....";
    ....

...but do make sure that you put something in the session when the user logs in. ...但一定要确保你把东西在会话当用户登录英寸

How about doing a boolean check on session_status()? 如何对session_status()进行布尔检查? If it returns true, then the session is active, else, it would return a "Session Expired" notice. 如果返回true,则该会话处于活动状态,否则,将返回“会话已过期”通知。 http://php.net/manual/en/function.session-status.php http://php.net/manual/zh/function.session-status.php

Edit : You would only need to check session_status() after a user has logged in. If they have not, then the session has not been started, therefore $_SESSION would already be false and a session expiration notice wouldn't be possible. 编辑 :您只需要在用户登录后检查session_status()。如果尚未登录,则会话尚未启动,因此$ _SESSION将为false,并且将不会有会话到期通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM