Last active
December 4, 2015 15:26
-
-
Save marcosfreitas/8848904 to your computer and use it in GitHub Desktop.
This script will redirect users from determined URL in accord with the your COUNTRY_NAME, COUNTRY_CODE, CITY (name), REGION_CODE or REGION_NAME returned by freegeoip.net webservice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * This script will redirect users from determined URL in accord with the your: | |
| * COUNTRY_NAME, COUNTRY_CODE, CITY (name), REGION_CODE or REGION_NAME returned by freegeoip.net webservice | |
| * @author Marcos Freitas - marcosfreitas[at]c4network[dot]com[dot]br | |
| * -- | |
| * Thanks to freegeoip.net for the 'simple' webservice. | |
| */ | |
| // Redirect function | |
| function Redirect($url, $permanent){ | |
| if (headers_sent() === false){ | |
| // moved permanently true or false. If false send code 303, 'see other' | |
| header('Location: ' . $url, true, ($permanent === true) ? 301 : 303); | |
| }else{ | |
| echo '<script> console.log("our ogres says: headers already send!");</script>'; | |
| } | |
| exit(); | |
| } | |
| // Creating a cookie to turn set language permissive on the options displayed to user in the site, as buttons or country flags. | |
| if(!isset($_COOKIE['away-from-here'])): | |
| setcookie('away-from-here',time(), time()+3600); | |
| endif; | |
| // This will allow you to view errors in the browser | |
| // set "display_errors" to 0 in production | |
| ini_set('display_errors',0); | |
| // Report all PHP errors (notices, errors, warnings, etc.) | |
| error_reporting(false); | |
| // Enable debug mode to show message info (true / false) | |
| $debug_mode = false; | |
| // url to redirect | |
| $redirect_to = 'http://c4network.com.br'; | |
| // set the type of filter user. | lowercase or uppercase | |
| // COUNTRY_NAME, COUNTRY_CODE, CITY (name), REGION_CODE or REGION_NAME | |
| $filter_user_by = 'region_name'; | |
| // set the default match value to the filter. | lowercase or uppercase | |
| // if this is satisfacted by conversation with the webservice, we will redirect the user to the user | |
| // EX.: If the ip of the user is from "maranhão", we will redirect he. Else, he still here. | |
| $filter_match = utf8_decode('maranhão'); | |
| // User IP Address | |
| $user_ip = $_SERVER['REMOTE_ADDR']; | |
| // Format of response I need | |
| $format = 'json'; | |
| // URI containing components necessary to make request | |
| $uri ="freegeoip.net/".$format."/".$user_ip; | |
| // Mechanism for transferring data | |
| $protocol = "http://"; | |
| // Request with all components | |
| $request = $protocol . $uri; | |
| // Initialize the session by passing the request as a parameter | |
| $session = curl_init($request); | |
| // Set curl options by passing session and flags | |
| // CURLOPT_HEADER allows us to receive the HTTP header | |
| curl_setopt($session, CURLOPT_HEADER, false); | |
| // CURLOPT_RETURNTRANSFER will return the response | |
| curl_setopt($session, CURLOPT_RETURNTRANSFER, true); | |
| // Make the request | |
| $response = curl_exec($session); | |
| // Working with the response | |
| $decoded = json_decode($response, true); | |
| ?> | |
| <?php | |
| if ( isset($decoded->response->status) && $decoded->response->status == 'ERROR'){ | |
| echo "<script>console.info('Oh no! We've found an error occured during curl exec. ". $decoded->response->errormessage."');</script>"; | |
| }else{ | |
| // options filter | |
| $country_code = strtolower(utf8_decode($decoded['country_code'])); | |
| $country_name = strtolower(utf8_decode($decoded['country_name'])); | |
| $city_name = strtolower(utf8_decode($decoded['city'])); | |
| $region_code = strtolower(utf8_decode($decoded['region_code'])); | |
| $region_name = strtolower(utf8_decode($decoded['region_name'])); | |
| $filter = array( | |
| 'country_code' => $country_code, | |
| 'country_name' => $country_name, | |
| 'city_name' => $city_name, | |
| 'city' => $city_name, | |
| 'region_code' => $region_code, | |
| 'region_name' => $region_name | |
| ) | |
| ; | |
| ?> | |
| <?php | |
| if ($debug_mode === true): | |
| ?> | |
| <style> | |
| .find-you-debug{font-weight: normal; font-family: helvetica, sans-serif; color: #fff !important; background-color: rgba(41, 128, 185, 1); padding: 10px;} | |
| </style> | |
| <div class="find-you-debug"> | |
| <?php var_dump($filter); ?> | |
| </div><br> | |
| <?php | |
| endif; | |
| ?> | |
| <?php | |
| if(!empty($filter_user_by)){ | |
| // search the match string to filter user in array of options | |
| // Ex.: search BR in the array, maybe return 'country_code' index else return 0 or FALSE | |
| $filter_match_result = array_search($filter_match, $filter); | |
| if(is_string($filter_match_result)){ | |
| $filter_match_result = true; | |
| }else{ | |
| $filter_match_result = false; | |
| } | |
| }else{ | |
| // verify if the string match is identic to the value of the filter type | |
| // the match considers the location of the IP | |
| // if TRUE the ip location is identic as filter match, if FALSE the ip is from other location and not need to redirect | |
| $filter_match_result = ($filter[$filter_user_by] === $filter_match) ? true : false; | |
| } | |
| if ($debug_mode === true): | |
| echo "Filter Match Result: "; var_dump($filter_match_result); | |
| echo " ### ".$filter_user_by.": "; var_dump($filter[$filter_user_by]); | |
| echo " ### Need to be: "; var_dump($filter_match); | |
| endif; | |
| if($filter_match_result === false){ | |
| echo "<script>console.info('Oh no! the search in the filter returns no results');</script>"; | |
| } | |
| if($filter_match_result === true && !isset($_COOKIE['away-from-here'])){ | |
| echo 'redirect.'; | |
| if(!empty($redirect_to)) | |
| Redirect($redirect_to, false); | |
| exit; | |
| } | |
| } | |
| // Close the curl session | |
| curl_close($session); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment