Post: IP Ban [PHP]
11-08-2012, 08:09 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Well, I coded this very fast so don't judge. I'm not the best at php and I hate it lol.

    
<?php
// IP to ban
$banned_ip = "1.3.2.2";

$user_ip = $_SERVER['REMOTE_ADDR'];

if($user_ip == $banned_ip) {
echo "Access denied"; //Don't have to have that
header( 'Location: https://www.yoursite.com/banned.php' ) ;
}

$banned_ips = array("1.2.3.5", "1.2.3.4");

foreach($banned_ips as $ip_ban) {
if($user_ip == $ip_ban) {
echo "Access denied";//Don't have to have that
header( 'Location: https://www.yoursite.com/banned.php' ) ;
}
}
?>
(adsbygoogle = window.adsbygoogle || []).push({});
11-08-2012, 10:30 PM #2
Originally posted by TheUnexpected View Post
Well, I coded this very fast so don't judge. I'm not the best at php and I hate it lol.


Why would you do it if you hate it? :p

anyway I would connect to a database so you don't have to update the PHP file every so often. Also I would check for proxy servers as this is very easy to bypass this ban. I threw up a quick code below, so you can update it if you would like or just study. Smile

    
<?php

//get ip or ip behind proxy
if($_SERVER["HTTP_X_FORWARDED_FOR"] != "")
{
$IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
$proxy = $_SERVER["REMOTE_ADDR"];
$host = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]);
}
else
{
$IP = $_SERVER["REMOTE_ADDR"];
$proxy = "No proxy detected";
$host = @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
}

//connect to database (mysql pref.)
//database connect here

$all_bans = mysql_query("SELECT * WHERE 'ban' = 'true'"); //get all ip bans

$banned = false; //initialize ban to false

//check through all the bans to determine if banned.
foreach( $banned_ip in $all_bans )
if($banned_ip == $IP)
$banned = true;

if($banned)
echo "Banned. gtfo.";
else
echo "Continue on! (Not Banned)";

?>
11-08-2012, 10:44 PM #3
Originally posted by GAMER View Post
Why would you do it if you hate it? :p

anyway I would connect to a database so you don't have to update the PHP file every so often. Also I would check for proxy servers as this is very easy to bypass this ban. I threw up a quick code below, so you can update it if you would like or just study. Smile

    
<?php

//get ip or ip behind proxy
if($_SERVER["HTTP_X_FORWARDED_FOR"] != "")
{
$IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
$proxy = $_SERVER["REMOTE_ADDR"];
$host = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]);
}
else
{
$IP = $_SERVER["REMOTE_ADDR"];
$proxy = "No proxy detected";
$host = @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
}

//connect to database (mysql pref.)
//database connect here

$all_bans = mysql_query("SELECT * WHERE 'ban' = 'true'"); //get all ip bans

$banned = false; //initialize ban to false

//check through all the bans to determine if banned.
foreach( $banned_ip in $all_bans )
if($banned_ip == $IP)
$banned = true;

if($banned)
echo "Banned. gtfo.";
else
echo "Continue on! (Not Banned)";

?>


I just code some in php to learn a little bit just in case I need it in the future. :p

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo