Undefined index and Undefined variable errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cyberlei
    New Member
    • Sep 2007
    • 28

    Undefined index and Undefined variable errors

    hi all,

    I`m getting this error
    Notice: Undefined index: user in c:\inetpub\wwwr oot\login.php on line 96
    Notice: Undefined variable: message in c:\inetpub\wwwr oot\login.php on line 101

    Could someone please tell me where I did wrong? Here is the Code, Thanks a lot
    [code=php]
    <?php

    function confirmUser($us ername, $password)
    {

    global $conn;
    /* Add slashes if necessary (for query) */
    if(!get_magic_q uotes_gpc()) {
    $username = addslashes($use rname);
    }

    /* Verify that user is in database */
    $q = "select Password from users where upper(User_Logi n)=upper('$user name') ";
    $result = mysql_query($q, $conn);
    if(!$result || (mysql_numrows( $result) < 1)){
    return 1; //Indicates username failure
    }

    /* Retrieve password from result, strip slashes */
    $dbarray = mysql_fetch_arr ay($result);
    $dbarray['Password'] = stripslashes($d barray['Password']);
    $password = stripslashes($p assword);

    /* Validate that password is correct */
    if(strtoupper($ password) == strtoupper($dba rray['Password'])){
    return 0; //Success! Username and password confirmed
    }
    else{
    return 2; //Indicates password failure
    }
    }

    function checkLogin()
    {

    /* Username and password have been set */
    if(isset($_SESS ION['username']) && isset($_SESSION['password']))
    {
    /* Confirm that username and password are valid */
    if(confirmUser( $_SESSION['username'], $_SESSION['password']) != 0)
    {
    /* Variables are incorrect, user not logged in */
    unset($_SESSION['username']);
    unset($_SESSION['password']);
    return false;
    }
    return true;
    }
    /* User not logged in */
    else
    return false;
    }

    function displayLogin()
    {
    $end=false;
    if(isset($_POST['sublogin']))
    {
    $_POST['user'] = trim($_POST['user']);
    if(!$_POST['user'] || !$_POST['pass'])
    $message= "You Didn't Fill In All The Required Field.";
    else if(strlen($_POS T['user']) > 15)
    $message= "Sorry,User name Cannot Be Longer Than 15 Characters.";
    else
    {
    //$md5pass = md5($_POST['pass']);
    $md5pass = $_POST['pass'];
    $result = confirmUser($_P OST['user'], $md5pass);
    $user=$_POST['user'];
    if($result == 1)
    $message= 'That User ID Doesn\'t Exist.';
    else if($result == 2)
    $message= 'Incorrect Password, Please Try Again.';
    else
    {
    $_POST['user'] = stripslashes($_ POST['user']);
    $_SESSION['username'] = $_POST['user'];
    $_SESSION['password'] = $md5pass;
    // header( 'refresh: 5; url=/main.php/' );
    echo "<META HTTP-EQUIV=Refresh CONTENT='0; URL=main.php'>" ;
    $end=true;


    }
    }

    }
    if(!$end)
    {
    ?>
    <form name="form1"act ion="" method="post">
    <table width="130" border="0" align="left" cellpadding="3" cellspacing="0" >
    <tr>
    <td colspan="2"><h3 align="center"> ULS Inventory Login</h3></td>
    </tr>
    <tr><td ><div align="left">Us er ID:</div></td><td width="100"><in put name="user" type="text" onkeypress="ret urn checkenter(wind ow.event.keyCod e)" value="<?php echo $_POST['user'] ?>" size="17" maxlength="20"> </td></tr>

    <tr><td><div align="left">Pa ssword:</div></td><td><input name="pass" type="password" size="17" maxlength="20" onkeypress="ret urn checkenter(wind ow.event.keyCod e)"></td></tr>

    <tr><td colspan="2" align="CENTER"> <input type="submit" name="sublogin" value="Login"></td></tr>
    <tr><td colspan="2" align="center"> <?php echo $message?></td></tr>

    </table>
    </form>
    <script type="text/javascript">
    if(document.for m1.user.value== '')
    form1.user.focu s();
    else if(document.for m1.pass.value== '')
    form1.pass.focu s();
    else
    form1.sublogin. focus();
    function checkenter($key )
    {
    if ($key==13 && document.form3. qty.value>0)
    document.form1. submit();
    }
    </script>
    <?php

    }
    }
    $logged_in = checkLogin();
    $_SESSION['logged_in']=$logged_in;
    ?>
    [/code]
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    This means you are calling an index in an array which doesnt exist (user) the $_POST['user'] hasnt been set.

    Same applies for the variable, youre calling a variable that doesnt exist.

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      These are not errors, just notices. To check before outputtinng it, you can do this:
      [php]<?php echo (isset($_POST['user'])) ? $_POST['user'] : ""; ?>[/php]
      Ronald

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        In your form:
        Originally posted by cyberlei
        [HTML]<div align="left">Us er ID:</div></td><td width="100"><in put name="user" type="text" onkeypress="ret urn checkenter(wind ow.event.keyCod e)" value="<?php echo $_POST['user'] ?>" size="17" maxlength="20">[/HTML]
        I am confused at how you have <?php echo $_POST['user'] ?> as the value, but nothing has been posted at this stage? This is the input? Maybe I have missed something?

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Originally posted by TheServant
          In your form:


          I am confused at how you have <?php echo $_POST['user'] ?> as the value, but nothing has been posted at this stage? This is the input? Maybe I have missed something?
          I noticed that also, but probably he hasn't shown all the code. Naughty!

          Ronald

          Comment

          Working...