Authorize view by IP (Simple Script)

This is like THE simplest, easiest way to make a certain web page only viewable by certain IP[s].

<?php
/*****************************
* Fetch IP
* You can also use:
* $ip = $_SERVER["REMOTE_ADDR"];
* Instead of:
* $ip = getnv(”REMOTE_ADDR”);
******************************/

$ip = getenv(”REMOTE_ADDR”);

// Add authorised IPs here
// example: $myip[0] = “127.0.0.1″;

$myip[0] = “IP_HERE”;
$myip[1] = “IP_HERE”;

/************************************************
* If you want to add more IPs, just copy the $myip line
* and change the IP and [0] accordingly
* [0] = first IP, [1] = second, [2] = thrid, and so on.
*
* Also if you are going to add another IP, don’t forget to
* edit the if line, for example, if you added a third IP
*
* this:
* if ($ip == $myip[0] || $ip == $myip[1])
*
* should be:
* if ($ip == $myip[0] || $ip == $myip[1] || $ip == $myip[2])
**************************************************/

if ($ip == $myip[0] || $ip == $myip[1]) {
?>

Your Site Content

<?php
}
else {
echo “Your IP is not authorized to view this page”;
}
?>

Of course there are many other ways in doing this, like using array(”ip1″, “ip2″), but this is the easiest one to understand for people who don’t know much about PHP.

This can basically be used for paranoid security, htpasswd + that script will make your page very secure.

Enjoy.

Article by el_jentel1

9 Responses to “Authorize view by IP (Simple Script)”

  1. Blake Kendrick Says:

    That scripts easy to bypass. It would block newbies, but not people with experience.

  2. Sander Says:

    Why don’t you use in_array in the if?

  3. el_jentel1 Says:

    If you read my comment after the script, you’d see that its not the “only” way, its the simplest way for people that don’t know much about PHP, if you want to comment with a better one, by all means go ahead, I’m sure some will be interested in a “Simple” and an “Advanced” version.

  4. WilliamF Says:

    Seria mais facil usar o array com foreach :/

  5. el_jentel1 Says:

    Anglish please.

  6. lol... Says:

  7. lol... Says:

    Code not showing up ^ - here it is again.

    $allowedips = array(’127.0.0.1′, ‘192.168.1.1′);
    if (!in_array($_SERVER['REMOTE_ADDR'], $allowedips)) die(’Access Denied’);

  8. Php For Foreach Says:

    Good site I “Stumbledupon” it today and gave it a stumble for you.. looking forward to seeing what else you have..later

  9. Amoxicillin 500mg. Says:

    Amoxicillin dosage….

    Amoxicillin for acne. Amoxicillin price. Amoxicillin alternative. Amoxicillin dosage for greyhound sinus infection….

Leave a Reply