views:

45

answers:

1

I have the following code in a PHP script:

<?php

if (!in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1')))
{
    die('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

// Other statements follow here
echo 'Hello there ...'

instead of echoing the message, I want to redirect the user to a 404 page. However, when I replace the die() statement with this one:

header("HTTP/1.0 404 Not Found");

A user running the script from a remote machine is able to see the message 'Hello there ...'

How can I get the page to redirect to 404 page for non-localhost requests for the script?

BTW, this snippet id from the dev environment front controller in the Symfony framework.

+6  A: 

It should be:

header("HTTP/1.0 404 Not Found");
exit();

You need to put the exit(); to avoid that.

Sarfraz
I'd also add: a `header` call alone won't stop the script execution, that's why an `exit` or `die` is neccesary.
Maerlyn