I’m a newbie in PHP. I just want to ask how to redirect accounts that are ‘admins’ and ‘users’ to a specific page when they login.
– When I login as an admin it will be directed to a page like “superuser.php” where I can create an account for “users” in the GUI of the website.– When the created “user” account is logged in it will be directed to a page like “user.php” with a different privilege.
The ‘user’ signup page is different from the ‘admin’ as it will be located at the admin’s homepage. that is where I’m stuck at the moment
so far I did the ‘admin’ part.
my php code:
‘<?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’);
}
}
// …
// LOGIN USER
if (isset($_POST[‘login_user’])) {
$username = mysqli_real_escape_string($db, $_POST[‘username’]);
$password = mysqli_real_escape_string($db, $_POST[‘password’]);
if (empty($username)) {
array_push($errors, “Username is required”);
}
if (empty($password)) {
array_push($errors, “Password is required”);
}
if (count($errors) == 0) {
$password = md5($password);
$query = “SELECT * FROM users WHERE username=’$username’ AND password=’$password'”;
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION[‘username’] = $username;
$_SESSION[‘success’] = “You are now logged in”;
header(‘location: home.html’);
}else {
array_push($errors, “Wrong username or password combination”);
}
}
}
?>’