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条)

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试