Categories

Protect a script with a required parameter in PHP

by adesignguy Posted on 01/02/2016 20:40:07 | Category: Code

Have you ever needed to protect a specific file with a required GET parameter, such as a script that runs via a scheduled task/cron job that checks for any database changes, updating prices and quantity of stock etc. The usage is ideal for any cronjob task.

It's easy, just add this code at the very top of your PHP file.

<?php

$param = $_GET['secret'];
if(empty($param) || !isset($param) || $param != "secretkey") {
  header("HTTP/1.0 404 Not Found"); //if not set trigger 404 error page
}

?>

Usage: hxxp://www.yourdomain.com/yourscript.php?secret=secretkey

Of course, make sure you change the secret key and/or the actual parameter.