Page 1 of 1

HELP! *Allow Access By IP Range* HELP!

Posted: Wed Jan 28, 2009 1:07 pm
by HSKrustofsky
I'm having a little difficulty trying to get something to work. I want to limit the access to a page on my site by IP. I am able to limit the access when a complete address is entered, but I would like to enter a certain IP range without having to enter every single IP address.

I did some research, and thought this might work:

Code: Select all

 
<?php
 
    $userip = array("123.456.789.10","456.789.123.*");
    
        if (!in_array($_SERVER['REMOTE_ADDR'], $userip))
            {
            header("Location:http://".$_SERVER['HTTP_HOST']."/error.html");
        } else 
            { 
            header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html"); 
            }
 
?>
 
...but doesn't. Can anyone help me? Thanks in advance.

Re: HELP! *Allow Access By IP Range* HELP!

Posted: Wed Jan 28, 2009 7:20 pm
by siapagweh
in IP Address ver.4 there are 3 type used to as Int'
A = 1._._._ ~ 126._._._ SM = 255.0.0.0
B = 128._._._ ~ 191._._._ SM = 255.255.0.0
C = 192._._._ ~ 224._._._ Sm = 255.255.255.0

lets try C (d simple one)
i have this IP range
192.168.0.1 ~ 192.168.0.90 ==> allowed to enter
192.168.0.91 ~ 192.168.0.254 ==> disallowed to enter / error

Code: Select all

$ip_header = "192.168.0";
$ip_client = $_SERVER['REMOTE_ADDR'];
 
$check = checkIP($ip_header, $ip_client);
if($check == 1){
    header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html"); 
}else{
    ...
}
 
function checkIP($ip_header, $ip_client){
$value_to_return = 0;
//do looping
for($a=1;$a<=90;$a++){
    $ip_to_match = $ip_header . "." . $a;
    if($ip_to_match == $ip_client){
        $value_to_return = 1;
    }
}
return $value_to_return;
}

nb: not tested yet ...

Re: HELP! *Allow Access By IP Range* HELP!

Posted: Thu Jan 29, 2009 4:32 am
by VladSun
Take a look at:
ip2long() and long2ip()

Re: HELP! *Allow Access By IP Range* HELP!

Posted: Thu Jan 29, 2009 5:39 pm
by HSKrustofsky
Thank you both for your help. I will try 'em out, and post my results.

Re: HELP! *Allow Access By IP Range* HELP!

Posted: Sat Jan 31, 2009 6:20 pm
by HSKrustofsky
Well, I was unable to get anything to work. I took siapagweh idea, but it didn't work. It sent me to my error page. Maybe I did something wrong.

Here is the code as how it looks on my page...

