By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,335 Members | 2,281 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,335 IT Pros & Developers. It's quick & easy.

new at sessions

P: n/a
Hello, I am using PHP 4.3.1 with register_globals off.

I want to pass information from one page to another and want to start a
session for this using: session_start() at the beginning of my page
searchresult.php

However, I get the following error:

Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at
/srv/www/htdocs/searchresult.php:7) in /srv/www/htdocs/searchresult.php
on line 8

Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
/srv/www/htdocs/searchresult.php:7) in /srv/www/htdocs/searchresult.php
on line 8

A session_ID is created but any $_SESSION['variable'] I define is not
recognised. What am I doing wrong??

Thanks in advance,
Edward

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"edward hage" <ed**@xs4all.nl> wrote in message
news:3f*********************@news.xs4all.nl...
Hello, I am using PHP 4.3.1 with register_globals off.

I want to pass information from one page to another and want to start a
session for this using: session_start() at the beginning of my page
searchresult.php

However, I get the following error:

Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at
/srv/www/htdocs/searchresult.php:7) in /srv/www/htdocs/searchresult.php
on line 8

Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
/srv/www/htdocs/searchresult.php:7) in /srv/www/htdocs/searchresult.php
on line 8

A session_ID is created but any $_SESSION['variable'] I define is not
recognised. What am I doing wrong??

Thanks in advance,
Edward


you have to call ob_start() before any other output, including spaces. so
put it right at the top of the page, and it should work ok.
Leslie
Jul 17 '05 #2

P: n/a
Leslie Hoare wrote:
"edward hage" <ed**@xs4all.nl> wrote in message
news:3f*********************@news.xs4all.nl...
Hello, I am using PHP 4.3.1 with register_globals off.

I want to pass information from one page to another and want to start a
session for this using: session_start() at the beginning of my page
searchresult.php

However, I get the following error:

Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at
/srv/www/htdocs/searchresult.php:7) in /srv/www/htdocs/searchresult.php
on line 8

Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
/srv/www/htdocs/searchresult.php:7) in /srv/www/htdocs/searchresult.php
on line 8

A session_ID is created but any $_SESSION['variable'] I define is not
recognised. What am I doing wrong??

Thanks in advance,
Edward

you have to call ob_start() before any other output, including spaces. so
put it right at the top of the page, and it should work ok.
Leslie

I tried ob_start(); before session_start(); but that did not help.

I also don't see why the connection between buffering output and a
session. Could anyone explain more about that?

Greetings, Edward

Jul 17 '05 #3

P: n/a
edward hage wrote:

Leslie Hoare wrote:
"edward hage" <ed**@xs4all.nl> wrote in message
news:3f*********************@news.xs4all.nl...
Hello, I am using PHP 4.3.1 with register_globals off.

I want to pass information from one page to another and want to start a
session for this using: session_start() at the beginning of my page
searchresult.php

However, I get the following error:

Warning: session_start() [function.session-start]: Cannot send session
cookie - headers already sent by (output started at
/srv/www/htdocs/searchresult.php:7) in /srv/www/htdocs/searchresult.php
on line 8

Warning: session_start() [function.session-start]: Cannot send session
cache limiter - headers already sent (output started at
/srv/www/htdocs/searchresult.php:7) in /srv/www/htdocs/searchresult.php
on line 8

A session_ID is created but any $_SESSION['variable'] I define is not
recognised. What am I doing wrong??

Thanks in advance,
Edward

you have to call ob_start() before any other output, including spaces. so
put it right at the top of the page, and it should work ok.
Leslie

I tried ob_start(); before session_start(); but that did not help.

I also don't see why the connection between buffering output and a
session. Could anyone explain more about that?


session_start() sends a cookie to the user's browser. Cookies must be sent in
the header. You cannot send headers once you start to send content, even a
single space or linebreak. The solution, then, is to put session_start() at the
top of your file, if possible.

This works:
-------------- START OF FILE ------------------
<?PHP
session_start();
?>
-------------- END OF FILE ------------------

This doesn't work because it outputs a blank line before the cookie is sent:
-------------- START OF FILE ------------------

<?PHP
session_start();
?>
-------------- END OF FILE ------------------
You don't need ob_start() to use sessions. It can be helpful, though, if you
need to output text before you call session_start(). It sticks the text in a
buffer, effectively delaying sending it until after session_start() sends the
cookie.

This works because it delays sending outputted text until after the cookie is
sent:
-------------- START OF FILE ------------------
<?PHP
ob_start();
?>

Hello World.

<?PHP
session_start();
ob_flush();
?>
-------------- END OF FILE ------------------

Regards,
Shawn

--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.