Jump to content
Larry Ullman's Book Forums

Position Of Session_Start() In Files With Include()


Recommended Posts

Hello all,

 

On my website I use a "body template" to hold contents of various pages, and the body template files will call for a "header template" to display the header contents of the site, like in the following two template files:

 

Note: the "body.php" file runs first.

 

HEADER TEMPLATE FILE 'HEADER.php':
================================

<?php
session_start();  //POSITION "A"

if (...) { ... }
else { ... }
?>
<!doctype html>
etc etc
</head>

<body>
<!-- END HEADER TEMPLATE FILE -->

 

BODY TEMPLATE FILE 'body.php':
================================

<?php
session_start();  //POSITION "B"

if (...) { ... }
else { ... }

include('HEADER.php');
?>
<!-- BEGIN BODY CONTENTS -->

Body contents etc etc

<!-- END BODY CONTENTS -->
<?php
include('FOOTER.php');
?>
<!-- END BODY TEMPLATE FILE -->

 

 

And as you can see, I also use session on the site as well. I learned that session_start() must be placed in the beginning of a file, so currently I have session_start() in both the body.php and HEADER.php files (see positions "A" and "B" in the templates above). Currently the pages are displaying fine but somehow I think I might have missed something.

 

Could someone please let me know if the above use is correct?

 

Or, should I use only one session_start() in one of the two templates? And, which one should I keep (position "A" or "B")?

 

Thanks in advance!

Cofa

Link to comment
Share on other sites

Calling session_start in PHP 5.3 and later will not hurt anything. Here are the details:

http://stackoverflow.com/questions/2580322/is-there-any-harm-in-running-session-start-multiple-times-as-the-page-request

 

As for where to put it, I think it depends on whether every page that includes the header file requires a session or not. If every page that includes the header file requires a session, put the session_start function call in the header file (it'll save you some typing and potential editing later). Otherwise, I think you should put it at the top of the individual PHP pages you need the session for.

 

Of course, there is probably no harm in calling the session_start function and not actually using the session, so I guess you could put it in the header file, even if a page that includes the header file doesn't actually access the session.

  • Upvote 1
Link to comment
Share on other sites

Hello Hartley San,

 

Thank you for the advice - this shall clear my confusion for sure. I think I'll stick with putting the session start in the BODY file (position "B") as this file will always run first.

 

But I do have a question about putting the session start in the HEADER file (position "A"). If there are some scripts run in the beginning of the BODY file before it calls the HEADER file, would that session start function in the HEADER file still work properly - as it is called after some scripts have been executed?

Link to comment
Share on other sites

Yes, session_start() can be executed any time ANYTHING (including blanks spaces) is sent to the browser. Most servers these days have output buffering enabled by default, in my experience, so you may never see a problem with it regardless of where you put it. 

Link to comment
Share on other sites

Thanks to HartleySan and Larry for the further explanation and confirmation. So it would in turn also suggest that putting the session_start() in the header file is a better practice (as per HartleySan's first message).

Link to comment
Share on other sites

 Share

×
×
  • Create New...