During web development many times it happens, you have a URL and when you apply it, URL redirects you somewhere else.
Scenario One:
Suppose you are working on a facebook project and you need to get user profile image. By Facebook PHP API you can get it very easily. But when you try to save image from given facebook profile image URL, you get nothing. Why? Because when you apply facebook profile image URL, it redirects you somewhere else.Scenario Two:
Suppose you are working on a project like stackoverflow. There will be many users who would like to trick others and start posing URL whose endpoint is different than posted URL.Posted URL
http://www.mygrtsite.com/goodpage.php
Endpoint
http://www.gotohellvisitor.com/iammakingmoney.php
To prevent this kind of situation you have to check the posted URL’s endpoint.
I think I have explained enough through above examples, let’s check the code and find out how it works.
Create a PHP file and name it endpoint.php. Copy n paste below code in it.
endpoint.php
<?php
function getEndPoint($url){
$url_parts = @parse_url($url);
if (!$url_parts) return false;
if (!isset($url_parts['host'])) return false; //can't process relative URLs
if (!isset($url_parts['path'])) $url_parts['path'] = '/';
$sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
if (!$sock) return false;
$request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
$request .= 'Host: ' . $url_parts['host'] . "\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($sock, $request);
$response = '';
while(!feof($sock)) $response .= fread($sock, 8192);
fclose($sock);
if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
if ( substr($matches[1], 0, 1) == "/" )
return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
else
return trim($matches[1]);
} else {
return false;
}
}
?>
function getEndPoint($url){
$url_parts = @parse_url($url);
if (!$url_parts) return false;
if (!isset($url_parts['host'])) return false; //can't process relative URLs
if (!isset($url_parts['path'])) $url_parts['path'] = '/';
$sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : 80), $errno, $errstr, 30);
if (!$sock) return false;
$request = "HEAD " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " HTTP/1.1\r\n";
$request .= 'Host: ' . $url_parts['host'] . "\r\n";
$request .= "Connection: Close\r\n\r\n";
fwrite($sock, $request);
$response = '';
while(!feof($sock)) $response .= fread($sock, 8192);
fclose($sock);
if (preg_match('/^Location: (.+?)$/m', $response, $matches)){
if ( substr($matches[1], 0, 1) == "/" )
return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
else
return trim($matches[1]);
} else {
return false;
}
}
?>
Copy n paste below code at the bottom in endpoint.php.
Try and Check:
$url = "http://bit.ly/1jUxzeH";
echo getEndPoint($url);
echo getEndPoint($url);
Open endpoint.php in your browser and find out the endpoint of URL.
0 comments:
Post a Comment