1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Redirecting External Links

Discussion in 'PHP & Perl' started by sp33dygonzal3s, Dec 3, 2010.

  1. sp33dygonzal3s

    sp33dygonzal3s Newbie

    Joined:
    Jul 21, 2010
    Messages:
    39
    Likes Received:
    4
    Hi everyone. I've been trying to figure out how to redirect external links sort of like the way BHW does it but have come up with no success. Is there a way to:

    1) Send all external links to an internal php page
    2) Notify where the user is going
    3) Send them off.

    Thanks!
     
  2. Crazy

    Crazy Jr. Executive VIP

    Joined:
    Jun 13, 2009
    Messages:
    640
    Likes Received:
    319
    Occupation:
    VB, C#, XHTML, CSS, PHP, MySQL, JavaScript, jQuery
    Location:
    Everywhere
    Can you explain a bit more what you mean? Seems relatively easy but I don't want to explain something if I'm not on the same page as you.
     
  3. sirgold

    sirgold Supreme Member

    Joined:
    Jun 25, 2010
    Messages:
    1,260
    Likes Received:
    645
    Occupation:
    Busy proving the Pareto principle right
    Location:
    A hot one
    What you're looking for is probably this:

    Code:
    header( 'Location: http://www.new.com/new_page.html' );
    Of course you can use a GET query string parameter if you want to redirect to a new location passed via the URL...

    Code:
    http://www.w3schools.com/PHP/php_get.asp
    Also, if the redirection is not a static, hard-coded URL you might want to implement a few security checks.

    P.S. Almsost forgot... You might want to add some javascript to do the tracking you were mentioning in your OP.
     
    • Thanks Thanks x 1
  4. majjed2008

    majjed2008 Registered Member

    Joined:
    Apr 7, 2009
    Messages:
    65
    Likes Received:
    6
    Hi

    I am intersting also if is there any products to add it to fourm to do that , and to hide my site from the site user will visit .

    Thankx
     
  5. sp33dygonzal3s

    sp33dygonzal3s Newbie

    Joined:
    Jul 21, 2010
    Messages:
    39
    Likes Received:
    4
    Good input so far guys.

    So a typical scenario I'm thinking of is a user is on my website (specifically a wiki) and clicks on an external link. I want to send them to a page on my website that displays the url address that they are going to briefly before sending them off to the external website. However, before the URL is displayed I want to be able to check the link for security purposes and modify it if necessary. If possible, I want to be able to keep the original page as the referrer to the new website.
     
  6. Crazy

    Crazy Jr. Executive VIP

    Joined:
    Jun 13, 2009
    Messages:
    640
    Likes Received:
    319
    Occupation:
    VB, C#, XHTML, CSS, PHP, MySQL, JavaScript, jQuery
    Location:
    Everywhere
    On the PHP side of things, you'd be looking to do something like this.

    Create a file, out.php

    Within out.php, you'd do something like this

    Code:
    <?php
      if(isset($_GET['s']) && $_GET['s'] != '') {
        $target_url = $_GET['s'];
        // do whatever you desire w/ $target_url such as cleaning it
        // storing it in a DB, etc (if you store it, sanitize it first)
        // also if magicquotes is enabled you'll need to strip slashes
        // before referencing it below
      } else {
        // error handling, such as returning to previous URL
      }
    ?>
    <html>
       <head>
     [FONT=monospace]   <script type="text/javascript">
         <!--
         function redirect() {
           window.location = "<?= $target_url ?>";
         }
         //-->
       </script>
    [/FONT]  </head>
       <body onload="setTimeout('redirect()', 5000)"> <!-- 5 second delay -->
         You're about to be redirected to the following URL:<br />
         <br />
         <strong><?= $target_url ?></strong>
       </body>
     </html>
    Now, if you want to set the previous page as the referrer, we'd have to handle it a bit differently. Also, you'll need to forcefully prepend "http://www.yoursite.com/out.php?s=" to all active hyperlinks on your pages.
     
    • Thanks Thanks x 1
    Last edited: Dec 4, 2010
  7. sp33dygonzal3s

    sp33dygonzal3s Newbie

    Joined:
    Jul 21, 2010
    Messages:
    39
    Likes Received:
    4
    wow that was exactly what I was looking for my man. Perfect. Now I just need your help finding out how to prepend all hyperlinks on mediawiki and using the previous page as a referrer. I really appreciate it!
     
  8. Crazy

    Crazy Jr. Executive VIP

    Joined:
    Jun 13, 2009
    Messages:
    640
    Likes Received:
    319
    Occupation:
    VB, C#, XHTML, CSS, PHP, MySQL, JavaScript, jQuery
    Location:
    Everywhere
    PM me