Strange Variable Passing Problem

Discussion in 'PHP' started by honey, Mar 19, 2006.

  1. #1
    I have a php page to which I pass a variable like:

    www.domain.com/test.php?id=1

    Now, the file test.php includes nothing but

    <?
    include("config.php");
    header("Location: http://www.redirect.com/main.php?domainID=$id");
    ?>
    PHP:
    technically, this should work, and it does. The problem is that it works on one hosting company, not the other. Now, there is a problem somewhere. Can anybuide help me/guide me and tell me what I should ask/tell to my hosting company so that this works.
     
    honey, Mar 19, 2006 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    For that to work, you need "register_globals" on in php. To fix it, use the $_GET super global variable.
    
    <?
    header( 'Location: http://www.redirect.com/main.php?domainID='.$_GET['id']."\n");
    exit;
    ?> 
    PHP:
    Note that according to the HTTP spec, the Location header needs a newline char at the end. (but most browsers accept it anyway.)
    Also, after the header call, you can add an exit or die, so php exits immediatly.
     
    exam, Mar 19, 2006 IP
    honey likes this.
  3. honey

    honey Prominent Member

    Messages:
    15,555
    Likes Received:
    712
    Best Answers:
    0
    Trophy Points:
    325
    #3
    Thanks a ton exam. I really appreciate your time and effort. It did solve the problem. Can you shoot me your paypal ID, for a small donation I would like to send as a thank you for your time.
     
    honey, Mar 19, 2006 IP
  4. samsam

    samsam Peon

    Messages:
    647
    Likes Received:
    53
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Honey, add the following in your .htaccess and you should be fine too.
    Your version of php has register_globals turned off, so you can turn it on just by adding this in your .htacces.

    php_flag register_globals on

    I hope that helps.
     
    samsam, Mar 21, 2006 IP
  5. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Except that it's not a very recommendable practice to have register_globals turned on. :) But that is a life saver especially when you're dealing with legacy php code that relies on register_globals, until you can get the code updated.
     
    exam, Mar 21, 2006 IP
  6. samsam

    samsam Peon

    Messages:
    647
    Likes Received:
    53
    Best Answers:
    0
    Trophy Points:
    0
    #6
    exam, can you please guide why "Except that it's not a very recommendable practice to have register_globals turned on.". I have it on.
     
    samsam, Mar 21, 2006 IP
  7. adstracker

    adstracker Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Because it's not safe. Anyone can manipulate variables within their script with the address bar.
    This can lead to SQL injection if you using sql or other nasty things such as your script getting read or deleted. ( Depends on how your script looks like ).
    I would definitely turn it OFF.
     
    adstracker, Mar 21, 2006 IP
  8. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Actually, there's nothing unsafe about having register_globals "on" if you *always* initialize variables before using them. BUT, There is a lot of old code out there that doesn't initialize variables before using them, plus, why clutter up the global namespace with a bunch of unneeded variable and as adstracker says, allow anyone to set variables in your script using the address bar.
     
    exam, Mar 21, 2006 IP
  9. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #9
    All the reasons adstracker stated are why PHP has been released with register_globals OFF by default for a while now... and also why the .htaccess trick might not work depending on how your web host has set things up.
     
    sketch, Mar 21, 2006 IP
  10. neroux

    neroux Active Member

    Messages:
    566
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    60
    #10
    http://www.php.net/manual/en/security.globals.php
     
    neroux, Mar 22, 2006 IP