Code: Select all

 
<?php
 
    $ip_header = "10.123.456..*";   // Actual IP here, of course
    $ip_client = $_SERVER['REMOTE_ADDR'];
 
    $check = checkIP($ip_header, $ip_client);
    if($check == 1)
        {
 
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="Differential Equations, Dynamical Systems, Nonlinear Problems, Engineering, Sciences" />
<meta name="description" content="Journal, Mathematics, Nonlinear Studies" />
<title>ADVANCED NONLINEAR STUDIES - Vol.9 Num.1</title>
 
<link rel="stylesheet" type="text/css" href="Styles/mainstyle.css" />
 
</head>
 
<body>
    
    <div id="link">
    <table width="800px" align="center" cellpadding="0" cellspacing="0">
        <tr>
            <td colspan="2">&nbsp;
            
            </td>
        </tr>
        <tr>
            <td width="128"  bgcolor="#015294">
                <a href="journalcover.html"><img src="Images/ShairCover_small.jpg" /></a>
            </td>
            <td align="center"  bgcolor="#015294">
                <font color="#FFFFFF" size="+5">ADVANCED NONLINEAR<br />STUDIES</font><br />
                <hr size="2" width="500px" color="#COCOCO" noshade="noshade" />
                <font color="#FFFFFF">ISSN 1536-1365</font>         
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;
            
            </td>
        </tr>
        <tr>
            <td style="font-family:Arial, Helvetica, sans-serif; color:#015294; border-bottom:solid 2px #015294; border-top:solid 2px #015294;" colspan="2" align="center">
                <b>
                <ul id="nav">
                    <li style="border-right:solid 2px #015294;"><a href="index.html">Home</a></li>
                    <li style="border-right:solid 2px #015294;"><a href="#">Abstracts</a>
                            <ul>
                                <li><a href="#">Vol. 9, Num. 1</a>
                                    <ul>
                                       <li><a href="Support/Abstracts/venegas.proofread.pdf">Abstract 1</a></li>
                                       <li><a href="Support/Abstracts/jleli2.proofread.pdf">Abstract 2</a></li>
                                       <li><a href="Support/Abstracts/Carl-Motreanu.proofread.pdf">Abstract 3</a></li>
                                       <li><a href="Support/Abstracts/Zhu.proofread.pdf">Abstract 4</a></li>
                                       <li><a href="Support/Abstracts/zanolin.proofread.pdf">Abstract 5</a></li>
                                       <li><a href="Support/Abstracts/ghimenti.proofread.pdf">Abstract 6</a></li>
                                       <li><a href="Support/Abstracts/Veron.proofread.pdf">Abstract 7</a></li>
                                       <li><a href="Support/Abstracts/Melian.proofread.pdf">Abstract 8</a></li>
                                       <li><a href="Support/Abstracts/cassani.proofread.pdf">Abstract 9</a></li>
                                       <li><a href="Support/Abstracts/Bene.proofread.pdf">Abstract 10</a></li>
                                       <li><a href="Support/Abstracts/draghici.proofread.pdf">Abstract 11</a></li>
                                    </ul>
                                </li>
                            </ul>
                      </li>
                      <li style="border-right:solid 2px #015294;"><a href="../editors.html">Journal Editors</a></li>
                      <li style="border-right:solid 2px #015294;"><a href="#">Articles</a>
                        <ul>
                            <li><a href="#">Volume 9</a>
                                <ul>
                                    <li><a href="v9n1.php">Number 1</a></li>
                                </ul>
                            </li>
                        </ul>
                      <li style="border-right:solid 2px #015294;"><a href="format.html">Format Instructions</a></li>
                      <li><a href="subscribe.html">Order Form</a></li>
                </ul> 
                </b>
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;
            
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center" style="border-bottom:solid 2px #015294;">
            <font size="5" face="Arial, Helvetica, sans-serif" color="#015294">
            <b>Table of Contents</b>
            </font>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <table width="100%" cellpadding="0" cellspacing="0" align="center" style="border-bottom:solid 2px #015294;">
                    <tr>
                        <td width="266">
                        <font face="Arial, Helvetica, sans-serif" color="#015294">
                        <b>Volume 9</b>
                        </font>
                        </td>
                        <td align="center" width="266">
                        <font face="Arial, Helvetica, sans-serif" color="#015294">
                        <b>Number 1</b>
                        </font>
                        </td>
                        <td align="right" width="267">
                        <font face="Arial, Helvetica, sans-serif" color="#015294">
                        <b>February 2009</b>
                        </font>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;
            
            </td>
        </tr>
        <tr>
            <td align="center" colspan="2">
           <font face="Arial, Helvetica, sans-serif" style="font-size:12pt">Click on the <img src="Images/pdficon_small.gif" /> icon to view the articles.</font>
            </td>
        </tr>
        <tr>
            <td colspan="2">
            <font face="Arial, Helvetica, sans-serif" style="font-size:12pt"><p><i><a href="Support/Articles/V9N1_pg1-27.pdf"><img src="Images/pdficon_small.gif" /></a>The Porous Medium Equation With Blowing Up<br />
            Boundary Data</i> ......................................................................................................................................................................... 1<br />
            <b>Osvaldo Venegas T.</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg29-68.pdf"><img src="Images/pdficon_small.gif" /></a>Moduli Space Theory of Constan Mean<br />
            Curvature Hypersurfaces</i> ..................................................................................................................................................... 29<br />
            <b>Mohamed JLELI</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg69-80.pdf"><img src="Images/pdficon_small.gif" /></a>Comparison Principle For Quaslinear Parabolic<br />
            Inclusions with Clarke's Gradient</i> ....................................................................................................................................... 69<br />
            <b>Siegfied Carl, Dumitru Motreanu</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg81-94.pdf"><img src="Images/pdficon_small.gif" /></a>A Note on Asymptotically Linear Schrödinger<br />
            Equation on R<sup>N</sup></i> ..................................................................................................................................................................... 81<br />
            <b>Hongbo Zhu</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg95-135.pdf"><img src="Images/pdficon_small.gif" /></a>Conected Branches of Initial Points for<br />
            Asymptotic BVPs, With Application to<br />
            Heteroclinic and Homoclinic Solutions</i> ............................................................................................................................. 95<br />
            <b>Alessandro Margheri, Carlotta Rebelo, Fabio Zanolin</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg137-147.pdf"><img src="Images/pdficon_small.gif" /></a>Semiclassical Limit For the Nonlinear Klein<br />
            Gordon Equation in Bounded Domains</i> .......................................................................................................................... 137<br />
            <b>Marco G. Ghimenti, Carlo R. Grisanti</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg149-164.pdf"><img src="Images/pdficon_small.gif" /></a>On Uniqueness of Large Solutions of Nonlinear<br />
            Parabolic Equations in Nonsmooth Domains</i> ................................................................................................................ 149<br />
            <b>Waad Al Sayed, Laurent Véron</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg165-176.pdf"><img src="Images/pdficon_small.gif" /></a>Quasilinear Equations with Boundary Blow-up<br />
            and Exponential Reaction</i> ................................................................................................................................................. 165<br />
            <b>Jorge García-Melián</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg177-197.pdf"><img src="Images/pdficon_small.gif" /></a>On a Fourth Order Elliptic Problem<br />
            with a Singular Nonlinearity</i>  .............................................................................................................................................. 177<br />
            <b>Daniele Cassani, João Marcos do Ó, Nassif Ghoussoub</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg199-214.pdf"><img src="Images/pdficon_small.gif" /></a>Retarded Functional Differential Equations on<br />
            Manifolds and Applications to Motion Problems<br />
            for Forced Constrained Systems</i> ...................................................................................................................................... 199<br />
            <b>Pierluigi Benevieri, Alessandro Calamai, Massimo Furi, Maria Patrizia Pera</b></p>
 
            <p><i><a href="Support/Articles/V9N1_pg215-226.pdf"><img src="Images/pdficon_small.gif" /></a>Uniqueness and Characterization of the<br />
            Maximizers of Integral Functionals With<br />
            Constraints</i> ........................................................................................................................................................................... 215<br />
            <b>Cristina Draghici, Hichem Hajaiej</b></p></font>
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;
            
            </td>
        </tr>
        <tr>
            <td style="font-family:Arial, Helvetica, sans-serif; font-size:10pt; border-bottom:solid 2px #015294; border-top:solid 2px #015294;" colspan="2" align="center">
            <b>Advanced Nonlinear Studies<br />
            Managing Editor: Prof. Shair Ahmad<br />
            Email: <a href="mailto:shair.ahmad@utsa.edu">shair.ahmad@utsa.edu</a></b>
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;
            
            </td>
        </tr>
    </table>
    </div>
 
</body>
</html>
 
<?php
    
        } else 
            { 
            header("Location:http://".$_SERVER['HTTP_HOST']."/error.html"); 
            }
    
    function checkIP($ip_header, $ip_client)
        {
        $value_to_return = 0;
        
        for($a=1;$a<=90;$a++)
            {
            $ip_to_match = $ip_header . "." . $a;
            if($ip_to_match == $ip_client)
                {
                $value_to_return = 1;
                }
            }
            return $value_to_return;
        }
 
