[PHP] How To Block A Certain Person Accessing Your Page! 02-02-2013, 10:13 PM
#1
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:
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:
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:
If you want to ONLY allow one IP:
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:
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
?>