Jump to content

[SOLVED] Help with IP Ban (simple)


env3rt

Recommended Posts

I want to make a simple IP ban with an IP ban list which can be added on to using append.

 

It is not working because $IP has to be equal to whatever is in the testFile.php, however I want the testFile.php text to be treated as code so it will work like this

 

if (

($IP == "77.77.777.7" OR $IP == "99.99.999.9")

)

 

etc..

 

<?php
session_start();

$IP = $_SERVER['REMOTE_ADDR'];

$myFile = "testFile.php";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);

if (
($IP == $theData)
)
{
echo "You have been banned.";
}
else
{
//do stuff
}
?>

inside testFile.php is

"77.77.777.7" OR $IP == "99.99.999.9" 

 

Please help :)

Link to comment
Share on other sites

I just tested it, works perfectly.

 

<?php

$lines = file('ips.txt');

foreach ($lines as $line) {
    $ips[] = $line;
}

$ip = "55.55.555.5";

if (in_array($ip, $ips)){
   echo "You are banned!";
} else {
   echo "You are not banned.";
}

?>

 

I put an else statement in there to help you out.

Link to comment
Share on other sites

Make a table in your database of banned IPs.  When someone tries to access your site, try to look up their IP in the table.  If the number of rows returned is zero, allow access.

I'm really a newbie at this I've never used tables, does that involve PHPmyadmin or whatever? Because I'm just uploading php files from my computer.

 

Could you tell me how to do it :D

Link to comment
Share on other sites

I'm just testing an IP ban right now.

 

New code:

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$lines = file('lol.txt');
if(in_array($ip, $lines)) {
   echo "You are banned!";
} else {
   echo "You are not banned.";
}
?>

 

The only problem is when you do multiple lines of ips in the text files it stops working.

Link to comment
Share on other sites

<?php

foreach ($lines as $line) {
    $ips[] = $line;
}

?>

 

EDIT - Thorpe beat me to it.

 

The quoted loop seems unnecessary.  Why not just do this?

 

<?php
if(in_array($ip, $lines)
?>

 

Your right! Sorry about that.

 

<?php

$ips = file('ips.txt');
$ip = "55.55.555.5";

if (in_array($ip, $ips)){
   echo "You are banned!";
} else {
   echo 'Not Banned';
}
   
?>

 

I just tested that code with multiple lines in the text file, it worked just fine.

 

Edit - Thorpe beat me to it.

Link to comment
Share on other sites

$ip = "55.55.555.5"

I want the ip to be different for each person... If you say the ip is 55.55.555.5 every single time everyone would be banned, if you banned that ip.

 

Try using $ip = $_SERVER['REMOTE_ADDR']; it won't work.

 

What? $_SERVER['REMOTE_ADDRESS'] gets the ip of the current client. Though this can also be tricked, but thats a whole other issue.

Link to comment
Share on other sites

If you use $ip = $_SERVER['REMOTE_ADDR']; and write multiple IPs in the .txt it won't work. But if you write $ip = "55.55.555.5"; it will work.

 

I'm hosting it on a free php hoster :) but I'm just uploading php files not using mySQL or anything

 

I want $ip = $_SERVER['REMOTE_ADDR']; to work with multiple lines of ips.

Link to comment
Share on other sites

Yes.. And I found out something new, it works with multiple lines but only if the ip you want to work is at the bottom of the list example:

 

Doesnt work

 

45.45.455.5

88.88.888.8 <--- ip u want to work

54.45.455.4

45.54.455.4

 

Works

 

45.45.455.5

88.88.888.8 <--- ip u want to work

 

but I want all the ips to work.

 

Is it working for anyone else?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.