note 73952 added to function.session-start

From: Date: Sat, 17 Mar 2007 12:21:44 +0000
Subject: note 73952 added to function.session-start
Groups: php.notes 
Request: Send a blank email to php-notes+get-124536@lists.php.net to get a copy of this message
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!!!"; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 200.230.62.36 (proxied: 172.16.7.74) ---- Manual Page -- http://www.php.net/manual/en/function.session-start.php Edit -- https://master.php.net/note/edit/73952 Del: integrated -- https://master.php.net/note/delete/73952/integrated Del: useless -- https://master.php.net/note/delete/73952/useless Del: bad code -- https://master.php.net/note/delete/73952/bad+code Del: spam -- https://master.php.net/note/delete/73952/spam Del: non-english -- https://master.php.net/note/delete/73952/non-english Del: in docs -- https://master.php.net/note/delete/73952/in+docs Del: other reasons-- https://master.php.net/note/delete/73952 Reject -- https://master.php.net/note/reject/73952 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#124536) next »