Create php MVC app. Registration
In fact we have finished to develop our php mvc app. Next lesson I will realize your wishes. Today we will make registration in our application
Code lesson (main.tpl.php)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $pageData['title']; ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/css/bootstrap.min.css">
<link rel="stylesheet" href="/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<header>
</header>
<div id="content">
<div class="container-fluid table-block">
<div class="row table-cell-block">
<div class="col-sm-6 col-md-6">
<h1 class="text-center login-title">Вход</h1>
<div class="account-wall">
<img class="profile-img" src="/images/user-login.png" alt="">
<form method="post" class="form-signin" id="form-signin" name="form-signin">
<input type="hidden" name="action" value="login">
<?php if(!empty($pageData['loginError'])) :?>
<p><?php echo $pageData['loginError']; ?></p>
<?php endif; ?>
<input type="text" name="login" class="form-control" id="login" placeholder="Логин" required autofocus>
<input type="password" name="password" class="form-control" id="password" placeholder="Пароль" required>
<input type="submit" class="btn btn-lg btn-primary btn-block" value="Вход"/>
</form>
</div>
</div>
<div class="col-sm-6 col-md-6">
<h1 class="text-center login-title">Регистрация</h1>
<div class="account-wall">
<form method="post" class="form-signin" id="form-reg" name="form-reg">
<input type="hidden" name="action" value="register">
<?php if(!empty($pageData['registerMsg'])) :?>
<p><?php echo $pageData['registerMsg']; ?></p>
<?php endif; ?>
<input type="text" name="fullName" class="form-control" id="regFullName" placeholder="ФИО" required>
<input type="text" name="login" class="form-control" id="regLogin" placeholder="Логин" required>
<input type="email" name="email" class="form-control" id="regEmail" placeholder="Email" required>
<input type="password" name="password" class="form-control" id="regPassword" placeholder="Пароль" required>
<input type="submit" class="btn btn-lg btn-primary btn-block" value="Регистрация"/>
</form>
</div>
</div>
</div>
</div>
</div>
<footer>
</footer>
<script src="/js/jquery.js"></script>
<script src="/js/bootstrap.min.js"></script>
<!-- <script src="/js/angular.min.js"></script>
<script src="/js/script.js"></script> -->
</body>
</html>
Code lesson (IndexController)
<?php
class IndexController extends Controller {
private $pageTpl = '/views/main.tpl.php';
public function __construct() {
$this->model = new IndexModel();
$this->view = new View();
}
public function index() {
$this->pageData['title'] = "Вход в личный кабинет";
if(!empty($_POST)) {
$action = $_POST['action'];
switch ($action) {
case 'login':
if(!$this->login()) {
$this->pageData['error'] = "Неправильный логин или пароль";
}
break;
case 'register':
if($this->register()) {
$this->pageData['registerMsg'] = "Вы успешно зарегистрированы. Пожалуйста, авторизуйтесь";
} else {
$this->pageData['registerMsg'] = "Произошла ошибка во время регистрации";
}
break;
}
}
$this->view->render($this->pageTpl, $this->pageData);
}
public function login() {
if(!$this->model->checkUser()) {
return false;
}
}
public function register() {
if(!empty($_POST) && !empty($_POST['fullName']) && !empty($_POST['login']) && !empty($_POST['email']) && !empty($_POST['password'])) {
$regUser = $_POST['fullName'];
$regLogin = $_POST['login'];
$regEmail = $_POST['email'];
$regPassword = md5($_POST['password']);
$this->model->registerNewUser($regUser, $regLogin, $regEmail, $regPassword);
return true;
} else {
$this->pageData['registerMsg'] = "Вы заполнили не все поля";
return false;
}
}
}
Code lesson (IndexModel)
<?php
class IndexModel extends Model {
public function checkUser() {
$login = $_POST['login'];
$password = md5($_POST['password']);
$sql = "SELECT * FROM users WHERE login = :login AND password = :password";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":login", $login, PDO::PARAM_STR);
$stmt->bindValue(":password", $password, PDO::PARAM_STR);
$stmt->execute();
$res = $stmt->fetch(PDO::FETCH_ASSOC);
if(!empty($res)) {
$_SESSION['user'] = $_POST['login'];
$_SESSION['userId'] = $res['id'];
$_SESSION['role_id'] = $res['role_id'];
header("Location: /cabinet");
} else {
return false;
}
}
public function registerNewUser($regUser, $regLogin, $regEmail, $regPassword) {
$sql = "INSERT INTO users(fullName, login, email, password, role_id)
VALUES (:login, :fullName, :email, :password, 2)
";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":login", $regLogin, PDO::PARAM_STR);
$stmt->bindValue(":fullName", $regUser, PDO::PARAM_STR);
$stmt->bindValue(":email", $regEmail, PDO::PARAM_STR);
$stmt->bindValue(":password", $regPassword, PDO::PARAM_STR);
$stmt->execute();
}
}
0 Comments