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.

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.

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

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’.

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.