douwei1904 2014-02-16 18:38
浏览 98
已采纳

声明session_start()函数?

I am designing a login system using core PHP, please refer to the following code -

funciton login($username,$password)  {

//  mysql query to check the username and password

if($res == 1)  {

    session_start();
$_SESSION['username'] = $username;
$_SESSION['loggedin'] = 1;
return true;

}

}

As you can see that I have started the session inside the login function, someone please tell me is it correct or do i start the session on every page of the applicaion i.e. in the common header file?

  • 写回答

2条回答 默认 最新

  • dtewnsdf47253 2014-02-16 18:49
    关注

    You should try and see that the "session" that session_start() starts has nothing to do with being logged in or not. Start the session as one of the first things you do, in every script of your application (if your application needs any session management, that is).

    The session represents the "visit" of a unique visitor on your website. A visit can, in theory, consist of consecutive login/logout pairs. It is still the same visit, but just a different login.

    For example, if you have a "Choose a language" dropdown on your website, you may want to store the choice a user made in the session. When a user logs out, you still may want to preserve that chosen language. The information about a user being logged in or not is just one part of what sessions can do, so the session should be present independently of a possible login.

    You can then store a "logged in" flag and a username in that session and just unset these two values once the visitor logs out.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历