I modified Adam Backstrom's answer and implemented the chiborg proposal. (Only for downloading HEAD). It has one more thing: it checks whether it is redirected to a page of the same server or not. Example: terra.com.br redirects to terra.com.br/portal. PHP will treat it as a redirect, and rightly so. But I just wanted to list this URL, which redirects to another URL. My English is not very good, so if someone has found something really difficult to understand and can edit it, please.
function RedirectURL() { $urls = array('http://www.terra.com.br/','http://www.areiaebrita.com.br/'); foreach ($urls as $url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // chiborg suggestion curl_setopt($ch, CURLOPT_NOBODY, true); // ================================ // READ URL // ================================ curl_setopt($ch, CURLOPT_URL, $url); $out = curl_exec($ch); // line endings is the wonkiest piece of this whole thing $out = str_replace("\r", "", $out); echo $out; $headers = explode("\n", $out); foreach($headers as $header) { if(substr(strtolower($header), 0, 9) == "location:") { // read URL to check if redirect to somepage on the server or another one. // terra.com.br redirect to terra.com.br/portal. it is valid. // but areiaebrita.com.br redirect to bwnet.com.br, and this is invalid. // what we want is to check if the address continues being terra.com.br or changes. if changes, prints on page. // if contains http, we will check if changes url or not. // some servers, to redirect to a folder available on it, redirect only citting the folder. Example: net11.com.br redirect only to /heiden // only execute if have http on location if ( strpos(strtolower($header), "http") !== false) { $address = explode("/", $header); print_r($address); // $address['0'] = http // $address['1'] = // $address['2'] = www.terra.com.br // $address['3'] = portal echo "url (address from array) = " . $url . "<br>"; echo "address[2] = " . $address['2'] . "<br><br>"; // url: terra.com.br // address['2'] = www.terra.com.br // check if string terra.com.br is still available in www.terra.com.br. It indicates that server did not redirect to some page away from here. if(strpos(strtolower($address['2']), strtolower($url)) !== false) { echo "URL NOT REDIRECT"; } else { // not the same. (areiaebrita) echo "SORRY, URL REDIRECT WAS FOUND: " . $url; } } } } } }
jaysponsored
source share