title: Not login whereas correct username and password in php
body: <p>I have a class called <code>User</code> and a <code>php</code> file called <code>login.php</code>.I didn't success when i enter <code>username</code> and <code>password</code> to login whereas these are correct.I guess authenticate function in <code>User</code> class is problematic.I put my code.</p>
  
  <pre><code>login.php
  &lt;?php
    require_once("../../includes/functions.php");
    require_once("../../includes/session.php");
    require_once("../../includes/database.php");
    require_once("../../includes/user.php");
    $message = "";
    if ($session-&gt;is_logged_in()) {
        redirect_to("index.php");
    }
    if (isset($_POST['submit'])) { // Form has been submitted.
       $username = trim($_POST['username']);
       $password = trim($_POST['password']);     
    // Check database to see if username/password exist.
    $found_user = User::authenticate($username, $password);
  
    if ($found_user) {
        echo "in the found_user&lt;br/&gt;";
        $session-&gt;login($found_user);
        redirect_to("index.php");
    } else {
        // username/password combo was not found in the database
        $message = "Username/password combination incorrect.";
    }
    } else { // Form has not been submitted.
             $username = "";
             $password = "";
           }
    ?&gt;
   &lt;?php include('../layouts/admin_header.php'); ?&gt;
       &lt;h2&gt;Staff Login&lt;/h2&gt;
  &lt;?php echo output_message($message); ?&gt;
  &lt;form action="login.php" method="post"&gt;
     &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;Username:&lt;/td&gt;
              &lt;td&gt;
              &lt;input type="text" name="username" maxlength="30" value="&lt;?php 
               echo htmlentities($username); ?&gt;" /&gt;
             &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;Password:&lt;/td&gt;
              &lt;td&gt;
                &lt;input type="password" name="password" maxlength="30" value="
                &lt;?php echo htmlentities($password); ?&gt;" /&gt;
              &lt;/td&gt;
         &lt;/tr&gt;
         &lt;tr&gt;
              &lt;td colspan="2"&gt;
                &lt;input type="submit" name="submit" value="Login" /&gt;
              &lt;/td&gt;
        &lt;/tr&gt;
     &lt;/table&gt;
  &lt;/form&gt;
  &lt;?php include('../layouts/admin_footer.php'); ?&gt;
  </code></pre>
  
  <p>and <code>user.php</code> file</p>
  
  <pre><code>class User{
  public static function find_by_sql($sql=""){
      global $database;
      $result_set=$database-&gt;query($sql);
      $object_array=array();
      while ($row=$database-&gt;fetch_array($result_set)) {
          $object_array[]=self::instantiate($row);
      }
      return $object_array;
  }
  public static function authenticate($username = "", $password = "") {
        global $database;
        $username = $database-&gt;escape_value($username);
        $password = $database-&gt;escape_value($password);
  
        $sql = "SELECT * FROM users ";
        $sql .= "WHERE username = '{$username}' ";
        $sql .= "AND password = '{$password}' ";
        $sql .= "LIMIT 1";
        $result_array = self::find_by_sql($sql);
        $str="in aout&lt;br/&gt;";
        return !empty($result_array) ? array_shift($result_array) : $str;
    }
  private static function instantiate($record){
      $object=new self;
      foreach ($record as $attribute=&gt;$value) {
          if($object-&gt;has_attribute($attribute)){
              $object-&gt;$attribute=$value;
          }
      }
      return $object;
  }
  private function has_attribute($attribute){
      $object_vars=get_object_vars($this);
      return array_key_exists($attribute,$object_vars);
   }
  }
  </code></pre>