Jump to content

[SOLVED] IP Range Help


HSKrustofsky

Recommended Posts

This is the script I am working with, and everything is working fine...

 

<?php  

    $ip_list = array('123.456.789.10');  
    $ip_range = array('123.456.789.*');
    $user_ip = $_SERVER['REMOTE_ADDR'];  


    if(in_array($user_ip, $ip_list))  
        {  
        header("Location:http://".$_SERVER['HTTP_HOST']."/enterpage.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']."/enterpage.html");  
                }  
            }  
        }

?> 

 

I just recent got a set of IPs where they want a specific range. For example: 123.456.789.1-55. Where they want to allow access to 1-55, but deny 56-255. What I was wondering is, is there an easy/fast way to do this without having to type all the numbers(123.456.789.1, 123.456.789.2,123.456.789.3, etc), 'til I get to 55?

 

Thank you in advanced for your help.

Link to comment
https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/
Share on other sites

Thank you very much, farkewie. That worked perfectly well. I just have 1, well 2 more question. First, what if I want to do this for multiple addresses? Will I have to imput the code multiple times? I have no probelem with that. Also, I was wondering what if it was a range of ranges? For example: 123.456.7-22.*. Is there such a thing? What I was thinking, is not adding the *, and just having that range within the third set of numbers. Will that work properly?

 

Once again, thank you very much for your help.

Link to comment
https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/#findComment-767266
Share on other sites

Like I said earlier I got it to work, but for some weird reason it only works with one. I am trying to add multiple IPs, and it won't let me. Here is the code I am trying to work with...

 

                $ip = "123.123.75";

	$check  = explode('.', $ip);

	if( $check[0] == 123 && $check[1] == 123 && $check[2] >= 34 && $check[2] <= 75)
		{
   			header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html");
		}

	$ip2 = "123.123.123.190";

	$check2  = explode('.', $ip2);

	if( $check2[0] == 123 && $check2[1] == 123 && $check2[2] == 123 && $check2[3] >= 1 && $check2[3] <=190)
		{
   			header("Location:http://".$_SERVER['HTTP_HOST']."/enter.html");
		}

 

Well what is happening is, instead of only allowing these IP ranges to enter.html, it is allowing everyone, and I don't want that. Any suggestions?

Link to comment
https://forums.phpfreaks.com/topic/146059-solved-ip-range-help/#findComment-767828
Share on other sites

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.