Does session_start send headers?
Moderator: General Moderators
Does session_start send headers?
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?
Anyone know anything about this?
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?
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?
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...
And with that you can set session variables and cookies even after echo statements, so that should work ok.
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();
?>This is actually misleading. What it means is that no output is sent except for Manually Sent HeadersThis 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.
ie
Code: Select all
<?php
ob_start();
echo 'hello';
header("HTTP/1.0 404 Not Found");
?>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
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.
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");
?>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.
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.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.
What is the root of the issue you're trying to solve?