To co dotychczas nabazgrałem:
<?php require_once 'config.php'; require_once 'class.mysql.php'; class User { private $id; private $username; private $password; private $email; private $db; public function __construct($db) { $this->db = $db; } public function __destruct() { $this->db = null; } public function register($username, $password, $email) { if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { throw new Exception('Email is not valid!', 1); } { throw new Exception('Password is not valid', 2); } { throw new Exception('Username is not valid!', 3); } // Checking for exists user $checkExistsUser = 'SELECT `id` FROM `' . USERS_TABLE_NAME . '` WHERE `username`=:username;'; $sth = $this->db->prepare($checkExistsUser); $sth->bindValue(':username', $username, PDO::PARAM_STR); $sth->execute(); if($sth->rowCount() > 0) { throw new Exception('Someone have got account with this username!', 5); } else { $password = hash('sha256', $password); // Insert new user to database $insertNewUser = 'INSERT INTO `' . USERS_TABLE_NAME . '` (`id`, `username`, `password`, `email`) VALUES (NULL, :username, :password, :email);'; $sth = $this->db->prepare($insertNewUser); $sth->bindValue(':username', $username, PDO::PARAM_STR); $sth->bindValue(':password', $password, PDO::PARAM_STR); $sth->bindValue(':email', $email, PDO::PARAM_STR); $checkSuccess = $sth->execute(); if($checkSuccess > 0) { throw new Exception('Register successfully!', 6); $this->id = $_SESSION['id'] = $this->db->lastInsertId(); $this->username = $_SESSION['username'] = $username; $this->password = $password; $this->email = $_SESSION['email'] = $email; } else { throw new Exception('Register failed!', 7); } } } // Najbardziej interesuje mnie jak wyglądać ma ta funkcja... public function login($username, $password) { { throw new Exception('Password is not valid', 2); } { throw new Exception('Username is not valid!', 3); } if($this->username == $username && $this->password == $password) { throw new Exception('Login successfully!', 8); } else { throw new Exception('Login failed!', 9); } } } ?>
I jeszcze jedno pytanie. Czy rzucanie wyjątków mogę wykorzystwać również do sytuacji wcale nie wyjątkowych? Takie coś działa, lecz chciałem się dowiedzieć czy nie jest to mało profesjonalne.