PHP variables are always empty

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Mueller

    #1

    PHP variables are always empty

    I'm trying to write a PHP script which allows to enter an username and a
    password and to click two buttons.
    If I start my PHP scripts 'mytest.php' I see the two input fields and the
    two buttons. However if I enter something to the input fields and press a
    button the echo commands show nothing.

    ======

    PHP script 'mytest.php'
    -----------------------

    <?php
    session_start() ;

    if (!isset($_SESSI ON['auth_ok'])) {
    $_SESSION['auth_ok'] = false;
    }

    echo "$user <br>";
    echo "$pass <br>";
    echo "$button1 <br>";
    echo "$button2 <br>";
    echo "$auth_ok <br>";

    if (isset($button1 )){
    echo "Button 1 has been pressed<br>";
    $auth_ok = true;
    }

    if (isset($button2 )){
    echo "Button 2 has been pressed<br>";
    $auth_ok = true;
    }
    ?>

    <html>
    <body>
    <form name="loginform " method="post" action="mytest. php">
    Username: <input type="text" name="user" size="30" maxlength="30"> <br>
    Passwort: <input type="password" name="pass" size="30"
    maxlength="30"> <br>
    <input type="submit" name="button1" value="Button 1">
    <input type="submit" name="button2" value="Button 2">
    </form>
    </body>
    </html>

    ======

    The PHP error logs shows the following errors:
    [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: user in
    D:\WWW\php\myte st.php on line 8
    [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: pass in
    D:\WWW\php\myte st.php on line 9
    [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: button1 in
    D:\WWW\php\myte st.php on line 10
    [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: button2 in
    D:\WWW\php\myte st.php on line 11
    [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: auth_ok in
    D:\WWW\php\myte st.php on line 12

    I'm very confused because my colleague told me that the same script on his
    machine is running without any problems.
    Has someone any idea what's wrong with my script or my server?

    Stefan


  • BKDotCom

    #2
    Re: PHP variables are always empty

    Stefan Mueller wrote:[color=blue]
    > PHP script 'mytest.php'
    > -----------------------
    >
    > <?php
    > session_start() ;
    >
    > if (!isset($_SESSI ON['auth_ok'])) {
    > $_SESSION['auth_ok'] = false;
    > }
    >
    > echo "$user <br>";
    > echo "$pass <br>";
    > echo "$button1 <br>";
    > echo "$button2 <br>";
    > echo "$auth_ok <br>";[/color]

    Your collegue is using a machine configured with register_global s on
    (not reccomended) and hasn't been the default setting for quite some
    time.
    use this instead:
    echo $_POST['user']."<br>";
    echo $_POST['pass']."<br>";
    echo $_POST['button1']."<br>";
    echo $_POST['button2']."<br>";
    echo $_POST['auth_od']."<br>";
    // etc...

    I could be wrong, but there's a good chance both buttons will be set..
    use if ( !empty($_POST['button1']) ) instead
    [color=blue]
    >
    > ?>
    >
    >
    > I'm very confused because my colleague told me that the same script[/color]
    on his[color=blue]
    > machine is running without any problems.
    > Has someone any idea what's wrong with my script or my server?
    >
    > Stefan[/color]

    Comment

    • Marcus

      #3
      Re: PHP variables are always empty

      Stefan Mueller wrote:[color=blue]
      > I'm trying to write a PHP script which allows to enter an username and a
      > password and to click two buttons.
      > If I start my PHP scripts 'mytest.php' I see the two input fields and the
      > two buttons. However if I enter something to the input fields and press a
      > button the echo commands show nothing.
      >
      > ======
      >
      > PHP script 'mytest.php'
      > -----------------------
      >
      > <?php
      > session_start() ;
      >
      > if (!isset($_SESSI ON['auth_ok'])) {
      > $_SESSION['auth_ok'] = false;
      > }
      >
      > echo "$user <br>";
      > echo "$pass <br>";
      > echo "$button1 <br>";
      > echo "$button2 <br>";
      > echo "$auth_ok <br>";
      >
      > if (isset($button1 )){
      > echo "Button 1 has been pressed<br>";
      > $auth_ok = true;
      > }
      >
      > if (isset($button2 )){
      > echo "Button 2 has been pressed<br>";
      > $auth_ok = true;
      > }
      > ?>
      >
      > <html>
      > <body>
      > <form name="loginform " method="post" action="mytest. php">
      > Username: <input type="text" name="user" size="30" maxlength="30"> <br>
      > Passwort: <input type="password" name="pass" size="30"
      > maxlength="30"> <br>
      > <input type="submit" name="button1" value="Button 1">
      > <input type="submit" name="button2" value="Button 2">
      > </form>
      > </body>
      > </html>
      >
      > ======
      >
      > The PHP error logs shows the following errors:
      > [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: user in
      > D:\WWW\php\myte st.php on line 8
      > [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: pass in
      > D:\WWW\php\myte st.php on line 9
      > [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: button1 in
      > D:\WWW\php\myte st.php on line 10
      > [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: button2 in
      > D:\WWW\php\myte st.php on line 11
      > [22-Apr-2005 19:02:17] PHP Notice: Undefined variable: auth_ok in
      > D:\WWW\php\myte st.php on line 12
      >
      > I'm very confused because my colleague told me that the same script on his
      > machine is running without any problems.
      > Has someone any idea what's wrong with my script or my server?
      >
      > Stefan
      >
      >[/color]

      Do you have register_global s on or off in your ini file? If it is off
      (as it should be) you will have to access your form variables like this:

      echo $_POST['user'] . "<br>";
      echo $_POST['$pass'] . "<br>";

      etc. etc.

      Comment

      • Stefan Mueller

        #4
        Re: PHP variables are always empty

        First of all thanks a lot for your answers. I appreciate that really very
        much.

        However, I still have some Questions:

        1) Why is the variable auth_ok still always empty?

        2) Why do I have the following error messages in the PHP error log depending
        on the button I pressed before?
        [23-Apr-2005 23:14:28] PHP Notice: Undefined index: button1 in
        D:\WWW\php\myte st.php on line 10
        [23-Apr-2005 23:14:29] PHP Notice: Undefined index: button2 in
        D:\WWW\php\myte st.php on line 11

        ============

        <?php
        session_start() ;

        if (!isset($_SESSI ON['auth_ok'])) {
        $_SESSION['auth_ok'] = false;
        }

        echo $_POST['user'] . "<br>";
        echo $_POST['pass'] . "<br>";
        echo $_POST['button1'] . "<br>";
        echo $_POST['button2'] . "<br>";
        echo $_SESSION['auth_ok'] . "<br>";

        if (isset($_POST['button1'])){
        echo "Button 1 has been pressed<br>";
        $auth_ok = true;
        }

        if (isset($_POST['button2'])){
        echo "Button 2 has been pressed<br>";
        $auth_ok = true;
        }
        ?>

        <html>
        <body>
        <form name="loginform " method="post" action="mytest. php">
        Username: <input type="text" name="user" size="30" maxlength="30"> <br>
        Passwort: <input type="password" name="pass" size="30"
        maxlength="30"> <br>
        <input type="submit" name="button1" value="Button 1">
        <input type="submit" name="button2" value="Button 2">
        </form>
        </body>
        </html>

        ============


        Comment

        • Tim Roberts

          #5
          Re: PHP variables are always empty

          "Stefan Mueller" <seekware-remove-@yahoo.com> wrote:
          [color=blue]
          >First of all thanks a lot for your answers. I appreciate that really very
          >much.
          >
          >However, I still have some Questions:
          >
          >1) Why is the variable auth_ok still always empty?[/color]

          Because $auth_ok and $_SESSION['auth_ok'] are two entirely different
          variables. You didn't finish fixing your code. Perhaps you want
          $auth_ok = $_SESSION['auth_ok'];
          after that first "if" statement.
          [color=blue]
          >2) Why do I have the following error messages in the PHP error log depending
          >on the button I pressed before?
          > [23-Apr-2005 23:14:28] PHP Notice: Undefined index: button1 in
          >D:\WWW\php\myt est.php on line 10
          > [23-Apr-2005 23:14:29] PHP Notice: Undefined index: button2 in
          >D:\WWW\php\myt est.php on line 11[/color]

          Because "button1" will only be present in the POST data if the user pressed
          button1. In that case, "button2" will not be defined at all, and the
          reference to $_POST['button2'] is a mistake.
          --
          - Tim Roberts, timr@probo.com
          Providenza & Boekelheide, Inc.

          Comment

          • Stefan Mueller

            #6
            Re: PHP variables are always empty

            Yes, you're right.
            Now my first PHP script is running. Cool!

            Thanks a lot for your help.
            Stefan


            "Tim Roberts" <timr@probo.com > wrote in message
            news:r38o61dpd8 f59rabc14akmfe2 t3icnadcd@4ax.c om...[color=blue]
            > "Stefan Mueller" <seekware-remove-@yahoo.com> wrote:
            >[color=green]
            >>First of all thanks a lot for your answers. I appreciate that really very
            >>much.
            >>
            >>However, I still have some Questions:
            >>
            >>1) Why is the variable auth_ok still always empty?[/color]
            >
            > Because $auth_ok and $_SESSION['auth_ok'] are two entirely different
            > variables. You didn't finish fixing your code. Perhaps you want
            > $auth_ok = $_SESSION['auth_ok'];
            > after that first "if" statement.
            >[color=green]
            >>2) Why do I have the following error messages in the PHP error log
            >>depending
            >>on the button I pressed before?
            >> [23-Apr-2005 23:14:28] PHP Notice: Undefined index: button1 in
            >>D:\WWW\php\my test.php on line 10
            >> [23-Apr-2005 23:14:29] PHP Notice: Undefined index: button2 in
            >>D:\WWW\php\my test.php on line 11[/color]
            >
            > Because "button1" will only be present in the POST data if the user
            > pressed
            > button1. In that case, "button2" will not be defined at all, and the
            > reference to $_POST['button2'] is a mistake.
            > --
            > - Tim Roberts, timr@probo.com
            > Providenza & Boekelheide, Inc.[/color]


            Comment

            Working...