Skip to main content

Command Palette

Search for a command to run...

Example Php Login Code

Updated
2 min readView as Markdown
Example Php Login Code
<?php
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get user input from the login form
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Validate the user input (you should perform more robust validation in a real application)
    if ($username === 'example' && $password === 'password') {
        // Authentication successful
        session_start();
        $_SESSION['username'] = $username;
        header('Location: dashboard.php'); // Redirect to a dashboard or home page
        exit();
    } else {
        // Authentication failed
        $error_message = 'Invalid username or password';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Page</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
        }

        .login-container {
            width: 300px;
            margin: 100px auto;
            padding: 20px;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }

        .error-message {
            color: #ff0000;
            margin-bottom: 10px;
        }

        .login-btn {
            background-color: #4caf50;
            color: #fff;
            padding: 10px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>Login</h2>
        <?php if (isset($error_message)) { ?>
            <p class="error-message"><?php echo $error_message; ?></p>
        <?php } ?>
        <form method="post" action="">
            <div class="form-group">
                <label for="username">Username:</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <div class="form-group">
                <button type="submit" class="login-btn">Login</button>
            </div>
        </form>
    </div>
</body>
</html>

This example includes a basic HTML form with fields for a username and password. The PHP script at the top handles form submission, checks the credentials, and sets a session variable upon successful login. Note that you should replace the simple check for credentials with a more secure authentication mechanism in a real-world application.

More from this blog

Cafer's Blog

37 posts