Why is my session variable not already set the first time

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

    #1

    Why is my session variable not already set the first time

    If I run the following PHP script the session variable auth_ok changes to
    true resp. false only if I press the button 1 resp. 2 twice.

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

    <?php
    session_start() ;

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

    echo "Username: " . $_POST['user'] . "<br>";
    echo "Password: " . $_POST['pass'] . "<br>";
    echo "Button 1: " . $_POST['button1'] . "<br>";
    echo "Button 2: " . $_POST['button2'] . "<br>";
    echo "Authentication : " . $_SESSION['auth_ok'] . "<br>";

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

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

    <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 says
    [24-Apr-2005 21:48:09] PHP Notice: Undefined index: button1 in
    D:\WWW\php\_log in-sisi\mytest.php on line 10
    or
    [24-Apr-2005 21:48:28] PHP Notice: Undefined index: button2 in
    D:\WWW\php\_log in-sisi\mytest.php on line 11
    depending on the button I pressed.

    What's wrong with my PHP script?
    Stefan


  • Janwillem Borleffs

    #2
    Re: Why is my session variable not already set the first time

    Stefan Mueller wrote:[color=blue]
    > If I run the following PHP script the session variable auth_ok
    > changes to true resp. false only if I press the button 1 resp. 2
    > twice.[/color]

    First you echo the values and then you are processing the input. See where
    it goes wrong?

    Turning things around would solve your problem:

    <?php
    session_start() ;

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

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

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

    echo "Username: " . $_POST['user'] . "<br>";
    echo "Password: " . $_POST['pass'] . "<br>";
    echo "Button 1: " . @$_POST['button1'] . "<br>";
    echo "Button 2: " . @$_POST['button2'] . "<br>";
    echo "Authentication : " . $_SESSION['auth_ok'] . "<br>";
    ?>

    Also note that in this revised snippet:

    * Notices are suppressed with the @ sign
    * True prints 1, false prints nothing.


    JW



    Comment

    • Stefan Mueller

      #3
      Re: Why is my session variable not already set the first time

      Yes, of course. Thanks a lot for your hint.
      Now my first PHP script is running. Cool!

      Thanks to all of you.
      Stefan


      "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
      news:426c0387$0 $16826$dbd45001 @news.euronet.n l...[color=blue]
      > Stefan Mueller wrote:[color=green]
      >> If I run the following PHP script the session variable auth_ok
      >> changes to true resp. false only if I press the button 1 resp. 2
      >> twice.[/color]
      >
      > First you echo the values and then you are processing the input. See where
      > it goes wrong?
      >
      > Turning things around would solve your problem:
      >
      > <?php
      > session_start() ;
      >
      > if (!isset($_SESSI ON['auth_ok'])) {
      > $_SESSION['auth_ok'] = false;
      > }
      >
      > if (isset($_POST['button1'])){
      > echo "Button 1 has been pressed<br>";
      > $_SESSION['auth_ok'] = true;
      > }
      >
      > if (isset($_POST['button2'])){
      > echo "Button 2 has been pressed<br>";
      > $_SESSION['auth_ok'] = false;
      > }
      >
      > echo "Username: " . $_POST['user'] . "<br>";
      > echo "Password: " . $_POST['pass'] . "<br>";
      > echo "Button 1: " . @$_POST['button1'] . "<br>";
      > echo "Button 2: " . @$_POST['button2'] . "<br>";
      > echo "Authentication : " . $_SESSION['auth_ok'] . "<br>";
      > ?>
      >
      > Also note that in this revised snippet:
      >
      > * Notices are suppressed with the @ sign
      > * True prints 1, false prints nothing.
      >
      >
      > JW
      >
      >
      >[/color]


      Comment

      Working...