Write session start on 1 page or all pages?

前端 未结 4 1028
盖世英雄少女心
盖世英雄少女心 2020-12-28 15:24

All the tutorials say to put session start. They don\'t say if that should be in all pages on the website, or some, or only 1.

And if it\'s only 1 page, does it h

相关标签:
4条回答
  • 2020-12-28 16:09

    Just for a matter of completeness you can choose to write session_start(); in all pages, in just one or in none of them. Let me explain this.

    You need to start session in every script where you need access to $_SESSION variable but instead of putting session_start(); in every single script you can create a file headers.php and put there all your repetitive code including session_start();

    If everything in your application needs access to $_SESSION you can forget the use of session_start(); simply setting session.auto_start = 1 in your php.ini file. You will be able to access $_SESSION without writing session_start(); before.

    More here

    0 讨论(0)
  • 2020-12-28 16:09

    You need to declare session_start(); in every page if you want to get data from $_SESSION or store data into $_SESSION in those particular page. If you do not need to interact with $_SESSION then you don't have to declare session_start().@hmwhat

    0 讨论(0)
  • 2020-12-28 16:13

    Anything that is going to access Session variables needs to start the session.

    So unless you have a php page that is non-dependent on the session than every page needs it.

    0 讨论(0)
  • 2020-12-28 16:26

    You need to put this in each page that need to access the session data before accessing (or creating) any session data.

    See: http://php.net/manual/en/function.session-start.php

    0 讨论(0)
提交回复
热议问题