Member Avatar

Hi everyone, happy new year!!!

I am trying to add some extra "security" to my website. I would like all links that do not link to my website to visit an "out" page that will tell the user that they are being taken to an external domain.

Would I be able to do this with PHP alone or will I need to use some JavaScript as well?

Many thanks.

Recommended Answers

I assume you'd want to use a confirm dialog for this, so you'd need JS, not PHP.
I don't know what you mean by 'security' though.

If your external site links have soemthing like class="ext" in them you could use js to handle the clicks:

e.g.
using …

Jump to Post

Have alook at parse_url(): http://www.php.net/manual/en/function.parse-url.php

That shows you how to extract the host - you can compare the link with your own host - if it's different use the redirect.

Why …

Jump to Post

All 8 Replies

Member Avatar

I assume you'd want to use a confirm dialog for this, so you'd need JS, not PHP.
I don't know what you mean by 'security' though.

If your external site links have soemthing like class="ext" in them you could use js to handle the clicks:

e.g.
using jquery

$('a.ext').click(function(){
  var ask = confirm("This link will take you to an external site. Do you wish to proceed?")
  if (ask){
     window.location = $(this).attr('href');
  }
});

Not tested

Member Avatar

I would rather not do it that way. As a lot of the links are from people who post things on my website and I don't want to manually add the

class="ext"

to each link. I would also like it to detect external links and take them to a page like "out.php?url=http://www.google.com"

Same to YOu.

i dont understand what you mean. When you talk of links it my be a link behind a button or <a> tag or you mean all the links on your site shouldn't go to external web page may be through redirection, actually i understand can give an example.

Same to YOu.

i dont understand what you mean. When you talk of links it my be a link behind a button or <a> tag or you mean all the links on your site shouldn't go to external web page may be through redirection, actually i understand can give an example.

Member Avatar

Ok sorry,

On my website I have a page where people can post content and some of them post URLs. Quite a lot of the time the URLs that they post take people to external domains. For example someone might post a link to a facebook page.

When the link is posted it would be like a normal link.

<a href="http://www.facebook.com/pagename">http://www.facebook.com/pagename</a>

I would like it so that when someone posts a link to a page that is not on my website the link would be like the following.

<a href="http://www.mydomain.com/external.php?redirect=http://www.facebook.com/pagename">http://www.facebook.com/pagename</a>

If someone posted a link to a page that is on my website then it should be displayed like a normal link

<a href="http://www.mydomain.com/attachment.php?id=1234">http://www.mydomain.com/attachment.php?id=1234</a>
Member Avatar

Have alook at parse_url(): http://www.php.net/manual/en/function.parse-url.php

That shows you how to extract the host - you can compare the link with your own host - if it's different use the redirect.

Why do you need to do this?

Member Avatar

Ok, thanks!

Sometimes users post content that may not be suitable or could cause harm to the users computer so I just want to make a page that warns the user that they are being taken to page that isn't on my website.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.