This is a discussion on Headers and sessions in php .cgi within the PHP General forums, part of the PHP Programming Forums category; Hi there, I have a problem setting my headers right with php running as .cgi. I have to specify a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi there, I have a problem setting my headers right with php running as .cgi. I have to specify a Content-type for the cgi file to work. But how should I do both that and start a session? I have a few options: #! /usr/local/bin/php <?php session_start(); print 'Content-type: text/html' . "\n\n"; This way $_SESSION['count'] stays unset even though I say $_SESSION['count'] = 1; in my program. To swap the two lines won't work because I heard \n\n terminates the header portion. Anyway, here goes: #! /usr/local/bin/php <?php print 'Content-type: text/html' . "\n\n"; session_start(); Now I get "Warning: session_start(): Cannot send session cookie - headers already sent by... line 3" and then "Warning: session_start(): Cannot send session cache limiter - headers already sent...". I have tried replacing print 'Content-type: text/html' . "\n\n"; by print 'Content-type: text/html' . "\n"; (one \n instead of two) with exactly the same result. With no \n at all I only get "Warning: session_start(): Cannot send session cache limiter - headers already sent". The next thing I try is to replace print 'Content-type: text/html' and the \n's by sending the same text in header(). This variety gives me a "500 Internal Server Error" independant on the number of \n's. Anyway, here's all my code. No matter what I do it always prints "Your visit number 1". $_SESSION['count'] doesn't get increased at all! ======================== #! /usr/local/bin/php <?php print 'Content-type: text/html' . "\n\n"; session_start(); print '<html>' . "\n"; print '<body>' . "\n"; if (!isset($_SESSION['count'])) { $_SESSION['count'] = 1; } else { $_SESSION['count']++; } print 'Your visit number ' . $_SESSION['count'] . "\n"; print '</body>' . "\n"; print '</html>' . "\n"; ?> ======================== I hope you can help me out getting my headers straight. Regards, Børge |
|
|||
|
On Sat, 10 Jan 2004 17:44:18 +0100 (MET), you wrote:
>I have a problem setting my headers right with php running as .cgi. I >have to specify a Content-type for the cgi file to work. But how >should I do both that and start a session? > >I have a few options: > >#! /usr/local/bin/php ><?php > session_start(); > print 'Content-type: text/html' . "\n\n"; > >This way $_SESSION['count'] stays unset even though I say >$_SESSION['count'] = 1; in my program. > >To swap the two lines won't work because I heard \n\n terminates the >header portion. Anyway, here goes: Stupid question: Have you tried header ('Content-type: text/html'); I'm pretty sure it will work. |
|
|||
|
--- Børge Strand <borges@ifi.uio.no> wrote:
> I have a problem setting my headers right with php running as > .cgi. I have to specify a Content-type for the cgi file to work. > But how should I do both that and start a session? Try using header() instead of print to set Content-Type. Hope that helps. Chris ===== Chris Shiflett - http://shiflett.org/ PHP Security Handbook Coming mid-2004 HTTP Developer's Handbook http://httphandbook.org/ |
|
|||
|
> >I have a problem setting my headers right with php running as .cgi. I > >have to specify a Content-type for the cgi file to work. But how > >should I do both that and start a session? > > > >I have a few options: > > > >#! /usr/local/bin/php > ><?php > > session_start(); > > print 'Content-type: text/html' . "\n\n"; > > > >This way $_SESSION['count'] stays unset even though I say > >$_SESSION['count'] = 1; in my program. > > > >To swap the two lines won't work because I heard \n\n terminates the > >header portion. Anyway, here goes: > > Stupid question: Have you tried Well, there's one thing I have found out here and that is that there's no such thing as a stupid question. I've tried searching for Content-type and session_start without getting any wiser :-( > header ('Content-type: text/html'); > > I'm pretty sure it will work. Tried it, didn't word I'm afraid. The text inside doesn't contain anything that's treated differently by " and '. -- Børge |
|
|||
|
BøRge Strand wrote:
> Hi there, > > I have a problem setting my headers right with php running as .cgi. I > have to specify a Content-type for the cgi file to work. But how > should I do both that and start a session? > > I have a few options: > > #! /usr/local/bin/php > <?php > session_start(); > print 'Content-type: text/html' . "\n\n"; > > This way $_SESSION['count'] stays unset even though I say > $_SESSION['count'] = 1; in my program. > > To swap the two lines won't work because I heard \n\n terminates the > header portion. Anyway, here goes: > > #! /usr/local/bin/php > <?php > print 'Content-type: text/html' . "\n\n"; > session_start(); > > Now I get "Warning: session_start(): Cannot send session cookie - > headers already sent by... line 3" and then "Warning: session_start(): > Cannot send session cache limiter - headers already sent...". > > I have tried replacing print 'Content-type: text/html' . "\n\n"; by > print 'Content-type: text/html' . "\n"; (one \n instead of two) with > exactly the same result. With no \n at all I only get "Warning: > session_start(): Cannot send session cache limiter - headers already > sent". > > The next thing I try is to replace print 'Content-type: text/html' and > the \n's by sending the same text in header(). This variety gives me a > "500 Internal Server Error" independant on the number of \n's. > > Anyway, here's all my code. No matter what I do it always prints "Your > visit number 1". $_SESSION['count'] doesn't get increased at all! > > ======================== > #! /usr/local/bin/php > <?php > print 'Content-type: text/html' . "\n\n"; > session_start(); > print '<html>' . "\n"; > print '<body>' . "\n"; > > if (!isset($_SESSION['count'])) > { > $_SESSION['count'] = 1; > } > else > { > $_SESSION['count']++; > } > > print 'Your visit number ' . $_SESSION['count'] . "\n"; > print '</body>' . "\n"; > print '</html>' . "\n"; > ?> > ======================== > > I hope you can help me out getting my headers straight. > > > Regards, > > Børge You should be using header as such: header('Content-type: text/html'); You don't need and should not include the newlines. The reason that other things aren't set is that two newlines ends headers (includeing cookies and such) which breaks sessions. If you simply use header() all of this is dealt with for you as it should be. Just make sure that you don't print or echo anything before using it (or starting a session). -- paperCrane <Justin Patrin> |