r/PHPhelp • • Feb 09 '19

Does 'session_start();' cascade into other php files or do I have to declare it every time?

I am working on a project with no framework. Bare bones php 7.

Using sessions quite a bit.

Do I have to 'session_start();' at the top of every PHP page I want to use sessions in, or can I just declare it once at top of the index or controller file and the 'session_start(); will cascade into other files.

8 Upvotes

4 comments sorted by

6

u/RandyHoward Feb 09 '19
if(session_status() == PHP_SESSION_NONE) {
    session_start();
}

If I'm not working in a framework, I tend to throw that in the top of any file that needs the session. It'll ensure you start a session if there is none, and won't try to start one if one exists.

1

u/[deleted] Feb 09 '19

Good code snippet, Thanks.

1

u/[deleted] Feb 09 '19

[removed] — view removed comment

4

u/wh33t Feb 09 '19

It will throw a warning, depending on how your server is config'd this can be undesireable. I think.