Kod klasy
<?php class Auth { private $username; private $password; private $salt; private $pdo; public $message; public function __construct() { require_once ('config.php'); $this-> pdo= new PDO ($config['dsn'], $config['user'], $config['password']); } public function get_data() { $stmt= $this->pdo->prepare ('SELECT username,password,salt FROM users WHERE username=:username ORDER BY id'); $stmt-> bindValue (':username', $_POST['username'], PDO::PARAM_STR); $stmt-> execute(); while ($row= $stmt-> fetch()) { $this-> username= $row['username']; $this-> password= $row['password']; $this-> salt= $row['salt']; } $stmt-> closeCursor(); } public function login($username, $password) { if ($this-> username == $username && $this-> password == $this->code($password, $this->salt)) { $_SESSION['user']= TRUE; $this-> message= 'Welcome , ' . '<a href="auth.php?signout=true">Sign out</a>'; } else { $this-> message= 'Invalid username/password'; } } public function signout() { if ($_GET['signout'] == TRUE) { } } public function signup($username, $password, $email, $salt) { { if ($this-> valid_email($_POST['email']) && $this->email_exists($_POST['email'])) { $stmt= $this->pdo->prepare ('INSERT INTO users (username,password,email,salt) VALUES (:username,:password,:email,:salt) '); $stmt-> bindvalue (':salt', $salt,PDO::PARAM_STR); $stmt-> execute(); $this-> message= 'Account has been created.'; $stmt-> closeCursor(); } } else { $this-> message= 'Fill in all fields.'; } } private function email_exists($email) { $stmt= $this->pdo->prepare ('SELECT email FROM users WHERE email=:email'); $stmt-> bindValue (':email', $_POST['email'], PDO::PARAM_STR); $stmt-> execute(); if ($stmt-> fetch() > 0) { $this-> message= 'This email address exists.'; } else { return true; } } private function valid_email($email) { { return true; } else { $this-> message= 'E-mail is invalid.'; } } public function salt($pass) { } public function code($pass, $salt) { } public function show() { return $this-> message; } } $user= new Auth; $user-> get_data(); $user-> login($_POST['username'],$_POST['password']); $user-> signout(); ?>