PHP Login Example

please click here for more wordpress cource

Sure! Here’s a simple PHP login example that you can use as a starting point:

  1. First, create a login form HTML page with the following fields:
<form method="post" action="login.php">
    <input type="text" name="username" placeholder="Username">
    <input type="password" name="password" placeholder="Password">
    <input type="submit" name="submit" value="Login">
</form>
  1. Then, create a PHP script called login.php that will process the form data and check if the user is authorized. Here’s an example:
<?php
session_start();

// Check if the user has submitted the form
if (isset($_POST['submit'])) {
    // Get the username and password from the form
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Check if the username and password are correct
    if ($username === 'admin' && $password === 'password') {
        // If they are, set the session variable to indicate that the user is logged in
        $_SESSION['loggedin'] = true;

        // Redirect the user to the dashboard page
        header('Location: dashboard.php');
        exit;
    } else {
        // If the username or password is incorrect, show an error message
        echo 'Invalid username or password';
    }
}
?>
  1. Finally, create a dashboard page (dashboard.php) that will only be accessible to logged-in users. Here’s an example:
<?php
session_start();

// Check if the user is logged in
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    // If not, redirect them to the login page
    header('Location: login.php');
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Dashboard</title>
</head>
<body>
    <h1>Welcome to the Dashboard</h1>
    <p>You are logged in as <?php echo $_POST['username']; ?></p>
</body>
</html>

This is a very basic example, and you’ll need to modify it to fit your specific requirements (e.g. adding database connectivity, password hashing, etc.). But it should give you a good starting point!

You may also like...

Popular Posts

Leave a Reply

Your email address will not be published. Required fields are marked *