PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $url="google.com" ; function get_last_location($url, $fail='Invalid URL') { $context = stream_context_create( array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, ), 'http'=>array( 'max_redirects'=>10, 'ignore_errors'=>0, 'method'=>"GET", 'header'=>"Accept-language: en\r\n" . "Cookie: \r\n" . "User-Agent: Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19\r\n" ) )); $headers = get_headers($url, 1, $context); if(preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#", $headers[0] , $response )) $reponse_code = intval($response[1]); else $reponse_code = 0; if ($reponse_code == 200) return $url; if (!isset($headers['Location'])) $headers['Location'] = [$url]; $location = $headers['Location']; if (!is_array($location)) $location = [$location]; if ( ($reponse_code == 301) || ($reponse_code == 302) ) { $last_location = $location[count($location)-1]; $httpfind = false; foreach (array_reverse($location) as $loc) { if (strpos($loc, 'http') !== FALSE) { $httpfind = true; if ($last_location != $loc) { $last_location = rtrim($loc,'/') .'/'. ltrim($last_location, '/'); } break; } } if (!$httpfind) { $last_location = rtrim($url,'/') .'/'. ltrim($last_location, '/'); } } else { $last_location = $fail; } return $last_location; } $q = get_last_location($url); echo "$q"; ?>
Show:  
Copy Clear