views:

840

answers:

1

For some reason a call to header() causes an internal server error for me. I'm using PHP5 and use mod_rewrite extensively in this script (if that helps). Here is the code (sort of):

<?php 

include 'core/initialize.php'; // Loads the core class (and session manager class)

    if($_GET['reset'] == 'true')
    {
         $core->Session->Visits = 0;
         header('Location', 'index.html');
         # header('X-Test', 'wtf'); // causes the error too :(
    }
    if(isset($core->Session->Visits)) $core->Session->Vists += 1;
    else $core->Session->Visits = 0;
    echo "Previous Visits: {$core->Session->Visits} (<a href='index.html?reset=true'>Reset</a>)";
?>

My .htaccess file looks like this:

# Start up the rewrite engine
Options +FollowSymLinks
RewriteEngine on

RewriteRule  ^(.*)$ navigator.php?nav=$1&%{QUERY_STRING} [NC]
+4  A: 

You are using this :

header('Location', 'index.html');

It is not the way header must be used : the second parameter should be a boolean.

ANd the first one should be the header name + value.

So, in your case, something like this :

header('Location: index.html');

Only one parameter ; and name + ':' + value :-)

There is an example in the documentation :

The second special case is the "Location:" header. Not only does it send this header back to the browser, but it also returns a REDIRECT (302) status code to the browser unless some 3xx status code has already been set.

<?php
header("Location: http://www.example.com/"); /* Redirect browser */

/* Make sure that code below does not get executed when we redirect. */
exit;
?>


As a sidenote, if I remember correctly, you should use a FULL absolute URL when using the Location header ; I know using relative URL works in (almost ?) all browsers, but it is not valid, when reading the HTTP RFC, if I recall correctly.


As a second sidenote (yes, the answer has already been accepted, but maybe it'll be useful anyway, for next time :-) ) : that "INternal Server Error" might indicate your PHP script encountered some kind of error.

In that case, you should check the error_log's file...
...Or activate error_reporting and display_errors to facilitate things -- at least on your development machine.

Pascal MARTIN
I feel pretty dumb right now. Thanks
Chris T
@Chris T — Don't worry too much about it. Sooner or later, all good programmers discover that the price of seeing the obscure is to sometimes miss the obvious. ;-)
Ben Blank
@Chris : don't worry about it ; I know having someone else take a look at our code can really help, sometimes :-) (btw, I edited my answer after you accepted it, to give another information about error_log)
Pascal MARTIN