Sinisterly
Tutorial [PHP] How To Block A Certain Person Accessing Your Page! - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: PHP (https://sinister.ly/Forum-PHP)
+--- Thread: Tutorial [PHP] How To Block A Certain Person Accessing Your Page! (/Thread-Tutorial-PHP-How-To-Block-A-Certain-Person-Accessing-Your-Page)



[PHP] How To Block A Certain Person Accessing Your Page! - BreShiE - 02-02-2013

This tutorial is to show you how to either block a certain IP Address from accessing a specific page on your website, or how to restrict a page for your eyes only.

Okay, so let's get started!

First you're going to need the basic starting tags of PHP:
Code:
<?PHP

?>

These are the start tags of PHP and are obviously needed for every PHP script. Right, so now we're going to have to create a variable to store the viewers IP! You can do this as follows:

Code:
$userip = $_SERVER['REMOTE_ADDR'];

What this piece of code does, is grabs the user/viewers IP and stores it in the "userip" variable for use when we want to check and deny or allow the IP Address. Now, we're going to have to create an if statement, asking the document: if it's this certain IP Address, then don't allow that user on! We can do this as follows:

If you want to block access to a certain IP:
Code:
if ($userip == "Target IP")
{
die();
}

If you want to ONLY allow one IP:
Code:
if ($userip != "Target IP")
{
die();
}

This is everything you need to create a secured page! Whether you use this to lock a page to your IP, or to block a certain person viewing the page is up to you, but I hope this helps! If you're too lazy to put this together, an example of a full page will be in the spoiler at the bottom. If you need any help with this, then please post here and ask. I don't bite. Also, obviously replace "Target IP" with the IP you want to restrict access/allow on.

Example:
Code:
<?PHP
if ($userip=="Target IP")
{
    die();
}

echo "Example document for blocking a user IP!"; // Just showing you that you can include content after the statement above

?>



RE: [PHP] How To Block A Certain Person Accessing Your Page! - Phytrix - 02-02-2013

Thanks for the tutorial. Smile


RE: [PHP] How To Block A Certain Person Accessing Your Page! - BreShiE - 02-02-2013

No worries Phytrx, I hope it helps you, and others out!


RE: [PHP] How To Block A Certain Person Accessing Your Page! - KyleFYI - 02-21-2013

Bit of a better script using arrays.
PHP Code:
<?PHP
    $ip 
$_SERVER['REMOTE_ADDR'];
    
$ips = array("1.1.1.1""3.3.3.3");
    if (
in_array($ip$ips)) {
        die(
"Error");
    }
?>



RE: [PHP] How To Block A Certain Person Accessing Your Page! - BreShiE - 02-21-2013

Well, that's if you want to use multiple IP's...

This tutorial is for 1 IP only.


RE: [PHP] How To Block A Certain Person Accessing Your Page! - KyleFYI - 02-21-2013

(02-21-2013, 03:11 AM)BreShiE Wrote: Well, that's if you want to use multiple IP's...

This tutorial is for 1 IP only.

Just a suggestion for people who need it Wink Its also a good example of arrays in PHP.