Page 1 of 1
Does session_start send headers?
Posted: Sun Dec 05, 2004 6:08 pm
by Benjamin
When using cookie based sessions, are headers immediately sent when session_start is called? I don't want any data to be sent to the browser, not even headers, until a script has finished execution, but I do need to start a session before the script starts executing.
Anyone know anything about this?
Posted: Sun Dec 05, 2004 6:22 pm
by dull1554
im pretty sure that it does because, i think it has to check the cookies on the user machine to see if it needs to start a session or to resume a current one
Posted: Sun Dec 05, 2004 6:40 pm
by Benjamin
I'm not sure that is correct, because cookies, even session cookies, are sent with the page request itself. So when session_start is called, it will already know whether or not session cookies were sent or not.
If your right, I can't use sessions then, I guess I will have to use cookies.
Another Idea I was throwing around was to use soley the ip and a database table to track logged in users.
Any thoughts on that?
Posted: Sun Dec 05, 2004 6:49 pm
by Benjamin
When exactly does PHP send headers? Right before the first byte of the webpage is sent or what? I have been searching google and can't find anything on this.
I found out that I can prevent anything from being sent by using...
Code: Select all
<?php
ob_start();
// all the code here blah blah
ob_end_flush();
?>
And with that you can set session variables and cookies even after echo statements, so that should work ok.
Posted: Sun Dec 05, 2004 7:12 pm
by josh
try ob_start
Posted: Sun Dec 05, 2004 7:13 pm
by timvw
output buffering won't help..
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
Posted: Sun Dec 05, 2004 7:57 pm
by Benjamin
This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.
This is actually misleading. What it means is that no output is sent except for
Manually Sent Headers
ie
Code: Select all
<?php
ob_start();
echo 'hello';
header("HTTP/1.0 404 Not Found");
?>
Would send the header, but not the hello.
Posted: Sun Dec 05, 2004 7:59 pm
by hedge
Session start does not send headers. Y would it? session start uses either the cookie or sid from the url (already sent by the browser) to locate the session file and build the session vars from that file.
Posted: Sun Dec 05, 2004 8:04 pm
by Benjamin
Cool, so when exactly does php decide to start sending headers out? After it has parsed a page and before webpage data starts being sent? This is something maybe only the developers know.
Posted: Mon Dec 06, 2004 3:12 am
by rehfeld
session_start() WILL send a header.
a cookie, is a header.
once php interprets the part of the code that should cause a header to be sent, it is sent
in this bit of code
Code: Select all
<?php
ob_start();
echo 'hello';
header("HTTP/1.0 404 Not Found");
?>
the 'hello' WILL be sent, but not before the header(because of output buffering)
if you dont manually empty the output buffer at the end of script execution,
php will automatically do it for you and output the contents to the browser
if this is not the desired behavior, you must call ob_clean() or ob_end_clean()
before the script finishes executing,
i would think you can test this sort of stuff by placing exit();
in strategic locations of your script, and then see for yourself.
Posted: Mon Dec 06, 2004 8:45 am
by hedge
hedge wrote:Session start does not send headers. Y would it? session start uses either the cookie or sid from the url (already sent by the browser) to locate the session file and build the session vars from that file.
Sorry should have qualified my response, bit of a brain lapse there. The cookie will only be sent the first time the session is established, for the rest of the pages called it just receives the cookie.
What is the root of the issue you're trying to solve?