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().

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失