Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question PHP Redirection Page

    I've been searching around for a php redirection script based on a variable. Example: redirect.mysite.com/?url=sitehere.com&disclaimer=true
    If disclaimer was set to true it would say you are visiting a website not on our site, and false would do a redirect imeidiately. Haven't used php in awhile

  • #2
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    I'd check to see that both variables are set, then if disclaimer was set to true I'd use a <meta> tag, enabling you to delay the redirect, and inform the user. If it was set to false I'd just use header().

    PHP Code:
    if (isset($_GET['url']) && isset($_GET['disclaimer']) && ($_GET['disclaimer'] == "true")) {
         echo 
    'You are about to be redirected to a website not on our site...';
         echo 
    '<meta http-equiv="refresh" content="5; URL='.urlencode($_GET['url']).'">';
    }
    elseif (isset(
    $_GET['url']) && isset($_GET['disclaimer']) && ($_GET['disclaimer'] == "false")) {
         
    header ('location: ' urlencode($_GET['url'])); 

    Last edited by dan-dan; 05-28-2012 at 09:27 PM.

  • Users who have thanked dan-dan for this post:

    pawtracks (05-28-2012)

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dan-dan View Post
    I'd check to see that both variables are set, then if disclaimer was set to true I'd use a <meta> tag, enabling you to delay the redirect, and inform the user. If it was set to false I'd just use header().

    PHP Code:
    if (isset($_GET['url']) && isset($_GET['disclaimer']) && ($_GET['disclaimer'] == "true")) {
         echo 
    'You are about to be redirected to a website not on our site...';
         echo 
    '<meta http-equiv="refresh" content="5; URL='.urlencode($_GET['url']).'">';
    }
    elseif (isset(
    $_GET['url']) && isset($_GET['disclaimer']) && ($_GET['disclaimer'] == "false")) {
         
    header ('location: ' urlencode($_GET['url'])); 

    I put it into a php file on my site and it only displays the code as text. What did I do wrong? (My server understands php)
    Last edited by pawtracks; 05-28-2012 at 10:00 PM. Reason: Question

  • #4
    New Coder
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    31
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Did you surround the code in opening/closing PHP tags, and name the file with a php extension?

  • #5
    Regular Coder dan-dan's Avatar
    Join Date
    Aug 2009
    Location
    England
    Posts
    483
    Thanks
    22
    Thanked 79 Times in 78 Posts
    What kbduvall said. Btw, my code is a very simple example of a redirect.

    In a real situation you'd need to sanitize any input. Assuming that your redirect URL's are stored in a database, you'd probably want to perform a simple query to check the record exists too.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •