私は苦労しており、自分のユーザータイプに基づいてユーザーをリダイレクトするためにログインフォームを取得する方法を知りたいと思います。私は顧客と管理者をデータベースのユーザータイプとして持っています。私のコードは、ログインページ、ログインコントローラ、データアクセスページ、ユーザークラスページ、およびログアウトで構成されています。PDO:MVC Usertypeに基づいてユーザーログインをリダイレクトする方法
ログインページ
<?php require_once ("../Controller/loginController.php"); ?>
<form action="" method= "POST">
<br><br>
Username:
<input name="loginName" placeholder="User Name">
<br><br><br>
Password:
<input type="password" name="loginPassword" placeholder="Password">
<br><br>
<input type="submit" value="Log In" name="button"> <span style="color: red"><?='<br><br>'. $error?></span>
</form>
ログインコントローラのページ
<?php
require_once("../Model/dataAccess.php");
require_once("../Model/user.class.php");
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (!isset($error)) {
$error = "";
}
if (isset($_SESSION["loggedInUser"])) {
header('Location: ../View/account.php');
die();
} else if (isset($_REQUEST["button"])) {
$username = !empty($_REQUEST["loginName"]) ? trim($_REQUEST["loginName"]) : null;
$password = !empty($_REQUEST["loginPassword"]) ? trim($_REQUEST["loginPassword"]) : null;
$currentuser = getLogIn($username, $password);
if ($currentuser) {
$_SESSION["loggedInUser"] = $currentuser;
$error = "You have logged in successfully";
header('Location: ../View/account.php');
die();
}
else {
$error = "The login details supplied do not match any valid user.";
header('Location: ../View/login.php');
}
}
?>
ログアウトページ
<?php
session_start();
unset($_SESSION["loggedInUser"]);
session_destroy();
$error = "You have successfully logged out.";
echo $error . '<br><br>';
echo 'Click <a href="login.php">here</a> to login.';
?>
データアクセスページ
<?php
require_once('../Model/user.class.php');
function getLogIn($username, $password)
{
global $pdo;
$sqlUserLog = 'SELECT userType FROM User WHERE userName= ? AND userPass= ?';
$stmtUserLog = $pdo->prepare($sqlUserLog);
if (!$stmtUserLog->execute(array(
$username,
$password
))) {
die('Error');
}
$UserLog = $stmtUserLog->fetch();
return $UserLog;
}
?>
私はこの部分if(!$stmtUserLog->execute(array($username, $password))) {
から想定してユーザークラスページ
<?php
class User
{
var $userID;
var $userType;
var $userName;
var $userPass;
function __get($name)
{
return $this->$name;
}
function __set($name, $value)
{
$this->$name = $value;
}
}
?>
'if(userLog = a){redirectToPage} else {redirectToAnotherPage}' –
@MasivuyeCokileログインコントローラでこれを行うことはできますか? – user3531869