Page 1 of 1

links without "http://"

Posted: Fri Aug 14, 2009 3:22 am
by annish
Hi,

I have a list of links in my db that I want to list.
These are links that users add to db, so I cannot know if they have written links with "http://".
Why is it so that links starting with "www." gets the whole path in front?
www.google.com in example under, wil be linked as localhost/mypage/www.google.com.

Of course I could do a check when user adds a link, and add "http://" if they don't, but for now I just want somebody to explain why this is so :)

Code: Select all

while($row = mysql_fetch_array($result))
{
    echo '<a href="' . $row['link'] . '">' . $row['subject'] . '</a><br />';
}

Re: links without "http://"

Posted: Fri Aug 14, 2009 3:58 am
by turbolemon
These links are interpreted as relative to the current directory, as unix directory names can contain most characters.

Code: Select all

while($row = mysql_fetch_array($result))
{
   if (strpos($row['link'],'http://') === false) $url = "http://{$row['link']}";
 
   echo '<a href="' . $url . '">' . $row['subject'] . '</a><br />';
}

Re: links without "http://"

Posted: Fri Aug 14, 2009 4:03 am
by annish
Thank you so much for the answer.

That was a good start for my forum-life here.

Re: links without "http://"

Posted: Fri Aug 14, 2009 5:41 am
by turbolemon
No problem :). Just a quick thought I had would be that it might be a good idea to add the http:// bit when the link is submitted, as opposed to when you are outputting them. It would make your script that little bit more efficient!