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 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示