How can one implement a login system for administrators and editors in a PHP blog using form submission?
To implement a login system for administrators and editors in a PHP blog using form submission, you can create a login form that collects the username and password. Upon form submission, you can validate the credentials against a database of users with appropriate roles. If the credentials are correct, you can set a session variable to indicate the user's role and redirect them to the appropriate dashboard.
<?php
session_start();
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Validate the username and password
$username = $_POST['username'];
$password = $_POST['password'];
// Check the credentials against a database
if ($username == 'admin' && $password == 'adminpass') {
$_SESSION['role'] = 'admin';
header('Location: admin_dashboard.php');
exit;
} elseif ($username == 'editor' && $password == 'editorpass') {
$_SESSION['role'] = 'editor';
header('Location: editor_dashboard.php');
exit;
} else {
echo "Invalid credentials. Please try again.";
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br>
<input type="submit" value="Login">
</form>
Keywords
Related Questions
- In PHP development, what are the recommended methods for securely storing and accessing sensitive database connection information, such as host, username, password, and database name?
- Are there any best practices or alternative methods to achieve the same result as preg_replace in PHP?
- What role does caching play in the differences between local environments like XAMPP and live servers when it comes to session handling in PHP?