Search code examples
phpexternal-links

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


Solution

  • 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
    }