Sure! Here’s a simple PHP login example:
HTML Form Code:
<form action="login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
PHP Code for login.php:
<?php
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form data
$username = $_POST["username"];
$password = $_POST["password"];
// Check if username and password are correct
if ($username == "myusername" && $password == "mypassword") {
// Start the session and store the username
session_start();
$_SESSION["username"] = $username;
// Redirect to the home page or dashboard
header("Location: home.php");
exit();
} else {
// Display an error message
echo "Invalid username or password.";
}
}
?>
In this example, we have an HTML form with two input fields for username and password. When the user submits the form, the PHP script checks if the username and password are correct. If they are, it starts a session and stores the username in the session. It then redirects the user to the home page or dashboard. If the username and password are incorrect, it displays an error message.
Note: This is just a simple example and doesn’t include any security measures like password hashing or SQL injection prevention. It’s important to implement proper security measures when building a login system.