Skip to content

Instantly share code, notes, and snippets.

@cereal-s
Created January 20, 2014 11:13
Show Gist options
  • Save cereal-s/8518394 to your computer and use it in GitHub Desktop.
Save cereal-s/8518394 to your computer and use it in GitHub Desktop.
Simple snippet, used to get the headers from link and his redirects. Requires PHP 5.1.3 and above.
<?php
$url = false;
if($_POST)
{
$url = $_POST['url'];
stream_context_set_default(array('http' => array('method' => 'HEAD')));
$headers = get_headers($url, 0);
$i = 0;
$result = array();
foreach($headers as $key => $value)
{
if(preg_match('/HTTP\/(?=.*+).*+/', $value, $match) == 1)
{
$i++;
}
if(preg_match('/(?<=Location:).*+/', $value, $match) == 1)
{
$link = trim($match[0]);
$result[$i][] = "<a href=\"$link\" target=\"_blank\">$link</a>";
continue;
}
$result[$i][] = $value;
}
}
?>
<form method="POST" action="">
<label for="url">URL</label>
<input type="text" name="url" <?php echo $url ? "value=\"$url\"":''; ?> />
<input type="submit" name="submit" value="get the headers" />
</form>
<?php
if( $_POST )
{
echo "<h3>Headers for: <a href=\"$url\">{$url}</a></h3>";
echo "<pre>";
print_r($result);
echo "</pre>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment