jamiet757 Posted November 18, 2009 Share Posted November 18, 2009 I am in need of a little PHP code help, on my website, you can view a users' profile page, and along with other information, their website is shown. Now for obvious reasons, I wanted to make the link open in a new window, as well as be rel=nofollow. Here is the problem: The original script had been automatically placing http:// before the url, so if the user put http:// in the website field in their profile, the link would look like this: http://http//www.link.com When I removed the http:// from the code, I started running into problems. Here is the code in the .php file: $userbox=eregi_replace("\{WEBSITE\}",$rs->row["website"],$userbox); and here is the code in the template file that I have modified, but still to no avail: <div style="margin-bottom:3px"><b>{WORD_WEBSITE}:</b> <a href="{WEBSITE}">{WEBSITE}</a></div> (I know the nofollow and target tags are missing, but this is just an example of the problem.) Now when I look at the source code of the page, it looks correct, i.e. <a href="www.link.com">www.link.com</a> But for some reason, unknown to me, my site's url is pasted before their url, i.e. the link will go to http://www.mysite.com/users/www.theirsite.com If you want to see the actual site, PM me. Any help is appreciated. I don't understand how the source code could be correct, but when you click the link it goes to another location (or even if you hover over the link you can see that it is wrong) Quote Link to comment https://forums.phpfreaks.com/topic/182002-php-code-help/ Share on other sites More sharing options...
premiso Posted November 18, 2009 Share Posted November 18, 2009 <a href="www.link.com"> That is not a proper link, as it is not internal to your site HTML will auto add an extra http://www.yourcurrentsite.com/wwww.link.com To fix this, you need to add the http:// to their site. It should look like: <a href="http://www.link.com"> If it does, it will work just fine. Add that in your code to the html tag and it will be just dandy! <div style="margin-bottom:3px"><b>{WORD_WEBSITE}:</b> <a href="http://{WEBSITE}">{WEBSITE}</a></div> That code should do what you want it to. Quote Link to comment https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-959985 Share on other sites More sharing options...
jamiet757 Posted November 18, 2009 Author Share Posted November 18, 2009 Well that is why I edited it in the first place. If a user puts http:// in their website, the resulting link is http://http//www.link.com Is there a way to quickly check to see if the user has entered http:// and either remove it, or warn the user not to enter http://? Quote Link to comment https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-959991 Share on other sites More sharing options...
Bricktop Posted November 18, 2009 Share Posted November 18, 2009 Hi jamiet757, The link is chagning to http://www.mysite.com/users/www.theirsite.com because you're not placing the http:// in front of the URL, therefore the browser is interpreting it as a local file/link. In the past I've used the following function to add http:// to posted website addresses. If the user forgets to add http:// it will be added, if they include it then it will not be added again (if that makes sense): function addHttp($post) { $url = $post; $protocols = array( 'http://', 'https://', 'ftp://' ); $found = false; foreach ($protocols as $value) { if (strpos($url, $value) !== false) { $found = true; break; } } if (!$found) {$url = 'http://' . $url;} return $url; } Call it by doing something like: addHttp({WEBSITE}); Although, it would be better to use this function on the user posted data so it is stored correctly in the first place. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-959992 Share on other sites More sharing options...
premiso Posted November 18, 2009 Share Posted November 18, 2009 Yes, there is. When the data is input simply do a $_POST['website'] = str_replace("http://", "", strtolower($_POST['website'])); On a side note I would avoid using eregi_replace as the ereg functions are being (have been) depreciated in newer PHP versions. Quote Link to comment https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-959994 Share on other sites More sharing options...
jamiet757 Posted November 18, 2009 Author Share Posted November 18, 2009 Great thanks Bricktop, that looks like exactly what I was looking for! Quote Link to comment https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-959996 Share on other sites More sharing options...
JonnoTheDev Posted November 18, 2009 Share Posted November 18, 2009 Bit more efficient <?php function addHttp($url) { if(!preg_match('%^https?://%',trim($url))) { $url = "http://" . $url; } return $url; } $url = "www.google.com"; $url = addHttp($url); print $url; ?> Quote Link to comment https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-960038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.