Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Tutorial [PHP] How To Block A Certain Person Accessing Your Page! filter_list
Author
Message
[PHP] How To Block A Certain Person Accessing Your Page! #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:
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

?>
[Image: F4Z9Dqw.png]

Reply

RE: [PHP] How To Block A Certain Person Accessing Your Page! #2
Thanks for the tutorial. Smile
[Image: 7uhCgFS.jpg?1]

Reply

RE: [PHP] How To Block A Certain Person Accessing Your Page! #3
No worries Phytrx, I hope it helps you, and others out!
[Image: F4Z9Dqw.png]

Reply

RE: [PHP] How To Block A Certain Person Accessing Your Page! #4
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");
    }
?>

Reply

RE: [PHP] How To Block A Certain Person Accessing Your Page! #5
Well, that's if you want to use multiple IP's...

This tutorial is for 1 IP only.
[Image: F4Z9Dqw.png]

Reply

RE: [PHP] How To Block A Certain Person Accessing Your Page! #6
(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.

Reply







Users browsing this thread: 2 Guest(s)