Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once 'Database.class.php';
- class Auth extends Database
- {
- const DATEFORMAT = 'd/m/Y';
- const TIMEFORMAT = 'H:i:s';
- public function __construct($host, $username, $password, $database)
- {
- parent::__construct($host, $username, $password, $database);
- $this->connect();
- $this->setTable('users');
- }
- public function insert(array $data)
- {
- $username = $data['username'];
- $password = $data['password'];
- $repeat_password = $data['repeat_password'];
- $last_login = date(self::DATEFORMAT . ' ' . self::TIMEFORMAT);
- if ($password == $repeat_password)
- {
- $password = hash("sha256", $password);
- $stmt = $this->connection->prepare("INSERT INTO {$this->table} (username, password, last_login) VALUES(:username, :password, :last_login)");
- $stmt->execute(array(':username' => $username,
- ':password' => $password,
- ':last_login' => $last_login
- )
- );
- }
- else
- {
- // do something
- }
- }
- public function select(array $data)
- {
- $username = $data['username'];
- $password = $data['password'];
- $password = hash("sha256", $password);
- $stmt = $this->connection->prepare("SELECT * FROM {$this->table} WHERE username=:username AND password=:password LIMIT 1");
- $stmt->execute(array(':username' => $username,
- ':password' => $password
- )
- );
- return $stmt->fetch(PDO::FETCH_ASSOC);
- }
- }
- <?php
- require_once 'Auth.class.php';
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $auth = new Auth('localhost', 'root', '', 'oop');
- $user = $auth->select($_POST);
- if (count($user) === 0) {
- echo 'User not found!';
- }else{
- echo 'Hello, ' . ucfirst($user['username']);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement