If one creates a link to a page 'x.php' (e.g. <a href="x.php">), then the resolved address points to that page on the same website. To link to another website, you provide the full address, (e.g. <a href="http://www.saos.org/x.php">).
But if you provide www.saos.org or saos.org (no www) for the href attribute, the browsers don't transform it into an external link, quite logically so. It will point to yoursite.com/www.saos.org
This was something I came across while fixing up commenting on {ego}trips. If one put a sitename without http:// it would show up as a link local to our website, which is NOT welcome. There is nobody to blame, though.
Fixing this was simple, though. I just appended a http:// to those which were missing it, using PHP's parse_url() function.
Note: haven't checked, or tried to append www to the links without it.
//here is a sample list of links of varying types.
$urls = array('www.google.com', 'http://www.yahoo.com', 'microsoft.com', 'www.saos.org/x.php');
//here is where we transform, then output them
foreach($urls as $url) {
$parsed = parse_url($url);
echo '<a href="';
if(empty($parsed['scheme']))
echo 'http://';
echo $url;
echo '">'.$url.'</a><br />';
} //end of foreach loop
?>
No comments:
Post a Comment