0

i have a database that stores the url example facebook.com i dispalyed the value in my table and i posted the same value in a href

            <a href="<?php echo $url['url']; ?>" target="_blank">
            <?php echo $fet_2['urlname']; ?></a>

so when i click the urll is opening in new tab like domain.COM/folder/folder/www.facebook.com and the fb page is not loaded. i want to load the fb page when i click the name. please help me

Pascal Belloncle
  • 11,184
  • 3
  • 56
  • 56
srinivasankanna
  • 1,333
  • 2
  • 9
  • 9

2 Answers2

1

I would think that you are missing the http:// from the URL, and that's why you are directed to inside the domain looking for that page.

You can solve it like this.

<a href="http://<?php echo $url['url']; ?>" target="_blank">
Achrome
  • 7,773
  • 14
  • 36
  • 45
1

As I mentioned in my comment, make sure the href has an "http://" in the URL. If not, the site will try to open in your current domain.

If some of the URLs in your database have the http://, then you need to do a check before prepending. Use something like this:

How to add http:// if it's not exists in the URL?

function addhttp($url) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

Good luck.

Community
  • 1
  • 1
sgeddes
  • 62,311
  • 6
  • 61
  • 83
  • `magnet://` ;) `\w+://` would be more flexible, but this will probably cover all the cases OP has in mind anyway. – mpen Feb 07 '13 at 02:32