PHP header () causes an internal server error

For some reason, calling header () causes an internal server error for me. I use PHP5 and make extensive use of mod_rewrite in this script (if that helps). Here is the code (view):

<?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
source share
1 answer

You use this:

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

This is not a header method: the second parameter must be a boolean.

ANd first must be the name of the header +.

So, in your case, something like this:

 header('Location: index.html'); 

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

The documentation has an example:

The second special case is the Heading "Location:". Not only does this send this header back to the browser, but it also returns a REDIRECT (302) status code for the browser, if only some 3xx status code is already 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 side element, if I remember correctly, you should use the full absolute URL when using the Location header; I know that using a relative URL works (almost?) In all browsers, but this is not true when reading HTTP RFC, if I remember correctly.


As the second one sits (yes, the answer has already been accepted, but perhaps it will be useful in any case, next time :-)): that the "Internal server error" may indicate that your PHP script error.

In this case, you should check the error_log file ...
... Or activate error_reporting and display_errors to make things easier - at least on your development machine.

+9
source

All Articles