Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP header() causes internal server error [duplicate]

Tags:

php

header

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]  
like image 666
Christopher Tarquini Avatar asked Jan 24 '23 09:01

Christopher Tarquini


1 Answers

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.

like image 82
Pascal MARTIN Avatar answered Jan 25 '23 23:01

Pascal MARTIN