SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using and not using $_POST for form variables

    Hey guys,

    I recently moved someone's site from our host to his own. The guy who originally wrote the code did not use $_POST for any of the form variables, and it worked fine on our host. But his host must not support this convenience because all the variables return empty strings unless they have the $_POST attached. Is there a way to get around this, or do I have to painstakingly go through every page and change the ones that need to be changed? php.net mentions using global $_POST; but that hasn't appeared to help. Any suggestions?

    He said his PHP is version 4.3.7.7.

    Cheers,
    C

  2. #2
    SitePoint Member
    Join Date
    Aug 2001
    Location
    Florida - USA
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would suspect that the issue is that the php directive register_globals is off. Which is the default from php 4.2 I believe.

    You can go to their php.ini and set this to on, but the best solution would be to update to use the predefined variables like $_SERVER,$_REQUEST,$_POST etc.

    Hope that helps.
    talkphp.com - come join the community.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, the register_globals is off. How do you update to use the predefined variables?

  4. #4
    I want my 4th arrow! garlinto's Avatar
    Join Date
    Jun 2002
    Location
    Riding the electron wave
    Posts
    372
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Drile's Friend
    Yeah, the register_globals is off. How do you update to use the predefined variables?
    It's easy. Even I can do it
    Replace each instance of:
    PHP Code:
    $HTTP_POST_VARS['someFormValue'
    ...with:
    PHP Code:
    $_POST['someFormValues'
    Same goes for:
    PHP Code:
    $HTTP_GET_VARS['someURLstring '
    ...replace with:
    PHP Code:
    $_GET['someURLstring '
    Go to this page at the PHP.net manual to learn more about the new predefined variables.
    Ducharme's Axiom: "If you view your problem closely
    enough, you will recognize yourself as part of the problem."


  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by garlinto
    It's easy. Even I can do it
    Replace each instance of:
    PHP Code:
    $HTTP_POST_VARS['someFormValue'
    ...with:
    PHP Code:
    $_POST['someFormValues'
    Same goes for:
    PHP Code:
    $HTTP_GET_VARS['someURLstring '
    ...replace with:
    PHP Code:
    $_GET['someURLstring '
    Go to this page at the PHP.net manual to learn more about the new predefined variables.
    Right, I understand that, but in our case the form variables have neither $_POST nor $HTTP_POST_VARS, they're just written as $someFormValue. Replacing each instance would be tedious because obviously some variables are not form variables.

    C

  6. #6
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh dear - I feel your pain - one of our developers wrote his old code like that. I had to go through it all and find the context the var was in an alter it to be $_POST or $_GET.

    It should actually be fairly easy to do, but admittedly painstaking
    if the data is in a script posted to from a form it's clearly $_POST otherwise probably a $_GET - or is it all vastly more complex than that...??

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Michigan
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I know the difference between $_POST and $_GET. I guess I'll just get some extra hours in this week.

    I thought using $_POST and $_GET was the old skool method and newer versions didn't need them. I guess I had it backwards >_< oh well.

    Thanks for the help.

    C

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    Leuven, Belgium
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At the top of each entry point page you can put:

    /* just list out all the parameters you need for the script to operate below */
    $desiredvars = array('this parameter', 'thatparameter', 'someotherparameter');
    foreach($desiredvars as $v){
    if(isset($_POST[$v])) $$v = $_POST[$v];
    }


    If you're a lazy, slovenly slacker, you can also just do one of the following, which in general mean lessening your script's security:

    - at the top of each script entry point, just put extract($_POST]);
    - or turn on register globals in an .htaccess file in the directory where the scripts are located which have this problem:
    php_flag register_globals 1

    If the parameters are consistenly the same name, you could 'auto prepend' a script which does something like the $desiredvars snippet, but just with the list of all the post variables used for the entire script. This is also done with an .htaccess file,
    php_value auto_prepend_file /full/path/to/thisfile.php


Bookmarks

Posting Permissions

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