473,790 Members | 2,617 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP ban script

Hello

I came across a php script on the net that will compare a web surfers IP
address to those in a text file. If a match or partial match is found, the
user is banned from accessing the php script, but if no match is found, the
php script executes.

The original script is as follows:

line 1 <?php
line 2 $IP = $_SERVER['REMOTE_ADDR'];
line 3 $IP_array = file('../folder/IP.dat');
line 4 $IP_array = str_replace("\n ", "", $IP_array);
line 5 $IP_array = str_replace("\r ", "", $IP_array);
line 6 foreach ($IP_array as $IP_test) {
line 7 if (substr_count($ IP, $IP_test) != "0") {
line 8
line 9 echo 'Banned World!';
line 10
line 11 die();
line 12 }
line 13 }
line 14
line 15 echo 'Hello World!';
line 16
line 17 ?>

I currently have not one text file with banned addresses, but five different
one (eg IP.dat, IP1.dat, IP2.dat, IP3.dat, IP4.dat) and was wondering if
there was an easy way of changing the code so that it efficiently checks the
surfers IP address to all the IPx.dat files to see if it is banned? I could
simply replicate the code 5 times to check each file, but if there was a way
of just looping through the various files, that would be great.

I know I could merge the 5 IPx.dat files into a single one and not have to
worry about this, but for management purposes of the bans, it works a lot
better for me having the 5 distinct files.

Thank you
May 8 '06 #1
6 5592
On Mon, 08 May 2006 05:33:41 GMT, "carmen" <dd**@kkdi.ck a> wrote:
Hello

I came across a php script on the net that will compare a web surfers IP
address to those in a text file. If a match or partial match is found, the
user is banned from accessing the php script, but if no match is found, the
php script executes.
For starters, this is something that would be much more efficient if
it were using a database as opposed to a group of text files. I
mention this primarily because you do seem to be concerned about
efficiency.
The original script is as follows:

line 1 <?php
line 2 $IP = $_SERVER['REMOTE_ADDR'];
line 3 $IP_array = file('../folder/IP.dat');
line 4 $IP_array = str_replace("\n ", "", $IP_array);
line 5 $IP_array = str_replace("\r ", "", $IP_array);
line 6 foreach ($IP_array as $IP_test) {
line 7 if (substr_count($ IP, $IP_test) != "0") {
line 8
line 9 echo 'Banned World!';
line 10
line 11 die();
line 12 }
line 13 }
line 14
line 15 echo 'Hello World!';
line 16
line 17 ?>

I currently have not one text file with banned addresses, but five different
one (eg IP.dat, IP1.dat, IP2.dat, IP3.dat, IP4.dat) and was wondering if
there was an easy way of changing the code so that it efficiently checks the
surfers IP address to all the IPx.dat files to see if it is banned? I could
simply replicate the code 5 times to check each file, but if there was a way
of just looping through the various files, that would be great.
<?php
for ($i = 1; $i <= 5; $i++) {
$ipArray = file('../folder/IP' . $i . '.dat');
$ipArray = preg_replace("/[\r\n]/", '', $ipArray);
if (in_array($_SER VER['REMOTE_ADDR'], $ipArray)) {
die('Banned');
}
}
echo 'Hello, world!';
?>
I know I could merge the 5 IPx.dat files into a single one and not have to
worry about this, but for management purposes of the bans, it works a lot
better for me having the 5 distinct files.


I would still suggest looking into a database solution if you can,
especially if you're getting decent traffic. Hitting 5 extra files per
pageload is not at all efficient compared to sending a query to a
database that caches things to RAM.

hth

-
Remove mypants to email.
<http://www.shaunc.com/>
May 8 '06 #2
Message-ID: <fl************ *************** *****@4ax.com> from Shaun
contained the following:
I came across a php script on the net that will compare a web surfers IP
address to those in a text file. If a match or partial match is found, the
user is banned from accessing the php script, but if no match is found, the
php script executes.


For starters, this is something that would be much more efficient if
it were using a database as opposed to a group of text files. I
mention this primarily because you do seem to be concerned about
efficiency.


For seconds, it is assuming that IP is a unique identifier. It isn't.

--
Geoff Berrow 011000100110110 0010000000110
001101101011011 001000110111101 100111001011
100110001101101 111001011100111 010101101011
May 8 '06 #3
Thank you for your help, this worked great.

