Jump to content

Recommended Posts

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)

Link to comment
https://forums.phpfreaks.com/topic/182002-php-code-help/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-959985
Share on other sites

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://?

Link to comment
https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-959991
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-959992
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182002-php-code-help/#findComment-959994
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.