Undefined index and Undefined variable errors

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cyberlei
Forum Commoner
Posts: 27
Joined: Tue Oct 16, 2007 6:19 pm
Location: Milpitas, CA

Undefined index and Undefined variable errors

Post by cyberlei »

Hi all,

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

Could someone please tell me where I did wrong? Here is the Code, Thanks a lot

Code: Select all

 
<?php
 
function confirmUser($username, $password)
{
 
   global $conn;
   /* Add slashes if necessary (for query) */
   if(!get_magic_quotes_gpc()) {
    $username = addslashes($username);
   }
 
   /* Verify that user is in database */
   $q = "select Password from users where upper(User_Login)=upper('$username') ";
   $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_array($result);
   $dbarray['Password']  = stripslashes($dbarray['Password']);
   $password = stripslashes($password);
 
   /* Validate that password is correct */
   if(strtoupper($password) == strtoupper($dbarray['Password'])){
      return 0; //Success! Username and password confirmed
   }
   else{
      return 2; //Indicates password failure
   }
}
 
function checkLogin()
{
 
   /* Username and password have been set */
   if(isset($_SESSION['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($_POST['user']) > 15)
       $message= "Sorry,Username Cannot Be Longer Than 15 Characters.";
   else 
   {
  //$md5pass = md5($_POST['pass']);
   $md5pass = $_POST['pass'];
   $result = confirmUser($_POST['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"action="" 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">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>
 
<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>
 
<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.form1.user.value=='')
        form1.user.focus();
else if(document.form1.pass.value=='')
        form1.pass.focus();
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;
?>
 
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Undefined index and Undefined variable errors

Post by Christopher »

Better code for those text input values would something like:

Code: Select all

<?php echo isset($_POST['user']) ? htmlspecialchars($_POST['user']) : ''; ?>
(#10850)
cyberlei
Forum Commoner
Posts: 27
Joined: Tue Oct 16, 2007 6:19 pm
Location: Milpitas, CA

Re: Undefined index and Undefined variable errors

Post by cyberlei »

arborint wrote:Better code for those text input values would something like:

Code: Select all

<?php echo isset($_POST['user']) ? htmlspecialchars($_POST['user']) : ''; ?>
Thanks, problem solved. following code also works :drunk:

Code: Select all

 
<?php echo (isset($_POST['user'])) ? $_POST['user'] : ""; ?>
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Undefined index and Undefined variable errors

Post by RobertGonzalez »

Even better would be setting those as default variables early in the script:

Code: Select all

<?php
$user = isset($_POST['user']) ? $_POST['user'] : null;
// ... carry on ...
?>
Post Reply