Redirecting to external urls through an internal page

Discussion in 'PHP' started by punkris, Oct 30, 2010.

  1. #1
    Hello guys,

    I'm new to PHP programming and I would like your help in performing a little task on my site.

    I have a Wordpress blog and I make posts which consist of some content and some links to external sites. I would like those links to pass through an internal page before letting the users access the external site.

    For example: I have some links to on my post to external sites. The visitor clicks a link and it gets him to an internal page of my blog where there is a button like "Access the content" and when he clicks it it redirects him to the external site.

    If you would like to see exactly what I mean, here's a link to site that has exactly this functionality I'm looking for: http://watch-series.com/episode/90210_s2_e14-53025.html (I'm not yet allowed to put a link). So, if you click on one of those links will get you to an internal page where you can push a button to direct you on an external site.

    Hope I explained it well. Please, if you have any guidance that may help me achieve this, I'll really appreciate.

    Thank you in advance,
    Chris
     
    punkris, Oct 30, 2010 IP
  2. Coranti

    Coranti Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I would recommend to create a redirect.php. Which you can call as:

    h-ttp://www.yoursite.com/redirect.php?url=1

    In here you do the following:



    <?
    if ($_GET['id'] == 1 {

    $url = "h-ttp://www.externalsite.com";

    }

    ?>

    <a href="<?php echo $url; ?>">Access the content</a>


    Of course you can extend your ifloop with more ID's and add more HTML to the page.

    I hope this helps!
     
    Coranti, Oct 30, 2010 IP
  3. punkris

    punkris Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Coranti,

    Thanks for replying. I tried to use your code, but can't get it to work. Right now, my redirect.php page looks like this:

    And I tried to access the page by this: h-ttp://www.mysite.net/redirect.php?url=1 and it just gives "Error 404". Am I missing something?
     
    punkris, Oct 30, 2010 IP
  4. Coranti

    Coranti Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I hope you changed externalsite and mysite to the appropiate URL's?
    Also did you remove the - in the URL's? (they are there because I'm not allowed to make a link)
     
    Coranti, Oct 30, 2010 IP
  5. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Just use JavaScript. Don't bother filtering URLs with PHP or needing XSS scripts or whatnot.

    Just visit /whatever-file-you-want-to-name-this.html?url=http://www.example.com
    Don't need to make new if/else statements. No security issues.

    <!doctype html>
    <html lang="en">
    <head>
    	<title>You are now leaving example.com.</title>
    	<style type="text/css">div{margin:0 auto}</style>
    </head>
    <body>
    <div id="container">
    <p>You are now leaving example.com.</p>
    <p>Your destination is: <span id="u"></span>.<br>Different terms of use and privacy policies may apply.</p>
    <p><button onclick="_redirect();">Click here to continue to external site</button></p>
    </div>
    <script type="text/javascript">
    	function _onload() {
    		var r = 'http://www.example.com/';
    		if (location.href.match(/url=([^&]+)/)) {var r = RegExp.$1;}
    		else if(location.href.match(/urlesc=([^&]+)/)) {var r = unescape(RegExp.$1);}
    		try{document.getElementById('u').innerHTML=r;}catch(e){}
    	}
    	function _redirect(){
    		if (location.href.match(/url=([^&]+)/)) {var r = RegExp.$1;}
    		else if(location.href.match(/urlesc=([^&]+)/)) {var r = unescape(RegExp.$1);}
    		location.href = r;
    	}
    	window.onload = _onload();
    </script>
    </body>
    HTML:
     
    krsix, Oct 30, 2010 IP
  6. punkris

    punkris Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes, I did all that and it didn't work. I'm gonna try also with JavaScript, as krsix suggested. Thank you both for trying to help.

    LE: krsix, I've tried your code, however I don't know if I did it correctly, because I can't get it to work. What I did, I created a page redirect.html where I've put the code and in place of example.com I've put my site's address.
    I tried to verify the functionality by accessing mysite.com/redirect.html?url='external_site.com' and I still get a 404. Using Wordpress blogging platform, is it possible that I may have to add other coding in other pages? If you have any ideas on how to get this to work... will be much appreciated. I feel bad I'm such a noob at this :(
     
    Last edited: Oct 31, 2010
    punkris, Oct 31, 2010 IP
  7. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ?url=http://external-site.com, not external-site.com
     
    krsix, Oct 31, 2010 IP