tags:

views:

62

answers:

2

can I include my session_start() in my header include or should I past session_start() in every pages? Is there any pros or cons in pasting the session_start() in the header include?

+3  A: 

session_start() should probably be one of the first lines. you have to start the session before any data is output. if you're using includes to simulate a template system then stick it in the page controller.

daedalus0x1a4
+1  A: 

The only real downside is if you ever want a session-less page. Then you'll have to have some convention to disable it.

<?php

define( 'NO_SESSION', true );

include( 'header.php' );

?>

header.php

<?php

if ( !defined( 'NO_SESSION' ) )
{
  session_start();
}
Peter Bailey
If you want to have a session-less page, you just don't call any $_SESSION variable :-)
Vinko Vrsalovic
@Vinko, no, you'll still be loading the user session—which takes (some) time.
aharon
well, i want to ask why you would want a session-less page in some parts of the application but not others. in that case you should be able to easily call session_destroy()
daedalus0x1a4
@aharon: True, although I really think that it would be worthwhile in very few cases (when you want a very fast page that gets called a lot that doesn't use the session at all and your session variables are usually big.)
Vinko Vrsalovic