Discussion:
ob_start & session_start
Joe Harman
2005-12-08 04:19:12 UTC
Permalink
Hello,

Something just crossed my mind about using output buffering.... is
there any reason why you should start a session before calling
ob_start() ???

Just curious which way would be the proper way of doing it... or
doesn't it matter?

Thanks

--
Joe Harman
---------
* My programs never have bugs, they just develop random features.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Joe Harman
2005-12-08 04:25:09 UTC
Permalink
Okay.. makes sense after you spelled it out to me... LOL... I always
start my session first.. so, that must why i have never had any
problems

Cheers & Thanks!
Joe
yes, it will display the content in the buffer before creating the session.
If your session uses cookies (this is usually automatically decided by php)
it cannot send out the header after the buffer.
Post by Joe Harman
Hello,
Something just crossed my mind about using output buffering.... is
there any reason why you should start a session before calling
ob_start() ???
Just curious which way would be the proper way of doing it... or
doesn't it matter?
Thanks
--
Joe Harman
---------
* My programs never have bugs, they just develop random features.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Joe Harman
---------
* My programs never have bugs, they just develop random features.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Chris Shiflett
2005-12-08 06:12:42 UTC
Permalink
Okay...makes sense after you spelled it out to me.
That didn't make sense to me (and I missed the original reply). Mind
elaborating? :-)

Chris
--
Chris Shiflett
Brain Bulb, The PHP Consultancy
http://brainbulb.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Zack Bloom
2005-12-08 06:23:03 UTC
Permalink
Sure, ob_start begins a buffer allowing you to display content in the
browser before your script has finished executing. This is useful when
loading a time intensive page to tell the user to wait. When you create a
session (provide php is not configured otherwise) php attempts to store a
cookie with the session id that corisponds to the session file on the
server. If it cannot set this cookie it appends the session id to pages in
the get format. If you were to call session_start() after the output
buffering, content and consequentially the headers would have been already
sent to the browser. Since cookies must be set in the headers and the
headers must be set before any content is sent to the page, to use cookie
based sessions you must begin the session before the buffer.

Hope that cleared it up,

Zack Bloom
Post by Chris Shiflett
Okay...makes sense after you spelled it out to me.
That didn't make sense to me (and I missed the original reply). Mind
elaborating? :-)
Chris
--
Chris Shiflett
Brain Bulb, The PHP Consultancy
http://brainbulb.com/
--
PHP General Mailing List ( http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Chris Shiflett
2005-12-08 07:18:16 UTC
Permalink
Post by Zack Bloom
Sure, ob_start begins a buffer allowing you to display content in
the browser before your script has finished executing.
Calling ob_start() turns on PHP's output buffering. In other words, it
buffers output from the moment this function is called until the buffer
is flushed (whether explicitly or because the script finishes).

As long as PHP is buffering the output, the client can't get it.
Post by Zack Bloom
This is useful when loading a time intensive page to tell the user
to wait.
I think you might be thinking of flush(), which flushes PHP's output
buffer as well as the output buffer of the web server (or whatever
backend PHP is using).
Post by Zack Bloom
When you create a session (provide php is not configured otherwise)
php attempts to store a cookie with the session id that corisponds
to the session file on the server. If it cannot set this cookie it
appends the session id to pages in the get format.
Yeah, PHP includes a Set-Cookie header in its response. If
session.use_trans_sid is enabled, it will also rewrite URLs to include
the session identifier. When PHP receives a request that includes a
session identifier, it knows whether the client accepts cookies.

if (session identifier in cookie)
{
cookies enabled
}
elseif (session identifier in URL)
{
cookies disabled
}
else
{
new user
}
Post by Zack Bloom
If you were to call session_start() after the output buffering,
content and consequentially the headers would have been already
sent to the browser.
Maybe you're getting the buffering and flushing concepts reversed? Think
of a toilet - buffering is the handle up, and flushing is the handle
down. :-)

Hope that helps!

