PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $url = 'google.com/'; // First let's find out if we just typed the domain name alone or we prepended with a protocol if (preg_match('/(http|https):\/\/[a-z0-9]+[a-z0-9_\/]*/',$url)) { $url = $url; } else { $url = 'http://' . $url; echo '<p>No protocol given, defaulting to http://'; } // Let's print out the initial URL echo '<p>Initial URL: ' . $url . '</p>'; // Prepare the HEAD method when we send the request stream_context_set_default(array('http' => array('method' => 'HEAD'))); // Probe for headers $headers = get_headers($url, 1); // If there is a Location header, trigger logic if (isset($headers['Location'])) { // If there is more than 1 redirect, Location will be array if (is_array($headers['Location'])) { // If that's the case, we are interested in the last element of the array (thus the last Location) echo '<p>Redirected URL: ' . $headers['Location'][array_key_last($headers['Location'])] . '</p>'; $url = $headers['Location'][array_key_last($headers['Location'])]; } else { // If it's not an array, it means there is only 1 redirect //var_dump($headers['Location']); echo '<p>Redirected URL: ' . $headers['Location'] . '</p>'; $url = $headers['Location']; } } else { echo '<p>URL: ' . $url . '</p>'; } // You can now send get_headers to the latest location $headers = get_headers($url, 1); ?>
Show:  
Copy Clear