Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class User {
- private $username;
- private $password;
- public $ISLOGGEDIN = 'ISLOGGEDIN';
- ## Make a class function
- public function __construct() {
- $this->pdo = new PDO("mysql:host=localhost;dbname=login",'root','');
- }
- ## TJEK OM BRUGER EKSISTERE, return true, hvis ja
- private function userExists($username) {
- $this->username = $username;
- if ($stmt = $this->pdo->prepare("SELECT id FROM users WHERE username = :username")) {
- $stmt->bindParam(':username', $this->username);
- if ($stmt->execute()) {
- if ($stmt->rowCount() > 0) {
- return true;
- } else {
- return false;
- }
- } else {
- return true;
- }
- } else {
- return true;
- }
- }
- ## Opret ny bruger
- public function create($username, $password) {
- $this->username = $username;
- $this->password = $password;
- if (!$this->userExists($this->username)) {
- // Connect to database
- if ($stmt = $this->pdo->prepare("INSERT INTO users (username, password) VALUES (:username, :password)")) {
- $stmt->bindParam(':username', $this->username);
- $stmt->bindParam(':password', password_hash($this->password, PASSWORD_BCRYPT, ['cost' => 12]));
- if ($stmt->execute()) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- ## Get the latest userID
- public function getLastUserId() {
- $result = $this->pdo->query("SELECT id FROM users ORDER BY id DESC LIMIT 1");
- $row = $result->fetch(PDO::FETCH_ASSOC);
- return $row['id'];
- }
- ## Verify users
- public function verifyUser($username, $password) {
- $this->username = $username;
- $this->password = $password;
- if ($stmt = $this->pdo->prepare("SELECT id, password FROM users WHERE username = :username")) {
- $stmt->bindParam(':username', $this->username, PDO::PARAM_STR);
- if ($stmt->execute()) {
- $result = $stmt->fetch(PDO::FETCH_ASSOC);
- if (password_verify($this->password, $result['password'])) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- ## Get userID from users
- public function getUserId($username) {
- $this->username = $username;
- if ($stmt = $this->pdo->prepare("SELECT id FROM users WHERE username = :username")) {
- $stmt->bindParam(':username', $this->username, PDO::PARAM_STR);
- if ($stmt->execute()){
- $result = $stmt->fetch(PDO::FETCH_ASSOC);
- return $result['id'];
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- ## Login Method Filter Function
- public function methodCheck($method) {
- $requestMethod = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_SPECIAL_CHARS);
- if ($requestMethod === $method) {
- return TRUE;
- } else {
- return FALSE;
- }
- }
- ## Login Session
- public function doLogin($id) {
- if ($this->isSessionStarted() == false) {
- session_start();
- }
- $_SESSION[$this->ISLOGGEDIN] = TRUE;
- $_SESSION['userId'] = $id;
- }
- ## Token Generator
- private function generateToken() {
- if ($this->isSessionStarted() == false) {
- session_start();
- }
- $_SESSION['token'] = sha1(time()*rand(5,1000));
- $_SESSION['tokentime'] = time();
- }
- ## Get token
- public function getToken() {
- if ($this->isSessionStarted() == false) {
- session_start();
- }
- if(isset($_SESSION['token'])) {
- return $_SESSION['token'];
- } else {
- $this->generateToken();
- return $_SESSION['token'];
- }
- }
- ## Validate Token
- public function validateToken($token) {
- if ($this->isSessionStarted() == false) {
- session_start();
- }
- if ($token === $_SESSION['token']) {
- if ((time() - $_SESSION['tokentime']) > 120) {
- return FALSE;
- } else {
- return TRUE;
- }
- } else {
- return FALSE;
- }
- }
- ## Delete the token after use
- public function destroyToken(){
- if ($this->isSessionStarted() == false) {
- session_start();
- }
- if(isset($_SESSION['token']) && isset($_SESSION['tokentime'])) {
- unset($_SESSION['token']);
- unset($_SESSION['tokentime']);
- }
- }
- ## Is session started
- private function isSessionStarted() {
- if (php_sapi_name() !== 'cli') {
- if (version_compare(phpversion(), '5.4.0', '>=') ) {
- return session_status() === PHP_SESSION_ACTIVE ? true : false;
- } else {
- return session_id() === '' ? false : true;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement