Does session_start send headers?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Does session_start send headers?

Post 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?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

try ob_start
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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

&lt;?php
ob_start();
echo 'hello';
header("HTTP/1.0 404 Not Found");
?&gt;
Would send the header, but not the hello.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post 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?
Post Reply