我正在嘗試根據存盤在 db 中的列舉輸入將登錄用戶重定向到不同的頁面。
我正在嘗試這些行,但它總是將我重定向到pro.php. 我怎樣才能以正確的方式做到這一點?代碼有什么問題?
這是我的表定義:
| # | 姓名 | 型別 | 空值 | 默認 | |
|---|---|---|---|---|---|
| 1 | ID | 主整數(11) | 不 | 沒有任何 | 自動遞增 |
| 2 | 名 | 變數(255) | 是的 | 空值 | |
| 3 | 姓 | 變數(255) | 是的 | 空值 | |
| 4 | 電子郵件 | 指數 | 變數(255) | 是的 | 空值 |
| 5 | 專業 | 列舉('親','stu') | 是的 | 空值 | |
| 6 | 密碼 | varchar(100) | 是的 | 空值 | |
| 7 | 上次登錄 | 時間戳 | 是的 | 空值 |
這是PHP代碼:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include_once("config.php");
include_once("session.php");
if (isset($_POST['signin'])) {
$email = mysqli_real_escape_string($conn, $_POST['email']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$errors = array();
if (empty($email)) {
array_push($errors, "email is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM user WHERE email='$email' AND password='$password'";
$results = mysqli_query($conn, $query);
if (mysqli_num_rows($results) == 1)
{
$_SESSION['email'] = $email;
$_SESSION["user_name"]=$firstName;
$_SESSION['success'] = "You are now logged in";
$logintime = "UPDATE user SET lastLogin = now() where email = '$email'";
mysqli_query($conn, $logintime);
//Check speciality and redirect accordingly
$speciality = $row['speciality'];
if($speciality == "stu"){
header("location:stu.php");
}else{header("location:pro.php");}
}else
{
array_push($errors, "Wrong username/password combination");
header("location:login.php");
}
}
}
?>
我知道用戶已登錄,因為在登錄后,我要去 db 并查看當前用戶的 lastLogin 更新。
uj5u.com熱心網友回復:
好的,我將專業型別列舉更改為 int(0 或 1),并將更多內容更改為代碼,現在正確重定向我并且幾乎所有內容都正常作業。
if (isset($_POST['submit'])) {
$email = $_POST['email'];
$password = $_POST['password'];
//also i hash the password with a better algorythm
$password = hash('sha256', $password);
$sql = "SELECT * FROM user WHERE email='$email' AND password='$password'";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
$row = mysqli_fetch_assoc($result);
$_SESSION['user_name'] = $row['user_name'];
$logintime = "UPDATE user SET lastLogin = now() where email = '$email'";
mysqli_query($conn, $logintime);
$_SESSION['speciality'] = $row['speciality'];
if($_SESSION['speciality']==1) {header("Location: pro.php");}
else{header("Location: stu.php");}
} else {
echo "<script>alert('Woops! Email or Password is Wrong.')</script>";
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/364464.html
上一篇:在React.js上是否有更好的重定向API錯誤的方法
下一篇:重定向到子檔案夾
