Php code help.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Php code help.
# 1  
Old 09-05-2014
Php code help.

Hello again,

I'm trying to block ips using a php script (no .htaccess).

Code:
<?
if ($_SERVER['REMOTE_ADDR'] == "109.101.51.119") die();
?>

How can I make this to block the entire block -> 109.101.*.*

Found something while searching the web but the codes are too complex. I just wanna make it simple.

Also tried:

Code:
<?
if ($_SERVER['REMOTE_ADDR'] == "109.101.*.*") die();
?>

and it doesnt work.

Any tips? Thanks in advance.

Last edited by Scrutinizer; 09-05-2014 at 05:42 AM.. Reason: code tags
# 2  
Old 09-05-2014
Try:
Code:
if (preg_match('/^109\.101\.[^.]+\./', $_SERVER['REMOTE_ADDR'])) .....

EDIT: or simpler, try:
Code:
if (preg_match('/^109\.101\./', $_SERVER['REMOTE_ADDR'])) .....


Last edited by Scrutinizer; 09-05-2014 at 06:24 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 09-05-2014
Ok.. I will try it and come back with reply...

LE: working as a peach. Thanks.

Last edited by galford; 09-05-2014 at 06:15 AM..
# 4  
Old 09-05-2014
Another approach...
PHP Code:
$remoteip $_SERVER['REMOTE_ADDR'];
$bannedblock "109.101.";

if (
substr($remoteip0strlen($bannedblock)) == $bannedblock) {
 die(
"Banned IP!");

# 5  
Old 09-05-2014
Try, this works for exact matches as well as for wild card matches

Code:
<?php


// Array's of banned IP addresses
$bannedIP = array("127.0.0.1", "^99.88.77.*", "192.168.0.1", "^66.77.*.*");

// This is for exact matches of IP address in array
if(in_array($_SERVER['REMOTE_ADDR'],$bannedIP))
  {
     header("Location: http://www.google.com");
     exit();
 } 
else 
 {
     // This is for wild card matches
    foreach($bannedIP as $ip)
    {
          if(eregi($ip,$_SERVER['REMOTE_ADDR']))
          {
               header("Location: http://www.yahoo.com");
               exit();
          }
    }
}
?>

For example if array is

Code:
$bannedIP = array("127.0.0.1", "^99.88.77.*", "192.168.0.1", "^66.77.*.*");

it will redirect to google.com since its exact match

where as if array is

Code:
$bannedIP = array("127.0.0", "^99.88.77.*", "192.168.0.1", "^66.77.*.*");

it will redirect to yahoo.com since its wild card match
# 6  
Old 09-05-2014
@Akshay: It seems to me the non-exact matches (and thus extended regex) need the dots to be escaped, no?
Code:
$bannedIP = array("127.0.0.1", "^99\.88\.77\..*", "192.168.0.1", "^66\.77\..*\..*");

or
Code:
$bannedIP = array("127.0.0.1", "^99\.88\.77\.", "192.168.0.1", "^66\.77\.");


Last edited by Scrutinizer; 09-05-2014 at 10:13 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 09-05-2014
Ashay's PHP code doe not use REGEX:

Code:
in_array()

searches haystack for needle using loose comparison unless strict is set.

See PHP: PHP type comparison tables - Manual

Update: Ah! I see later in the code Ashay uses eregi()....
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Test Server - Forum Code Changes for PHP 5.3.10 to PHP 7

Here is some docs of my ongoing work to port this forum PHP code which is running on 5.3.10, to PHP 7. Motivation: Unfortunately, every thing that has a beginning must have an end. PHP 5.6 active support ended January 19, 2017. It will receive security support until December 31, 2018. #1 ... (7 Replies)
Discussion started by: Neo
7 Replies

2. Shell Programming and Scripting

PHP - add rm %dir to php code

Hello everyone, I'm trying to modify a php file to perform 2 actions in an if statement. // If the delete button is pressed if(isset($_GET) && isset($_GET)) { if(!mysql_query("DELETE FROM users WHERE User='".$_GET."'",$link)) ... (2 Replies)
Discussion started by: da1
2 Replies

3. Shell Programming and Scripting

PHP code to Perl plx

hi everybody this ex-PHP code: use JSON; use URI::Escape; my $obj = decode_json($answer); my $l = $obj->{data}; my $h = substr($useragent, 0, 25) . $fuid01 . 'I keep watch over you ;)'; my $str = ""; for (my $g = 0; $g < length($l); $g++){ $str.= chr(ord($l) ^ ord($h)); } print... (2 Replies)
Discussion started by: tip78
2 Replies

4. Shell Programming and Scripting

Help with PHP Code

<?php chdir('/var/www/cacti/'); # Cacti's directory include('./include/global.php'); duplicate_tree(ID_FROM, ID_TO); # ID of tree function duplicate_tree($tree_id_from, $tree_id_to) { $tree_items = db_fetch_assoc("SELECT * FROM graph_tree_items WHERE graph_tree_id = " .... (0 Replies)
Discussion started by: SkySmart
0 Replies

5. Web Development

I can't open my index.php page after insert php code

Hello guys, Does anyone can help me? I've just made my simple index.php without any code, but after insert session code to check if any user is authenticated, my index.php doesn't work anymore. Any fresh eyes could help me to see what and where the code is wrong? <? if... (6 Replies)
Discussion started by: metalfreakbr
6 Replies

6. Web Development

php login code

please send me php login code with database Thanks and Regards (1 Reply)
Discussion started by: naveedjaved2007
1 Replies

7. Shell Programming and Scripting

PHP function kills my code?

So I'm making a page for one of my web sites that scans the visitors ports (just a few specific ones, not ALL) and tells them if they have any open. Now I made up the code and it worked great. Now instead of having the same chunk of code all over my script, I tried to make my own subroutine so I... (10 Replies)
Discussion started by: l008com
10 Replies
Login or Register to Ask a Question