Can PHP authentication be implemented alongside an HTML form for user login?
Yes, PHP authentication can be implemented alongside an HTML form for user login. You can create a PHP script that processes the form data, checks the user credentials against a database, and sets a session variable upon successful authentication. This session variable can then be used to restrict access to certain pages or resources on your website.
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$password = $_POST['password'];
// Validate username and password against database
if ($username === 'admin' && $password === 'password') {
$_SESSION['authenticated'] = true;
header('Location: dashboard.php');
exit;
} else {
$error = "Invalid username or password";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form method="post" action="">
<input type="text" name="username" placeholder="Username" required><br><br>
<input type="password" name="password" placeholder="Password" required><br><br>
<button type="submit">Login</button>
</form>
<?php if(isset($error)) { ?>
<p><?php echo $error; ?></p>
<?php } ?>
</body>
</html>
Keywords
Related Questions
- What are some common pitfalls to avoid when using cURL in PHP for web service communication?
- What are the potential consequences of using a single equals sign instead of a double equals sign in PHP comparison operations?
- How can one troubleshoot and resolve issues related to headers and cookies in PHP?