Created
July 26, 2023 22:44
-
-
Save vihugarcia/e9acee6a4fe63ca7b8830d7ffd6b317f to your computer and use it in GitHub Desktop.
Chapter 09 UserController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\controllers; | |
use SimpleMVC\core\Controller as Controller; | |
use App\models\User as User; | |
use SimpleMVC\core\View as View; | |
use SimpleMVC\core\db as db; | |
class UserController extends Controller { | |
private $user; | |
private $view; | |
private $db; | |
public function __construct() | |
{ | |
$this->user = new User('users'); | |
$this->view = new View('users'); | |
} | |
public function register() | |
{ | |
if ($_SERVER["REQUEST_METHOD"] == 'POST') { | |
$data = [ | |
'username' => $_POST["username"], | |
'password' => password_hash($_POST["password"], PASSWORD_BCRYPT), | |
'email' => $_POST["email"] | |
]; | |
$this->user->load($data); | |
$this->user->save(); | |
header('Location: ' . SITE_BASE . 'site/index/'); | |
exit; | |
} | |
$this->view->setAction('register'); | |
$this->view->set('user', $this->user); | |
$this->view->render(); | |
} | |
public function login() | |
{ | |
session_id(APP_SESSION_ID); | |
session_start(); | |
if (isset($_SESSION["username"])) { | |
header('Location: '.SITE_BASE.'site/index'); | |
exit; | |
} | |
if ($_SERVER["REQUEST_METHOD"] == 'POST' && isset($_POST["username"])) { | |
$this->db = new db(CONFIG); | |
$conn = $this->db->getConnection(); | |
$stmt = $conn->prepare("SELECT password FROM users WHERE TRIM(username) = :username"); | |
$stmt->bindParam(":username", $_POST["username"]); | |
$stmt->execute(); | |
$stmt->setFetchMode(\PDO::FETCH_ASSOC); | |
$result = $stmt->fetch(); | |
if (empty($result)) { | |
$msg = "Wrong username or password"; | |
$this->view->setAction('login'); | |
$this->view->set('msg', $msg); | |
$this->view->render(false); | |
} else { | |
if (password_verify($_POST["password"], $result["password"])) { | |
session_id(APP_SESSION_ID); | |
session_start(); | |
$_SESSION["username"] = $_POST["username"]; | |
$_SESSION["login_time_stamp"] = time(); | |
header('Location: '.SITE_BASE.'site/index'); | |
exit; | |
} else { | |
$msg = "Wrong username or password"; | |
$this->view->setAction('login'); | |
$this->view->set('msg', $msg); | |
$this->view->render(false); | |
} | |
} | |
exit; | |
} else { | |
$this->view->setAction('login'); | |
$this->view->render(); | |
} | |
} | |
public function logout() | |
{ | |
if ($_SERVER["REQUEST_METHOD"] == 'POST') { | |
session_id(APP_SESSION_ID); | |
session_start(); | |
session_unset(); | |
session_destroy(); | |
header('Location: '.SITE_BASE.'user/login'); | |
exit; | |
} else { | |
header('Location: '.SITE_BASE.'site/index'); | |
exit; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment