Search code examples
phpwampserverhttp-status-code-403

Make a webpage inaccessible to all but my PHP script


How would I be able to make a webpage return HTTP 403 to anyone who tries to access it, apart from my PHP page which fetches data from it? If it helps at all, I'm running WAMP server on localhost.


Solution

  • The .htaccess method mentioned by Yada is valid. Another approach would be to do this in your PHP script itself. If it's a cronjob running through CLI:

    if (!empty($_SERVER['REMOTE_ADDR'])) {
        // If a "remote" address is set, we know that this is not a CLI call
        header('HTTP/1.1 403 Forbidden');
        die('Access denied. Go away, shoo!');
    }
    

    Or if it's triggered by a browser request from the other PHP script, just verify if the IP is yours/local:

    if ($_SERVER['REMOTE_ADDR'] != '192.168.1.5') { // Or whatever your local IP is
        header('HTTP/1.1 403 Forbidden');
        die('Get out and stay out!');
    }