You are correct about the database method being faster, but I'm looking
forward to seeing what kind of speed this script will run at as I do want to
apply it to all my websites and if I can avoid having to setup databases, it
will allow faster implementation. If not, I'll go the database route.

Thanks again.

"Shaun" <sh***@mypants. drunkwerks.com> wrote in message
news:fl******** *************** *********@4ax.c om...
On Mon, 08 May 2006 05:33:41 GMT, "carmen" <dd**@kkdi.ck a> wrote:
Hello

I came across a php script on the net that will compare a web surfers IP
address to those in a text file. If a match or partial match is found,
the
user is banned from accessing the php script, but if no match is found,
the
php script executes.


For starters, this is something that would be much more efficient if
it were using a database as opposed to a group of text files. I
mention this primarily because you do seem to be concerned about
efficiency.
The original script is as follows:

line 1 <?php
line 2 $IP = $_SERVER['REMOTE_ADDR'];
line 3 $IP_array = file('../folder/IP.dat');
line 4 $IP_array = str_replace("\n ", "", $IP_array);
line 5 $IP_array = str_replace("\r ", "", $IP_array);
line 6 foreach ($IP_array as $IP_test) {
line 7 if (substr_count($ IP, $IP_test) != "0") {
line 8
line 9 echo 'Banned World!';
line 10
line 11 die();
line 12 }
line 13 }
line 14
line 15 echo 'Hello World!';
line 16
line 17 ?>

I currently have not one text file with banned addresses, but five
different
one (eg IP.dat, IP1.dat, IP2.dat, IP3.dat, IP4.dat) and was wondering if
there was an easy way of changing the code so that it efficiently checks
the
surfers IP address to all the IPx.dat files to see if it is banned? I
could
simply replicate the code 5 times to check each file, but if there was a
way
of just looping through the various files, that would be great.


<?php
for ($i = 1; $i <= 5; $i++) {
$ipArray = file('../folder/IP' . $i . '.dat');
$ipArray = preg_replace("/[\r\n]/", '', $ipArray);
if (in_array($_SER VER['REMOTE_ADDR'], $ipArray)) {
die('Banned');
}
}
echo 'Hello, world!';
?>
I know I could merge the 5 IPx.dat files into a single one and not have to
worry about this, but for management purposes of the bans, it works a lot
better for me having the 5 distinct files.


I would still suggest looking into a database solution if you can,
especially if you're getting decent traffic. Hitting 5 extra files per
pageload is not at all efficient compared to sending a query to a
database that caches things to RAM.

hth

-
Remove mypants to email.
<http://www.shaunc.com/>

May 8 '06 #4
Hello Shaun

Could you confirm, does the code you provided ban based on partial IP
addresses similar to the original script? I did try it a couple of times to
see if i could get it to ban based on a partial match of my IP address, but
no luck.

For example, if ip1.dat had 1.2.3 as one ip address, it would ban anyone
coming to the site with an ip of 1.2.3.4, 1.2.3.5, 1.2.3.6, etc, or if
ip1.dat had 1.2, it would ban 1.2.3.4, 1.2.5.6, etc

thx
"Shaun" <sh***@mypants. drunkwerks.com> wrote in message
news:fl******** *************** *********@4ax.c om...
On Mon, 08 May 2006 05:33:41 GMT, "carmen" <dd**@kkdi.ck a> wrote:
Hello

I came across a php script on the net that will compare a web surfers IP
address to those in a text file. If a match or partial match is found,
the
user is banned from accessing the php script, but if no match is found,
the
php script executes.


For starters, this is something that would be much more efficient if
it were using a database as opposed to a group of text files. I
mention this primarily because you do seem to be concerned about
efficiency.
The original script is as follows:

line 1 <?php
line 2 $IP = $_SERVER['REMOTE_ADDR'];
line 3 $IP_array = file('../folder/IP.dat');
line 4 $IP_array = str_replace("\n ", "", $IP_array);
line 5 $IP_array = str_replace("\r ", "", $IP_array);
line 6 foreach ($IP_array as $IP_test) {
line 7 if (substr_count($ IP, $IP_test) != "0") {
line 8
line 9 echo 'Banned World!';
line 10
line 11 die();
line 12 }
line 13 }
line 14
line 15 echo 'Hello World!';
line 16
line 17 ?>

