By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Undefined index and Undefined variable errors

P: 28
hi all,

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

Could someone please tell me where I did wrong? Here is the Code, Thanks a lot
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. function confirmUser($username, $password)
  4. {
  5.  
  6.    global $conn;
  7.    /* Add slashes if necessary (for query) */
  8.    if(!get_magic_quotes_gpc()) {
  9.     $username = addslashes($username);
  10.    }
  11.  
  12.    /* Verify that user is in database */
  13.    $q = "select Password from users where upper(User_Login)=upper('$username') ";
  14.    $result = mysql_query($q,$conn);
  15.    if(!$result || (mysql_numrows($result) < 1)){
  16.       return 1; //Indicates username failure
  17.    }
  18.  
  19.    /* Retrieve password from result, strip slashes */
  20.    $dbarray = mysql_fetch_array($result);
  21.    $dbarray['Password']  = stripslashes($dbarray['Password']);
  22.    $password = stripslashes($password);
  23.  
  24.    /* Validate that password is correct */
  25.    if(strtoupper($password) == strtoupper($dbarray['Password'])){
  26.       return 0; //Success! Username and password confirmed
  27.    }
  28.    else{
  29.       return 2; //Indicates password failure
  30.    }
  31. }
  32.  
  33. function checkLogin()
  34. {
  35.  
  36.    /* Username and password have been set */
  37.    if(isset($_SESSION['username']) && isset($_SESSION['password']))
  38.    {
  39.       /* Confirm that username and password are valid */
  40.       if(confirmUser($_SESSION['username'], $_SESSION['password']) != 0)
  41.       {
  42.          /* Variables are incorrect, user not logged in */
  43.          unset($_SESSION['username']);
  44.          unset($_SESSION['password']);
  45.          return false;
  46.       }
  47.       return true;
  48.    }
  49.    /* User not logged in */
  50.    else
  51.       return false;
  52. }
  53.  
  54. function displayLogin()
  55. {
  56. $end=false;    
  57. if(isset($_POST['sublogin']))
  58. {
  59.     $_POST['user'] = trim($_POST['user']);
  60.    if(!$_POST['user'] || !$_POST['pass'])
  61.          $message= "You Didn't Fill In All The Required Field.";
  62.    else if(strlen($_POST['user']) > 15)
  63.        $message= "Sorry,Username Cannot Be Longer Than 15 Characters.";
  64.    else 
  65.    {
  66.   //$md5pass = md5($_POST['pass']);
  67.    $md5pass = $_POST['pass'];
  68.    $result = confirmUser($_POST['user'], $md5pass);
  69. $user=$_POST['user'];
  70.    if($result == 1)
  71.        $message= 'That User ID Doesn\'t Exist.';
  72.    else if($result == 2)
  73.           $message= 'Incorrect Password, Please Try Again.';
  74.    else
  75.    {
  76.            $_POST['user'] = stripslashes($_POST['user']);
  77.            $_SESSION['username'] = $_POST['user'];
  78.            $_SESSION['password'] = $md5pass;
  79.      // header( 'refresh: 5; url=/main.php/' );
  80.     echo "<META HTTP-EQUIV=Refresh CONTENT='0; URL=main.php'>";
  81.     $end=true;    
  82.  
  83.  
  84.    }
  85.    }
  86.  
  87. }
  88. if(!$end)
  89. ?>
  90. <form name="form1"action="" method="post">
  91. <table width="130" border="0" align="left" cellpadding="3" cellspacing="0">
  92. <tr>
  93.   <td colspan="2"><h3 align="center">ULS Inventory Login</h3></td>
  94.   </tr>
  95. <tr><td ><div align="left">User ID:</div></td><td width="100"><input name="user" type="text" onkeypress="return checkenter(window.event.keyCode)" value="<?php echo $_POST['user'] ?>" size="17" maxlength="20"></td></tr>
  96.  
  97. <tr><td><div align="left">Password:</div></td><td><input name="pass" type="password" size="17" maxlength="20" onkeypress="return checkenter(window.event.keyCode)"></td></tr>
  98.  
  99. <tr><td colspan="2" align="CENTER"><input type="submit" name="sublogin" value="Login"></td></tr>
  100. <tr><td colspan="2" align="center"><?php echo $message?></td></tr>
  101.  
  102. </table>
  103. </form>
  104. <script type="text/javascript">
  105. if(document.form1.user.value=='')
  106.         form1.user.focus();
  107. else if(document.form1.pass.value=='')
  108.         form1.pass.focus();
  109. else 
  110.         form1.sublogin.focus();
  111. function checkenter($key)
  112. {
  113.     if ($key==13 && document.form3.qty.value>0)
  114.         document.form1.submit();
  115. }                
  116. </script>
  117. <?php
  118.  
  119.  }
  120. $logged_in = checkLogin();
  121. $_SESSION['logged_in']=$logged_in;
  122. ?>
  123.  
Mar 12 '08 #1
Share this Question
Share on Google+
4 Replies


Markus
Expert 5K+
P: 6,050
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.
Mar 12 '08 #2

ronverdonk
Expert 2.5K+
P: 4,258
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
Mar 12 '08 #3

TheServant
Expert 100+
P: 1,168
In your form:
[HTML]<div align="left">User ID:</div></td><td width="100"><input name="user" type="text" onkeypress="return checkenter(window.event.keyCode)" 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?
Mar 12 '08 #4

ronverdonk
Expert 2.5K+
P: 4,258
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
Mar 12 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.