我是 PHP 的新手。我只想问如何在登录时将“管理员”和“用户”帐户重定向到特定页面。
当我以管理员身份登录时,它将被定向到一个页面,如“superuser.php”,我可以在该页面的 GUI 中为“用户”创建一个帐户。
当创建的“用户”帐户登录后,它将被定向到具有不同权限的页面,如“user.php”。
“用户”注册页面与“管理员”不同,因为它将位于管理员的主页。那就是我现在被困的地方
到目前为止,我完成了“管理”部分。
我的PHP代码:
<?php
session_start();
// variable declaration
$username = "";
$errors = array();
$_SESSION['success'] = "";
// connect to database
$db = mysqli_connect('localhost','root','','registration');
if (mysqli_connect_errno($db)) {
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (username, password)
VALUES('$username', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: login.php');
}
}
慕工程0101907
烙印99
呼啦一阵风
随时随地看视频慕课网APP