I currently have not one text file with banned addresses, but five
different
one (eg IP.dat, IP1.dat, IP2.dat, IP3.dat, IP4.dat) and was wondering if
there was an easy way of changing the code so that it efficiently checks
the
surfers IP address to all the IPx.dat files to see if it is banned? I
could
simply replicate the code 5 times to check each file, but if there was a
way
of just looping through the various files, that would be great.


<?php
for ($i = 1; $i <= 5; $i++) {
$ipArray = file('../folder/IP' . $i . '.dat');
$ipArray = preg_replace("/[\r\n]/", '', $ipArray);
if (in_array($_SER VER['REMOTE_ADDR'], $ipArray)) {
die('Banned');
}
}
echo 'Hello, world!';
?>
I know I could merge the 5 IPx.dat files into a single one and not have to
worry about this, but for management purposes of the bans, it works a lot
better for me having the 5 distinct files.


I would still suggest looking into a database solution if you can,
especially if you're getting decent traffic. Hitting 5 extra files per
pageload is not at all efficient compared to sending a query to a
database that caches things to RAM.

hth

-
Remove mypants to email.
<http://www.shaunc.com/>

May 8 '06 #5
carmen wrote:
Hello

I came across a php script on the net that will compare a web surfers IP
address to those in a text file. If a match or partial match is found, the
user is banned from accessing the php script, but if no match is found, the
php script executes.


And if they come from AOL, via a proxy you *could* be banning thousands
of users. Since they're using AOL it's probably not an issue, but if
you're selling penis enlargement/fat busting pills then you could be
loosing the majority of your business.

May 8 '06 #6
And if they come from AOL, via a proxy you *could* be banning
thousands
of users. Since they're using AOL it's probably not an issue, but if
you're selling penis enlargement/fat busting pills then you could be
loosing the majority of your business.


LOL. Also, if I disconnect from my provider and a friend logs in at
the same provider, the IP might be my old one. So, you should make IPs
invalid after a time of, say 1 hour or so...
May 8 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
12987
by: Mike Daniel | last post by:
I am attempting to use document.write(pageVar) that displays a new html page within a pop-up window and the popup is failing. Also note that pageVar is a complete HTML page containing other java scripts. Being a javascript newbie and after significant testing, I suspect that the document.write fails after finding a </script> within pageVar. Does a trick exist that enables one to slightly alter pageVar whereby enabling...
1
3256
by: bayouprophet | last post by:
Cant get menu script to to put linked page in the same frame. I am new to Java and I am wondering what am I doing wrong? below are my java applet file, frame.html file, and my text file and one of my link file that should load next to the menu on the same page. And Thank You in advance. Here is my menu applet: <html>
1
4819
by: Allen | last post by:
I am trying to add an additional photo/hyperlink to the company web site (I didn't create it) without any luck. The mouseover feature 'highlights' pics by swapping them with another pic using this command in some type of array. I added the mailbox in the lower left corner (see link below)http://www.aamechanical.com/indextemp.htm but I cannot get it to swap. Here is the code for the page: Thanks for your help in advance
3
2960
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None of the script gets executed. Can someone please give me a direction as to what I may be missing? Thanks.
2
2972
by: bilaribilari | last post by:
Hi all, I am using Tidy (C) for parsing html pages. I encountered a page that has some script as follows: <script> .... var abc = "<script>some stuff here</" + "script>"; .... </script>
19
3827
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
3
2491
by: rsteph | last post by:
I have a script that shows the time and date. It's been working on my site for quite a while now. Suddenly it stops showing up, after getting my drop down menu to work. If I put text between the <div id="datetime"></div> tags, the text shows up, but the javascript output being sent to those some divs doesn't seem to want to work. It should be showing up in the middle of the content pane, just below the first table box. (set by .css) Here's...
3
3687
by: Angus | last post by:
I have a web page with a toolbar containing a Save button. The Save button can change contextually to be a Search button in some cases. Hence the button name searchsavechanges. The snippet of html is: <a class="searchsavechanges btn btn3d tbbtn" href="javascript:" style="position:static"> <div id="TBsearchsavechanges">Search</div> </a>
7
3617
by: imtmub | last post by:
I have a page, Head tag Contains many Scripts and style sheet for Menu and Page. This code working fine and displaying menus and page as i wanted. Check this page for reference. http://www.marco.com.cn/web-content/marco_re10.html -------------------------------------------------------------- <head> <!-- InstanceBeginEditable name="doctitle" --> <title>Marco</title> <!-- InstanceEndEditable -->
1
47490
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10412
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.