Chris
--
Chris Shiflett
Brain Bulb, The PHP Consultancy
http://brainbulb.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Joe Harman
2005-12-08 06:23:40 UTC
Permalink
I guess this was just out of general curiousity... If you started
'session_start()' after 'ob_start()' would the sessions work
correctly? k.. maybe I am still confused... lol... I normally do
session_start() before the ob_start()...

----
Zack Said : 'yes, it will display the content in the buffer before
creating the session. If your session uses cookies (this is usually
automatically decided by php) it cannot send out the header after the
buffer.'
----

So, the question is really... in what order is the best way to do
this... I would think that you always want to start a session first...
but then again, you guys are the experts... i am sure someone knows a
reason when you should not do that.

;o)
Joe
Post by Chris Shiflett
Okay...makes sense after you spelled it out to me.
That didn't make sense to me (and I missed the original reply). Mind
elaborating? :-)
Chris
--
Chris Shiflett
Brain Bulb, The PHP Consultancy
http://brainbulb.com/
--
Joe Harman
---------
* My programs never have bugs, they just develop random features.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Curt Zirzow
2005-12-08 06:55:02 UTC
Permalink
Post by Joe Harman
So, the question is really... in what order is the best way to do
this... I would think that you always want to start a session first...
but then again, you guys are the experts... i am sure someone knows a
reason when you should not do that.
The first thing I would ask is why are you using/need ob_start().

session_start() should be the first thing that happens in most
cases. Since, well, any code that exist is potentially going to
rely on a session state.

If you are trying to use ob_start() to rid of the common error
'headers already sent in some_file.php on line such and such', i
would first consider why that is causing the error and how can you
call session_start() before that happens.

Since '97 i think i've used ob_start twice, in all my php apps, and
that was cause I wanted to filter the output either with tidy or
modify the data in a very obscure way.

Curt.
--
cat .signature: No such file or directory
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Zack Bloom
2005-12-08 07:41:20 UTC
Permalink
I have never tried it but if it did work i doubt it would use cookies, it
would probably pass it in the addresses or throw an error.
Post by Joe Harman
I guess this was just out of general curiousity... If you started
'session_start()' after 'ob_start()' would the sessions work
correctly? k.. maybe I am still confused... lol... I normally do
session_start() before the ob_start()...
----
Zack Said : 'yes, it will display the content in the buffer before
creating the session. If your session uses cookies (this is usually
automatically decided by php) it cannot send out the header after the
buffer.'
----
So, the question is really... in what order is the best way to do
this... I would think that you always want to start a session first...
but then again, you guys are the experts... i am sure someone knows a
reason when you should not do that.
;o)
Joe
Post by Chris Shiflett
Okay...makes sense after you spelled it out to me.
That didn't make sense to me (and I missed the original reply). Mind
elaborating? :-)
Chris
--
Chris Shiflett
Brain Bulb, The PHP Consultancy
http://brainbulb.com/
--
Joe Harman
---------
* My programs never have bugs, they just develop random features.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Zack Bloom
2005-12-08 07:43:14 UTC
Permalink
Sorry it looked like the rest of your email was part of the previous one.
To answer your question it is better to call session_start() before
ob_start()
Post by Zack Bloom
I have never tried it but if it did work i doubt it would use cookies, it
would probably pass it in the addresses or throw an error.
Post by Joe Harman
I guess this was just out of general curiousity... If you started
'session_start()' after 'ob_start()' would the sessions work
correctly? k.. maybe I am still confused... lol... I normally do
session_start() before the ob_start()...
----
Zack Said : 'yes, it will display the content in the buffer before
creating the session. If your session uses cookies (this is usually
automatically decided by php) it cannot send out the header after the
buffer.'
----
So, the question is really... in what order is the best way to do
this... I would think that you always want to start a session first...
but then again, you guys are the experts... i am sure someone knows a
reason when you should not do that.
;o)
Joe
Post by Chris Shiflett
Okay...makes sense after you spelled it out to me.
That didn't make sense to me (and I missed the original reply). Mind
elaborating? :-)
Chris
--
Chris Shiflett
Brain Bulb, The PHP Consultancy
http://brainbulb.com/
--
Joe Harman
---------
* My programs never have bugs, they just develop random features.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...