Connecting Tech Pros Worldwide Forums | Help | Site Map

new at sessions

edward hage
Guest
 
Posts: n/a
#1: Jul 17 '05
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


Leslie Hoare
Guest
 
Posts: n/a
#2: Jul 17 '05

re: new at sessions



"edward hage" <edha@xs4all.nl> wrote in message
news:3fd31c70$0$201$e4fe514c@news.xs4all.nl...[color=blue]
> 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
>[/color]

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


edward hage
Guest
 
Posts: n/a
#3: Jul 17 '05

re: new at sessions


Leslie Hoare wrote:[color=blue]
> "edward hage" <edha@xs4all.nl> wrote in message
> news:3fd31c70$0$201$e4fe514c@news.xs4all.nl...
>[color=green]
>>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
>>[/color]
>
>
> 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
>
>[/color]
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

Shawn Wilson
Guest
 
Posts: n/a
#4: Jul 17 '05

re: new at sessions


edward hage wrote:[color=blue]
>
> Leslie Hoare wrote:[color=green]
> > "edward hage" <edha@xs4all.nl> wrote in message
> > news:3fd31c70$0$201$e4fe514c@news.xs4all.nl...
> >[color=darkred]
> >>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
> >>[/color]
> >
> >
> > 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
> >
> >[/color]
> 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?[/color]



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
shawn@glassgiant.com
http://www.glassgiant.com
Closed Thread