Our blog

 

Easy Security for PHP Scripts

If you have a script, admin area or whatever that you would like to make a bit more secure, you can use the following chunk of code to do this. If you don't have SSL (HTTPS) set up then you would need to get this sorted first.

This isn't bullet proof protection, but it helps.

PHP:
  1. //IP addresses that you would like to be able to access the system
  2. $allowed_ips[] = '99.99.99.01';
  3. $allowed_ips[] = '99.99.99.02';
  4. $allowed_ips[] = '99.99.99.03';
  5. if(!in_array($_SERVER['REMOTE_ADDR'], $allowed_ips)){
  6.     header('HTTP/1.1 500 Internal Server Error');
  7.     exit();
  8. }
  9.  
  10. //Force SSL Usage
  11. if($_SERVER['SERVER_PORT'] != 443){ //assuming your server is running SSL on port 443
  12.     $url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  13.     header('Location: '.$url);
  14. }

More Reading:

 

Leave a Reply