Buax
04-03-2008, 10:53 PM
I know it's basic, but it's useful for new site owners or people that want to ban people! This is great if you are getting abuse/spam though a contact-box.
This is where you'll list all of the IP's you'd like banned. Please do not ban these IP addresses, they are you're own in case you didn't know. We are separating the IP addresses using a '|' symbol. We'll be splitting them up using this symbol in a minute!
<?php
// The ip addresses to be banned separated by |
$ban = "127.0.0.1|127.0.0.2|127.0.0.3");
?>
This part of the script will store the current users IP address in a varible to save coding space. $visitor will now be the users IP.
<?php
// Store current user's IP in a variable
$visitor = $_SERVER['REMOTE_ADDR'];
?>
This part of the script will split up the IP addresses using the '|' symbol
<?php
// Split the IPs using |
$list = explode("|", $ban);
?>
The final part of the script will check the current IP address of the user ($visitor) with the list of IP's which have just been split up.
<?php
foreach($list as $ip)
{
if($visitor == $ip)
{
echo "You have been banned from this website!";
exit;
}
}
?>
If you are too lazy to put it all together. It will look like this.
<?php
// The ip addresses to be banned separated by |
$ban = "34.43.97.31|86.53.73.27|41.107.14.621");
// Store current user's IP in a variable
$visitor = $_SERVER['REMOTE_ADDR'];
// Split the IPs using |
$list = explode("|", $ban);
foreach($list as $ip)
{
if($visitor == $ip)
{
echo "You are banned from this site";
exit;
}
}
?>
Now that is quite a bit of code to put on every single page. If you use PHP includes, we can cut it down to three lines, although you have to create a PHP file on you're website.
<?php
include ('banned_users.php');
?>
This is where you'll list all of the IP's you'd like banned. Please do not ban these IP addresses, they are you're own in case you didn't know. We are separating the IP addresses using a '|' symbol. We'll be splitting them up using this symbol in a minute!
<?php
// The ip addresses to be banned separated by |
$ban = "127.0.0.1|127.0.0.2|127.0.0.3");
?>
This part of the script will store the current users IP address in a varible to save coding space. $visitor will now be the users IP.
<?php
// Store current user's IP in a variable
$visitor = $_SERVER['REMOTE_ADDR'];
?>
This part of the script will split up the IP addresses using the '|' symbol
<?php
// Split the IPs using |
$list = explode("|", $ban);
?>
The final part of the script will check the current IP address of the user ($visitor) with the list of IP's which have just been split up.
<?php
foreach($list as $ip)
{
if($visitor == $ip)
{
echo "You have been banned from this website!";
exit;
}
}
?>
If you are too lazy to put it all together. It will look like this.
<?php
// The ip addresses to be banned separated by |
$ban = "34.43.97.31|86.53.73.27|41.107.14.621");
// Store current user's IP in a variable
$visitor = $_SERVER['REMOTE_ADDR'];
// Split the IPs using |
$list = explode("|", $ban);
foreach($list as $ip)
{
if($visitor == $ip)
{
echo "You are banned from this site";
exit;
}
}
?>
Now that is quite a bit of code to put on every single page. If you use PHP includes, we can cut it down to three lines, although you have to create a PHP file on you're website.
<?php
include ('banned_users.php');
?>