Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Getting error password and username not matching:

my class function is

PHP
public function login(array $data)
       {
          $_SESSION['logged_in']=FALSE;
          if(!empty($data))
          {
              $trim_data=array_map('trim',$data);
              $email=mysqli_real_escape_string($this->mysqli,$trim_data['email']);
              $password=mysqli_real_escape_string($this->mysqli,$trim_data['password']);
              if((!$email)||(!$password))
              {
                  throw new Exception("User name or password is missing");
              }
              $password=md5($password);
              $sql2="select id,uname,email from users where email='$email' and upass='$password'";
              $result = mysqli_query($this->mysqli, $sql2);
              $data = mysqli_fetch_assoc($result);
              $count = mysqli_num_rows($result);
              mysqli_close($this->mysqli);
              if( $count == 1){
               $_SESSION = $data;
               $_SESSION['logged_in'] = TRUE;
               return true;
           }else{
               throw new Exception( LOGIN_FAIL );
           }
        }
       }



my login page is:

XML
<?php
    ob_start();
    session_start();
   include('Crud_class.php');
    include('message.php');
    ?>
<?php
    if(!empty($_POST))
    try{
       $user=new users('localhost','root','ricky','mydatabase');
       $data=$user->login($_POST);
     if(isset($_SESSION['logged_in']) && $_SESSION['logged_in'])
       {
          header('Location:home1.php');
       }
    }catch(Exception $e)
    {
    $error=$e->getMessage();
    }
    if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']){
        header('Location: home1.php');
    }
?>
<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
<body>
    <?php include 'templates/message.php'?>
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" name="login_form">
        <label>Email:</label><br>
        <input type="email" name="email"><br>
        <label>Password:</label><br>
        <input type="password" name="password" id="password"><br>
        <input type="submit" name="submit" value="Login">
    </form>
    <p>Not registered?<a href="register.php">Click to register</a></p>
</body>
</html>



This is my table:

SQL
CREATE TABLE IF NOT EXISTS `users` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`uname` varchar(100) NOT NULL,

`email` varchar(150) NOT NULL,

`upass` varchar(255) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
select * from users;





Please help me why this is not selecting the correct username and password
Even i am using the correct username and password
Posted
Updated 5-Feb-15 7:50am
v2
Comments
Rakesh 00 5-Feb-15 13:54pm    
This part is not working
$sql2="select id,uname,email from users where email='$email' and upass='$password'";
$result = mysqli_query($this->mysqli, $sql2);
$data = mysqli_fetch_assoc($result);
$count = mysqli_num_rows($result);
mysqli_close($this->mysqli);
if( $count == 1){
$_SESSION = $data;
$_SESSION['logged_in'] = TRUE;
return true;
}else{
throw new Exception( LOGIN_FAIL );
}
please help me getting mad now
Richard Deeming 5-Feb-15 14:20pm    
You are storing passwords in plain-text. That's a very bad idea. You should only ever store a salted hash of the password:

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
Sergey Alexandrovich Kryukov 5-Feb-15 15:00pm    
First and foremost, never store a password anywhere and never pass it through the network. This is absolutely not needed for authentication. Cryptographics hash function should be used instead (don't mix it up with encryption of passwords or anything).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900