?>
 
 

Re: HELP! *Allow Access By IP Range* HELP!

Posted: Sat Jan 31, 2009 7:12 pm
by VladSun

Code: Select all

$banned = array();
 
$banned[] = array('192.168.0.23', '192.168.0.26');
$banned[] = array('192.168.1.123', '192.168.1.126');
$banned[] = array('192.168.2.213', '192.168.4.226');
 
 
function checkIP($banned, $ip)
{
    $ip = ip2long($ip);
    foreach ($banned as $range)
        if ($ip >= ip2long($range[0]) && $ip <= ip2long($range[1]))
            return false;
    return true;
}

Re: HELP! *Allow Access By IP Range* HELP!

Posted: Sat Jan 31, 2009 9:27 pm
by HSKrustofsky
Well, I tried that, but for some weird reason it was giving me a blank page. I did some research and found something hat halfway works, and was wondering if anyone could give me any suggestions.

Here is what I am trying to work with...

Code: Select all

 
<?php  
 
    $ip_list = array('123.456.789.10', '10.789.456.789');  
    $ip_range = array('10.123.456.*', '10.456.789.*');  
    $user_ip = $_SERVER['REMOTE_ADDR'];  
  
  
    if(!in_array($user_ip, $ip_list))  
        {  
        header("Location:http://".$_SERVER['HTTP_HOST']."/error.html");  
        }  
 
    if(!empty($ip_range))  
        {  
        foreach($ip_range as $range)  
            {  
            $range = str_replace('*','(.*)', $range);  
  
            if(preg_match('/'.$range.'/', $user_ip))  
                {  
                header("Location:http://".$_SERVER['HTTP_HOST']."/error.html");  
                }  
            }  
        } 
 
