Problem with $_POST variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • arun.kumar.varma@gmail.com

    Problem with $_POST variable

    Hi,

    I'm learning PHP and prepared a simple login form. validate.php does
    the validation and it was behaving erratically. So, I did var_dump of
    $_POST variable and it's NULL. Did I miss anything here regarding the
    configuration or code? Code for validate.php is given below.

    <?php
    var_dump($_POST );

    $user=$_POST['login'];
    $password=$_POS T['password'];

    // .... Rest of the validation ...
    ?>


    Any kind of help would be highly beneficial.

  • Micha³ Wo¼niak

    #2
    Re: Problem with $_POST variable

    One quick glance of an experienced eye allowed to understand the blurred
    and almost unreadable arun.kumar.varm a@gmail.com's handwriting:[color=blue]
    > I'm learning PHP and prepared a simple login form. validate.php does
    > the validation and it was behaving erratically. So, I did var_dump of
    > $_POST variable and it's NULL. Did I miss anything here regarding the
    > configuration or code? Code for validate.php is given below.
    >
    > <?php
    > var_dump($_POST );[/color]

    Well, if you DUMP the $_POST var here...
    [color=blue]
    > $user=$_POST['login'];
    > $password=$_POS T['password'];[/color]

    .... then obviously you won't get anything here. $_POST is dumped, so
    there is no such thing as $_POST[whatever]. Don't dump. :)

    Besides, you should check whether there is anything in those vars:
    if ( (isset($_POST['login'])) && (isset($_POST['password'])) ) then
    {
    // Validation here
    }
    else
    {
    // Error message ("No username/password supplied" for example) here
    }

    Cheers
    Mike

    Comment

    • arun.kumar.varma@gmail.com

      #3
      Re: Problem with $_POST variable

      My original code is
      <?php
      $user=$_POST['login'];
      $password=$_POS T['password'];

      // .... Rest of the validation ...
      ?>

      login and password fields were not empty when the form was submitted.
      But still, $_POST['login'] and $_POST['password'] were not set (I've
      confirmed it using isset() functions).

      Can someone help?

      Comment

      • Micha³ Wo¼niak

        #4
        Re: Problem with $_POST variable

        One quick glance of an experienced eye allowed to understand the blurred
        and almost unreadable arun.kumar.varm a@gmail.com's handwriting:
        [color=blue]
        > My original code is
        > <?php
        > $user=$_POST['login'];
        > $password=$_POS T['password'];
        >
        > // .... Rest of the validation ...
        > ?>
        >
        > login and password fields were not empty when the form was submitted.
        > But still, $_POST['login'] and $_POST['password'] were not set (I've
        > confirmed it using isset() functions).
        >
        > Can someone help?[/color]

        Could you also include:
        1. the form's HTML code
        2. The filenames of both files (the form's file and the validating file

        Comment

        • rich

          #5
          Re: Problem with $_POST variable

          arun.kumar.va.. .@gmail.com wrote:[color=blue]
          > login and password fields were not empty when the form was submitted.
          > But still, $_POST['login'] and $_POST['password'] were not set (I've
          > confirmed it using isset() functions).[/color]

          Sure your login and password fields in the form are named correctly? =\

          Comment

          • arun.kumar.varma@gmail.com

            #6
            Re: Problem with $_POST variable

            I'm really sorry guys. PHP version on my server is 4.0.6. Found it out
            with phpinfo()

            Came to know that $HTTP_POST_VARS is the variable to use for the
            versions earlier than 4.1.0

            Comment

            • Ramius

              #7
              Re: Problem with $_POST variable


              Michal Wozniak wrote:[color=blue][color=green]
              > > <?php
              > > var_dump($_POST );[/color]
              >
              > Well, if you DUMP the $_POST var here...
              >[color=green]
              > > $user=$_POST['login'];
              > > $password=$_POS T['password'];[/color]
              >
              > ... then obviously you won't get anything here. $_POST is dumped, so
              > there is no such thing as $_POST[whatever]. Don't dump. :)[/color]


              This is absolutely false.

              The "dump" in var_dump should be thought of as "dumping output to
              screen". It is analagous to print or echo. It does not unset the
              variable, or change it in any way.

              It is very useful to use var_dump in the way the original poster used
              it, because "print" would not work ( $_POST is an array ) and "print_r"
              would produce incomplete results ( because $_POST might contain nested
              arrays ). var_dump will echo all the contents of $_POST which is
              useful for testing purposes.

              Comment

              • Micha³ Wo¼niak

                #8
                Re: Problem with $_POST variable

                One quick glance of an experienced eye allowed to understand the blurred
                and almost unreadable Ramius's handwriting:
                [color=blue]
                > se var_dump in the way the original poster used
                > it, because "print" would not work ( $_POST is an array ) and "print_r"
                > would produce incomplete results ( because $_POST might contain nested
                > arrays ).  var_dump will echo all the contents of $_POST which is
                > useful for testing purposes.[/color]

                Oooops... Sorry, my mistake. :/
                I stand corrected.

                Regards
                Mike

                Comment

                • BKDotCom

                  #9
                  Re: Problem with $_POST variable

                  My "workaround " for this problem is something like:

                  put this code in any function that needs $_POST, $_GET, etc...
                  if ( !isset($_SERVER ) )
                  {
                  global $HTTP_POST_VARS ;
                  $_POST = &$HTTP_POST_VAR S;
                  // do this for any other superglobal $_whatevers you need
                  }

                  Comment

                  • Joe Estock

                    #10
                    Re: Problem with $_POST variable

                    arun.kumar.varm a@gmail.com wrote:[color=blue]
                    > Hi,
                    >
                    > I'm learning PHP and prepared a simple login form. validate.php does
                    > the validation and it was behaving erratically. So, I did var_dump of
                    > $_POST variable and it's NULL. Did I miss anything here regarding the
                    > configuration or code? Code for validate.php is given below.
                    >
                    > <?php
                    > var_dump($_POST );
                    >
                    > $user=$_POST['login'];
                    > $password=$_POS T['password'];
                    >
                    > // .... Rest of the validation ...
                    > ?>
                    >
                    >
                    > Any kind of help would be highly beneficial.
                    >[/color]
                    Odds are your problem is with your html. Double-check your variable
                    names in the html form as well as in your php source. Also, check your
                    form tag in your html. You may inadvertantly be using GET instead of
                    POST, in which case $_POST will contain nothing. Also, update to the
                    latest stable release of PHP. See below for an example.

                    test.html:
                    <form action="login.p hp" method="POST">
                    Username: <input type="text" name="user"><br >
                    Password: <input type="password" name="password" ><br>
                    <input type="submit" value="Submit">
                    </form>

                    login.php:
                    <?php
                    $user = isset($_POST['user']) ? $_POST['user'] : '';
                    $pass = isset($_POST['password']) ? $_POST['password'] : '';

                    echo 'Username: '.$user.'<br>Pa ssword: '.$pass;
                    ?>

                    Comment

                    • Jan Pieter Kunst

                      #11
                      Re: Problem with $_POST variable

                      Ramius wrote:[color=blue]
                      > "print_r"
                      > would produce incomplete results ( because $_POST might contain nested
                      > arrays ). var_dump will echo all the contents of $_POST which is
                      > useful for testing purposes.[/color]

                      print_r() prints nested arrays.

                      JP

                      --
                      Sorry, <devnull@cauce. org> is a spam trap.
                      Real e-mail address unavailable. 5000+ spams per month.

                      Comment

                      Working...