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>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to