?>  
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Page</title>
</head>
 
<body>
    Here is the main page!
</body>
</html>
 
The first array allows the $ip_list to enter my list, but the second array is banning the $ip_range. I need the$ip_range to allow the IPs within its array, or else, redirect to error.html.

Any suggestions? Thanks in advanced.

Re: HELP! *Allow Access By IP Range* HELP!

Posted: Sun Feb 01, 2009 5:37 am
by VladSun
HSKrustofsky wrote:Any suggestions? Thanks in advanced.
Yes, you should start debugging :)

Re: HELP! *Allow Access By IP Range* HELP!

Posted: Thu May 02, 2013 4:49 pm
by swbratcher
Here's your fix:

Code: Select all


$ip_list = array('61.2.51.124', '61.2.51.123');  // one-off allowed IPs.
$ip_range = array('61.2.51.2.*', '61.2.11.*');     // main allowed IP ranges.
$user_ip = $_SERVER['REMOTE_ADDR'];  
if( ip_list_check($user_ip,$ip_list) == "FAIL" && ip_range_check($user_ip,$ip_range) == "FAIL" ) {	
    header("Location:http://www.domain.com");
	// echo("\n\nDUMPED.");
	exit();
}
function ip_list_check($user_ip,$ip_list){
	// echo("list checked.\n");
	if(in_array($user_ip, $ip_list)) {  
		// echo("FOUND.\n");
		return "PASS";
	} else {	
		// echo("NOT FOUND.\n");
		return "FAIL";
	}
}

function ip_range_check($user_ip,$ip_range){
	if(!empty($ip_range)) {  
	    foreach($ip_range as $range) {  
			// echo("range checked: ");
	        $range = str_replace('*','(.*)', $range);  
			// echo($range."\n");
	        if(preg_match('/'.$range.'/', $user_ip)) { 
				// echo("FOUND.\n");
				return "PASS";
	        }
	    }	
		// echo("NOT FOUND.\n");
		return "FAIL";
	}
}
You can clear out those 'echo' lines if you don't have any use for debugging output.

Enjoy.

Re: HELP! *Allow Access By IP Range* HELP!

Posted: Fri May 03, 2013 9:58 am
by pickle
This is a 4 year old thread. Please check the date before necroposting