Headers and sessions in php .cgi

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-10-2004
BøRge Strand
 
Posts: n/a
Default Headers and sessions in php .cgi


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
Reply With Quote
  #2 (permalink)  
Old 01-10-2004
David Otton
 
Posts: n/a
Default Re: [PHP] Headers and sessions in php .cgi

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.
Reply With Quote
  #3 (permalink)  
Old 01-10-2004
Chris Shiflett
 
Posts: n/a
Default Re: [PHP] Headers and sessions in php .cgi

--- 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/
Reply With Quote
  #4 (permalink)  
Old 01-10-2004
BøRge Strand
 
Posts: n/a
Default Re: [PHP] Headers and sessions in php .cgi


> >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
Reply With Quote
  #5 (permalink)  
Old 01-10-2004
Justin Patrin
 
Posts: n/a
Default Re: Headers and sessions in php .cgi

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>
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:31 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0