Php session_start with files included

I learned a lot about the start of the session from the previous question . Now I am wondering how the session is locked when files are included in other files. Let's say I have:

page.php

include('header.php'); ...some html content.... include('sub_page.php'); ...more html.... 

header.php:

 session_start(); ..save session vars... ..print web page header... 

sub_page.php

 session_start(); ...use session vars.... ..print page content... 

When I open page.php, does the session unlock as soon as the header lights up? or is it alive for the whole page.php page, so is the sub_page session blocked? Is session_start required in sub_page? Would it be better if I session_write_close every time I do session data? (Although this will mean session_start every time I want to use a session variable).

+8
php session
source share
5 answers
  • You must start a session only once. In your example, you just need session_start () in the first line of page.php
  • session_start () will generate E_NOTICE if the session was previously started. You can use @session_start () to ignore it.
  • It also generates E_NOTICE if you use session_start () after the HTML output.
+9
source share

I would recommend creating a session.php file that you would include once on the first line of each page. Thus, the session is processed in ONE file if you need to change the verification or session parameters (and you do not need to worry about your question).

+10
source share

Starting with PHP 4.3.3, calling session_start () after the session was previously started will result in an E_NOTICE level error. In addition, starting a second session will simply be ignored.

+6
source share

Due to the above error answers, if the session is already running, I just wanted to indicate what you can do:

 if (!isset($_SESSION)) { session_start(); } 

Then, if $ _SESSION is already running (installed), it will not perform the launch function.

Although there is nothing better than a well-structured layout of the file and folders with a good structure setting. Even if just a simple structure structure that separates business logic from presentation.

Thus, you will have something similar to a configuration folder with initialization scripts, or at least include files in some folder that are included in all pages / scripts.

Then you simply have your session_start() in (depending on your installation) either the very first include file, or in a separate include file, and then include this session file, when necessary, in a specific area of ​​the script.

In any case, you do not need to name it in any other files, as you know that this is simply not required based on your design structure.

If you do not have a file that is always included, at least use the isset() check.

+4
source share

Until you access or create session variables, you do not need to worry about session_start (). You really need to worry about session_start if the script you use will create session variables or use access to session functions.

If file1 does not have access or creates variables for use by other scripts, then do not call it. If file2, included by file 1, creates or uses variables in the session, then file2 should call session_start (). File2 will be included in the session and will have access to all session variables, but file1 will not.

If you call session_start () in file1, then file2 will have access to all session vars, as if it was called session_start ().

We hope that this once again clarifies the situation.

Great tip from James using isset. This will prevent an attempt at a pointless session call.

Also check the php.ini file for session.auto_start var. If this value is 1, all files will be launched as if they had made a session_start () call. Set it to 0 in the php.ini file if you want to control it yourself.

+1
source share

All Articles