filiptronicek

PHP stuff

Oct 6th, 2019
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if(!emptyempty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){  
  2.     //If AJAX Request Then  
  3. }else{  
  4. //something else  
  5. }  
  6. if (file_exists('example.png')) {
  7.     // File exists
  8. }
  9. function detect_city($ip) {
  10.        
  11.         $default = 'UNKNOWN';
  12.  
  13.         if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
  14.             $ip = '8.8.8.8';
  15.  
  16.         $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
  17.        
  18.         $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
  19.         $ch = curl_init();
  20.        
  21.         $curl_opt = array(
  22.             CURLOPT_FOLLOWLOCATION  => 1,
  23.             CURLOPT_HEADER      => 0,
  24.             CURLOPT_RETURNTRANSFER  => 1,
  25.             CURLOPT_USERAGENT   => $curlopt_useragent,
  26.             CURLOPT_URL       => $url,
  27.             CURLOPT_TIMEOUT         => 1,
  28.             CURLOPT_REFERER         => 'http://' . $_SERVER['HTTP_HOST'],
  29.         );
  30.        
  31.         curl_setopt_array($ch, $curl_opt);
  32.        
  33.         $content = curl_exec($ch);
  34.        
  35.         if (!is_null($curl_info)) {
  36.             $curl_info = curl_getinfo($ch);
  37.         }
  38.        
  39.         curl_close($ch);
  40.        
  41.         if ( preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs) )  {
  42.             $city = $regs[1];
  43.         }
  44.         if ( preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs) )  {
  45.             $state = $regs[1];
  46.         }
  47.  
  48.         if( $city!='' && $state!='' ){
  49.           $location = $city . ', ' . $state;
  50.           return $location;
  51.         }else{
  52.           return $default;
  53.         }
  54.        
  55.     }
Advertisement
Add Comment
Please, Sign In to add comment