Friday, November 5, 2021

PHP Redirect based on geolocation and use Proxy for location detection

 As we discussed how to use proxy with http requests made, here is a complete solution to make a dynamic request redirection based on client's country.

 <?php  
 try {  
   if (session_status() === PHP_SESSION_NONE) {  
     session_start();  
   }   
 $q='';  
 $redirectUrl='';  
 $country = '';  
 $country = $_SESSION["country"];  
 if (isset($_GET['q'])) {  
   $q = $_GET['q'];  
 } else {  
   header("https://yoursite.com");   
 }  
 if($country=='' || $country == null){  
   $url="http://ip-api.com/json/".getUserIP();  
   $auth = base64_encode('username:password');  
   $aContext = array(  
     'http' => array(  
       'proxy'      => 'tcp://yourproxyserver.io:31112',  
       'request_fulluri' => true,  
       'header'     => "Proxy-Authorization: Basic ".$auth,  
     ),  
   );  
   $cxContext = stream_context_create($aContext);  
   $sFile = file_get_contents($url, False, $cxContext);  
   $arrIpInfo=json_decode($sFile);  
   $country = strtolower($arrIpInfo->country);  
   $_SESSION["country"] = $country;  
 }  
   if($country=="argentina") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="armenia") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="australia") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="austria") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="azerbaijan") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="belarus") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="belgium") $redirectUrl = "https://redirectionsite.com/s706?q=".$q;  
   else if($country=="brazil") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="canada") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="chile") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="columbia") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="denmark") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="finland") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="france") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="georgia") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="germany") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="hong kong") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="india") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="indonesia") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="ireland") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="italy") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="japan") $redirectUrl = "https://redirectionsite.com/s706?q=".$q;  
   else if($country=="kazakhstan") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="kyrgyzstan") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="luxemburg") $redirectUrl = "https://redirectionsite.com/s706?q=".$q;  
   else if($country=="malaysia") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="mexico") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="moldova") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="netherlands") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="new zealand") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="norway") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="peru philippines") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="russia") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="singapore") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="spain") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="sweden") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="switzerland") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="taiwan") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="tajikistan") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="thailand") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="turkmenistan") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="uk") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="united kingdom") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="united states") $redirectUrl = "https://redirectionsite.com/s533?q=".$q;  
   else if($country=="uzbekistan") $redirectUrl = "https://redirectionsite.com/s564?q=".$q;  
   else if($country=="venezuela") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else if($country=="vietnam") $redirectUrl = "https://redirectionsite.com/s545?q=".$q;  
   else $redirectUrl = "https://redirectionsite.com/s901?q=".$q;  
      header("Location: ".$redirectUrl);    
 }  
 catch(Exception $e) {  
   echo 'Message: ' .$e->getMessage();  
  }  
 function getUserIP() {  
   $ipaddress = '';  
   if (isset($_SERVER['HTTP_CLIENT_IP']))  
     $ipaddress = $_SERVER['HTTP_CLIENT_IP'];  
   else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))  
     $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];  
   else if(isset($_SERVER['HTTP_X_FORWARDED']))  
     $ipaddress = $_SERVER['HTTP_X_FORWARDED'];  
   else if(isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']))  
     $ipaddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];  
   else if(isset($_SERVER['HTTP_FORWARDED_FOR']))  
     $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];  
   else if(isset($_SERVER['HTTP_FORWARDED']))  
     $ipaddress = $_SERVER['HTTP_FORWARDED'];  
   else if(isset($_SERVER['REMOTE_ADDR']))  
     $ipaddress = $_SERVER['REMOTE_ADDR'];  
   else  
     $ipaddress = 'UNKNOWN';  
   return $ipaddress;  
 }  
 ?>  
redirectionsite.com

This is where the actual redirection happens. based on it's s code (ex: s564) it redirects to the country specific location.

Happy coding....


No comments:

Post a Comment