PHP Header Redirect with registers_global = off

Discussion in 'PHP' started by ozgression, Nov 15, 2008.

  1. #1
    Hi All,

    I'm looking to use a php script, similar to the one discussed here to redirect visitors to different websites.

    Now, I can get this script working if I turn register_globals on, however, I hear there is security issues with this. So, can anyone advise me about how I can modify the following out.php file in order for it to work with register_globals off?

    <?php
    if ($m == "google") {$link = "http://www.google.com";}
    if ($m == "yahoo") {$link = "http://www.yahoo.com";}
    if ($m == "live") {$link = "http://www.live.com";}
    
    header("Location: $link"); // Jump to the url above
    exit();
    ?>
    Code (markup):
    Eg. the redirect URL would be /out.php?m=google

    Thanks in advance!
     
    ozgression, Nov 15, 2008 IP
  2. shineDarkly

    shineDarkly Banned

    Messages:
    241
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    just use $_GET to get the querystring

    ex: $m = $_GET['m'];
     
    shineDarkly, Nov 15, 2008 IP
  3. Bind

    Bind Peon

    Messages:
    70
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    an example of what shineDarly means:

    
    <?php
    if (isset($_GET['m']))
       {
          $m = $_GET['m'];
          if ($m == "google") {$link = "http://www.google.com";}
          if ($m == "yahoo") {$link = "http://www.yahoo.com";}
          if ($m == "live") {$link = "http://www.live.com";}
          header("Location: $link"); // Jump to the url above
    }
    ?>
    
    PHP:
    here is a version that allows for unlimited links

    
    <?php
    if (isset($_GET['m']))
        {
            $url=Array(
                        "google"=>"google.com",
                        "yahoo"=>"yahoo.com",
                        "live"=>"live.com",
                        );
            $m = $_GET['m'];
            foreach ($url as $this_m=>$this_url)
                {
                    if ($m == $this_m)
                        {
                            header("location: http://".$this_url."/");
                        }
                }
    }
    ?>
    
    PHP:
    or

    
    <?php
    if (isset($_GET['m']))
        {
            $url=Array(
                        "google.com",
                        "yahoo.com",
                        "live.com",
                        );
            $m = $_GET['m'];
            foreach ($url as $this_url)
                {
                    if ($m == substr($this_url,0,strlen($m)))
                        {
                            header("location: http://".$this_url."/");
                        }
                }
    }
    ?>
    
    PHP:
     
    Bind, Nov 15, 2008 IP
  4. ozgression

    ozgression Well-Known Member

    Messages:
    343
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    140
    #4
    Worked perfectly. Thank-you both... +rep given to both of you.
     
    ozgression, Nov 16, 2008 IP
  5. netproint

    netproint Peon

    Messages:
    334
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hey guys,

    I know this is a very old thread. Found it through Google and need help with modifying this script. This is what I wish to do:

    http ://site1/out.php?id=http ://site2/

    When the user clicks the above link, the script should simply redirect to http ://site2/

    For that matter, I tried to modify the script in out.php file like this:

    <?PHP
    $id = $_GET['id'];
    header("Location: $id");
    exit();
    ?>
    PHP:
    As per my understanding, $id variable pulls the information from id using GET. Then header should redirect to the link stored in $id variable. Although this works for simple urls like:

    http ://www .site1.com/out.php?id=http ://www .site2.com/

    But it doesn't work if there are more parameters in the link:

    http ://www .site1.com/out.php?id=http ://www .site2.com/a=10&b=11&c=12&d=13

    My purpose is to just make the outgoing links a part of my website. When the user clicks the link, the script should simply redirect to Site2.

    I do not wish to use the out.php too much and keep adding links to it, it will be very time consuming. So, what would you suggest?

    Appreciate any help you guys can provide.

    Regards.
     
    netproint, Nov 6, 2011 IP