SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with echo URL being added to current page URL

    Hello, I am using the below code to retrieve a user submitted URL from a database if one exists and turn their username into a link.

    But for some reason it is adding the url to the current pages url. As so:

    "www.currentsite.com/www.targetsite.com"

    I've had a look and the common theme seems to be that if the URL lacks "http://" then the above is what you get. Add "http://" and it works fine giving you "www.targetsite.com".

    I'm guessing that it has something to do with formatting when putting it into and retrieving it from the DB? I've had the problem before but I cannot remember how I fixed it so help is appreciated.

    Code PHP:
    if(isset($_POST['website'])) { $url=mysql_real_escape_string($_POST['website']);} else {$url = '';}

    I've tried this line with and without stripslashes but not gotten anywhere.
    Code PHP:
    $userurl=($commentData['author_url']);

    Code PHP:
    <?php 
    							if(trim($userurl) != '') 
    								{ 
    									echo "<a href='".$userurl."' target='_blank' rel='nofollow'>".$username."</a></b>"; 
    								}
    							else 
    								{
    									echo $username."</b>"; 					
    								}
     
    									echo $comment; ?>

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    if(strpos($userurl'http')!==0) {
        
    $userurl 'http://' $userurl;

    If you don't add http:// to the beginning of the URL, browsers assume it's an URL on your own server, hence it tries to load "www.currentsite.com/www.targetsite.com"

    Using the (very simple) snippet of code I posted above, you can automatically add http:// to all URLs that don't start with 'http'.

  3. #3
    SitePoint Zealot darkwarrior's Avatar
    Join Date
    Dec 2010
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a strange default action to take, especially if www. is there but thank you very much for that, it will help a lot.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •