Vercas
Vercas

Reputation: 9141

Detect and change external links in PHP?

I have a free PHP webserver and I would like to provide a redirect to external links page, just like deviantart.com does. Is there any way to do this with just PHP? I have no access to the server.

Edit: I meant a page asking "Are you sure you want to leave [MA WEBSITE]? NOPE ; DUH - GO TO http://outside-example.com"

Edit2: I actually meant a function to catch outside links and replace them with a /redirect/?url=PARSED_URL_ADDRESS

Upvotes: 2

Views: 1771

Answers (4)

Sandeep Manne
Sandeep Manne

Reputation: 6092

You need to detect if there is any link which redirects to outside website then you need a page to show something like "Now Leaving yourwebsite.com"

If that is the case then you need to analyze the content of your page before rendering and find out if there is any tags and replace ref of them with some gatway.php?url=outgoing-url

Where in gateway.php compare if the url belongs to your website or external website by using string comparison methods

Use this js code in footer (I am expecting there is some common footer page)

var urls = document.getElementsByTagName("a");
for (urlIndex in urls ) {
   urls[urlIndex].href = "dummy.php?url="+urls[urlIndex].href; //replace dummy.php with urs
}

Upvotes: 1

bigblind
bigblind

Reputation: 12867

The best way to do it is using the location header, but you also need to set a 301 response code, this also tells the search engines crawling the link that the content at that url is at a different location, and it's a best practice to set the response code for redirects in general.

Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" ); 

Upvotes: 0

sinni800
sinni800

Reputation: 1449

Provide, for example, a function that creates <a> tags. Or just one that converts URLs to your redirector: redirect.php?url=http://.... The redirector then issues a HTTP header called "refresh" set to the new address.. Beautify it so the user knows he is being redirected, voilá.

Find out yourself how :)

Upvotes: 0

tdammers
tdammers

Reputation: 20721

You mean like header('Location: http://www.example.com/');?

Upvotes: 1

Related Questions