Jump to content

$var = any text file lines


TEENFRONT

Recommended Posts

Hey

 

Im wanting to build a banned IP list to stop abusive users accessing my site, i know this will only stop some people, but it will do for now lol.

 

I have a text file called banned.txt which is set out simply like this

 

1.2.3.4

5.6.3.4

1.2.3.4

5.6.3.4

 

Just with a banned ip on each new line...

 

and i want to put each of the lines of the text file into one global var so that $banned  could equal any of the lines from the text file. So i can use

 

if($banned == $user_ip)

 

Im not sure how i go about setting $banned to cover each of the text lines, any help?

 

Very much appreciated..

Link to comment
https://forums.phpfreaks.com/topic/110209-var-any-text-file-lines/
Share on other sites

banning users on the script level may keep them from getting to see your content/use your services, but every time they try to access it, bandwidth and processing resources are used.  They could still be free to for instance make a bot to stage a dns attack.

Hey

 

Thanks thebadbad, will give that a go now.

 

I'll use this to block access to our online game, i still want to show the page, but in place of the game if there banned via ip, then show a banned message. i dont intend to use it to 100% block page access, just certain aspects of the page.

Hey

 

Didnt quite do what i wanted. Hopefully someone can help me further

 

<?php
$user_ip = $_SERVER['REMOTE_ADDR'];
$banned = file('/home/baller8/public_html/codetests/banned.txt');
foreach ($banned as $ip) {
if (trim($ip) == $user_ip) {
	$userban = "YES";
} else { $userban = "NO"; }
}
if($userban == "YES") { echo "Your banned"; }
if($userban == "NO")  { echo "Wooo Your not banned"; }
?>

 

my text file is set up like this

 

1.2.3.4

5.6.7.8

9.10.11.12

 

etc..

 

So the above code applied to that text file will overwrite $userban  everytime. so it will check line 1 which is 1.2.3.4  if its not my ip it will set $userban to NO. but then on line to 5.6.7.8 is my ip, so it sets $userban to YES, but then it continues to loop to the 3rd line and it isnt my ip so $userban is again set to NO.

 

How, once an ip is found in the banned.txt can i stop the loop and assign a var to say this user is banned, so i simply end up with $userban YES or NO.

 

Many thanks :-)

To stop the loop if a match is made, add break; after $userban = "YES";

 

Also Change

if($userban == "YES") { echo "Your banned"; }
if($userban == "NO")  { echo "Wooo Your not banned"; }

to

if($userban == "YES")
{
    echo "Your banned";
}
else
{
    echo "Wooo Your not banned";
}

I would like to add that using that method with a large IP ban list can be slow.

 

I would assume something like this would be faster:

 

<?php
$banned = file_get_contents("ipbans.txt");
if(strstr($banned, $_SERVER['REMOTE_ADDR']) !== false) {
    // user is banned
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.