Jump to content

[SOLVED] session start on an include page


Dirty-Rockstar

Recommended Posts

user logs in, passes thru auth.php..if that passes $_Session[usersession] is now true.

on include.php i have

 


session_start();

 

include.php is on all pages. do i have to have

 

session_start();

 

on any other pages to keep session active? or will include.php do it for me

 

hope i worded that right

Link to comment
Share on other sites

I just found this as a comment in the manual:

 

TAGS: session_start headers output errors include_once require_once php tag new line

Errors with output headers related to *session_start()* being called inside include files.

If you are starting your session inside an include file you must be aware of the presence of undesired characters after php end tag.

Let's take an example:
> page.php
<?php
include_once 'i_have_php_end_tag.inc.php';
include_once 'init_session.inc.php';

echo "Damn! Why I'm having these output header errors?";
?>

> i_have_php_end_tag.inc.php
<?php
$_JUST_A_GLOBAL_VAR = 'Yes, a global var, indeed';
?>

> init_session.inc.php
<?php
session_start();
$_SESSION['blabla'] = 123;
?>

With all this stuff we will get an error, something like:
"... Cannot send session cache limiter - headers already sent (output started at ...", right?

To solve this problem we have to ignore all output sent by include files. To ensure that we need to use the couple of functions: *ob_start()* and *ob_end_clean()* to suppress the output. So, all we have to do is changing the *page.php* to this:

<?php
ob_start();
include_once 'i_have_php_end_tag.inc.php';
include_once 'init_session.inc.php';
ob_end_clean();

echo "Woo hoo! All right! Die you undesired outputs!!!";
?> 

 

**if Im not allowed to post stuff like this admins please delete and let me know  :P

Link to comment
Share on other sites

if your include is on the very top (except your start tag <?php) and your session start is on the very top (except your start tag <?php) of your include page it should work

 

yep that is exactly how it is, i always put my included pages at the top of each page before i code (after the start tags of course) and on include.php the first thing is the session

 

thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.