(help)country based url redirecting ???

Started on the old forum in November 2011 · view the original in the archive · old links redirect here

12 replies · 805 views · started 15 years ago · 1 min read

4 participants
Mr-59 Developer · 137 posts #2
<?php 
$ip=$_SERVER['REMOTE_ADDR']; 
 geoCheckIP($ip); 
   
           

       //Get an array with geoip-infodata 
       function geoCheckIP($ip) 
       { 
               //check, if the provided ip is valid 
               if(!filter_var($ip, FILTER_VALIDATE_IP)) 
               { 
                       throw new InvalidArgumentException("IP is not valid"); 
               } 

               //contact ip-server 
               $response=@file_get_contents('http://www.netip.de/search?query='.$ip); 
               if (empty($response)) 
               { 
                       throw new InvalidArgumentException("Error contacting Geo-IP-Server"); 
               } 
    //Array containing all regex-patterns necessary to extract ip-geoinfo from page 
              $patterns=array(); 
               $patterns["domain"] = '#Domain: (.*?) #i'; 
               $patterns["country"] = '#Country: (.*?) #i'; 
               $patterns["state"] = '#State/Region: (.*?)<br#i'; 
               $patterns["town"] = '#City: (.*?)<br#i'; 

            
               //Array where results will be stored 
               $ipInfo=array(); 

               //check response from ipserver for above patterns 
               foreach ($patterns as $key => $pattern) 
               { 
                       //store the result in array 
                       $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found'; 
               } 

               return $ipInfo; 
       } 
$my_array=geoCheckIP($ip); 

foreach ($my_array as $key => $value){ 
 if($key=='country'){ 
 #switch on the country: 
 switch ($value) { 
 case 'IN - India': 
 echo "('header : ../index2.php')"; 
 break; 
default :
echo "('header : ../index3.php')":
 break; 
  } 
 } 

} 
?>

all indian users redirect to index2.php page and othe country user index3.php page. Call this in in your top of index page using include function.

Mr-59 Developer · 137 posts #4

fix quotes and single quotes arround.

s3nzo Developer · 156 posts #5

Switch should be like this

switch ($value) { 
 case 'IN - India': header('Location:../index2.php'); 
 break; 
default : header('Location:../index3.php');
 break; 
  } 
softwarefreak Senior · 349 posts #8

slowy wrote: if ip is wrong, then how to redirect it to other page?????

You mean if the user is behind a proxy?

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

softwarefreak Senior · 349 posts #10
<?php
function gum_real_ip()  
{  
    $keyname_ip_arr = array('HTTP_X_FORWARDED_FOR', 'HTTP_REMOTE_ADDR_REAL', 'HTTP_CLIENT_IP', 'HTTP_X_REAL_IP', 'REMOTE_ADDR');  
    foreach ($keyname_ip_arr as $keyname_ip) {  
        if (!empty($_SERVER[$keyname_ip])) {  
            $ip = $_SERVER[$keyname_ip];  
            break;  
        }  
    }  
    if (strstr($ip, ',')) { 
        $ips = explode(',', $ip); 
         if(substr($ips[0], 1, 3)=='10.'&&strlen($ips[1])>5) 
            $ip = trim($ips[1]); 
        else $ip = trim($ips[0]); 
    }  
    if(!preg_match("^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}^", $ip)) $ip = $_SERVER["REMOTE_ADDR"];  
    return $ip; 
    list($ip1,$ip2,$ip3,$ip4) = explode('.',$ip);  
    return $ip1.'.'.$ip2.'.'.$ip3.'.0'; //REAL IP  
}  
$ip = gum_real_ip();
//& continue with the below code

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

softwarefreak Senior · 349 posts #11
<?php 
function gum_real_ip()   
{   
    $keyname_ip_arr = array('HTTP_X_FORWARDED_FOR', 'HTTP_REMOTE_ADDR_REAL', 'HTTP_CLIENT_IP', 'HTTP_X_REAL_IP', 'REMOTE_ADDR');   
    foreach ($keyname_ip_arr as $keyname_ip) {   
        if (!empty($_SERVER[$keyname_ip])) {   
            $ip = $_SERVER[$keyname_ip];   
            break;   
        }   
    }   
    if (strstr($ip, ',')) {  
        $ips = explode(',', $ip);  
         if(substr($ips[0], 1, 3)=='10.'&&strlen($ips[1])>5)  
            $ip = trim($ips[1]);  
        else $ip = trim($ips[0]);  
    }   
    if(!preg_match("^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}^", $ip)) $ip = $_SERVER["REMOTE_ADDR"];   
    return $ip;  
    list($ip1,$ip2,$ip3,$ip4) = explode('.',$ip);   
    return $ip1.'.'.$ip2.'.'.$ip3.'.0'; //REAL IP   
}   
function geoCheckIP($ip)  
       {  
               //check, if the provided ip is valid  
               if(!filter_var($ip, FILTER_VALIDATE_IP))  
               {  
                       throw new InvalidArgumentException("IP is not valid");  
               }  

               //contact ip-server  
               $response=@file_get_contents('http://www.netip.de/search?query='.$ip);  
               if (empty($response))  
               {  
                       throw new InvalidArgumentException("Error contacting Geo-IP-Server");  
               }  
    //Array containing all regex-patterns necessary to extract ip-geoinfo from page  
              $patterns=array();  
               $patterns["domain"] = '#Domain: (.*?) #i';  
               $patterns["country"] = '#Country: (.*?) #i';  
               $patterns["state"] = '#State/Region: (.*?)<br#i';  
               $patterns["town"] = '#City: (.*?)<br#i';  

             
               //Array where results will be stored  
               $ipInfo=array();  

               //check response from ipserver for above patterns  
               foreach ($patterns as $key => $pattern)  
               {  
                       //store the result in array  
                       $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';  
               }  

               return $ipInfo;  
       }
$ip = gum_real_ip(); 
$my_array=geoCheckIP($ip);  

foreach ($my_array as $key => $value){  
 if($key=='country'){  
 #switch on the country:  
 switch ($value) {  
 case 'IN - India':  
 header("location:indian_visitors.php");
 break;  
default : 
 header("location:other_visitors.php");
 break;  
  }  
 }  

}  
?>

This shud work now..

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

softwarefreak Senior · 349 posts #13

slowy wrote: here other_visitors =anonymous proxy users?

NO 😀,the real ip will be detected(even if they are using proxy) & then they will be redirected like b4,other_visitors.php is the location you want to send ppl from countries other than India 🙂

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close