doudengjin8251 2009-02-11 07:47
浏览 10
已采纳

如何检查在PHP中注册的会话?

I am creating a site and I'm using sessions to manage the user log in status. When a user logs in, I create a session id for the user like so:

$_SESSION['username'] = "username";

How can I check if a user is logged in, and if so, who it is?

  • 写回答

1条回答 默认 最新

  • dongna1593 2009-02-11 07:54
    关注

    If, after you have authenticated the user you are writing $_SESSION['username'] to the the session superglobal, all you need to do is double check the existance of a session username:

     session_start()
     if (empty($_SESSION['username'])) {
          // user is not logged in
     }
    

    Figuring out who is logged in is as simple as checking what username you have stored in the $_SESSION['username'] variable that we checked earlier. If you wanted to save their real name from a database result, or other information, just dump that information into the $_SESSION['data_type'] array as well.

    The session variables should automatically expire in 24 minutes (PHP default, I think) of inactivity, in which case you should expect the user to login again. If you want to log the user out immediately, check out session_destroy().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度