Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require 'Database.class.php';
- $db = new Database();
- class Login {
- //array for error's messages
- private $errors = array();
- private $username;
- private $password;
- private $sql;
- public function __construct($username, $password) {
- $this->username = $username;
- $this->password = $password;
- $this->sql = "SELECT id, username, password FROM users WHERE username = :username";
- $this->userLogin();
- }
- private function userLogin() {
- $stmtX = $db->executeQuerry($this->username, $this->sql);
- $result = $stmtX->fetch(PDO::FETCH_ASSOC);
- if($result !== FALSE) {
- //compare the passwords
- $validPassword = password_verify($this->password, $result['password']);
- if ($validPassword == TRUE) {
- $_SESSION['user_id'] = $result['id'];
- $_SESSION['user'] = $result['username'];
- $_SESSION['now_logged'] = TRUE;
- $_SESSION['logged_in'] = time();
- header('Location: index.php');
- }
- else {
- //passwords don't match
- $stmt = NULL;
- $db = NULL;
- $result = NULL;
- array_push($this->errors, "Invalid password!");
- }
- }
- else {
- $stmt = NULL;
- $db = NULL;
- $result = NULL;
- array_push($this->errors, "Invalid username!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement