Current Location: Home> Latest Articles> How to Implement Form Handling and "Remember Me" Feature in PHP: Login Persistence with Cookies

How to Implement Form Handling and "Remember Me" Feature in PHP: Login Persistence with Cookies

M66 2025-06-13

PHP Form Handling: Implementing the "Remember Me" Feature with Cookies

In web development, we often encounter user login scenarios. To improve the user experience, we can use cookies to implement the "Remember Me" feature, allowing users to avoid re-entering their credentials when they return to the website. This article will show you how to handle forms with PHP and use cookies to achieve this functionality.

HTML Form Design

First, we need to create an HTML form that allows the user to input their username and password, and provide a checkbox for users to choose whether to remember their login status.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <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>
        
        <label for="remember">Remember Me:</label>
        <input type="checkbox" id="remember" name="remember"><br><br>
        
        <input type="submit" value="Login">
    </form>
</body>
</html>

PHP Form Handling

Create a PHP file named login.php to process the form data.

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get the username and password from the form submission
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Validate the username and password
    if ($username == 'admin' && $password == '123456') {
        // If the user chooses to remember login, set cookies to store username and password
        if (isset($_POST['remember'])) {
            setcookie('username', $username, time()+3600*24*7); // Save for 7 days
            setcookie('password', $password, time()+3600*24*7);
        }

        // Redirect to another page after successful login
        header("Location: welcome.php");
    } else {
        echo 'Incorrect username or password!';
    }
}
?>

Welcome Page

Create a PHP file named welcome.php to display a welcome page after successful login. On this page, we can greet the user based on the username stored in the cookie.

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Welcome</title>
</head>
<body>
    <?php
    if (isset($_COOKIE['username'])) {
        $username = $_COOKIE['username'];
        echo '<h1>Welcome back, ' . $username . '!</h1>';
    } else {
        echo '<h1>Please log in first!</h1>';
    }
    ?>
</body>
</html>

In the above code, we use isset($_COOKIE['username']) to check if the username is saved in the cookies. If it exists, we retrieve the username from $_COOKIE['username'] and display a welcome message. Otherwise, we prompt the user to log in first.

By following these steps, you can create a simple PHP login form with the "Remember Me" feature. When the user selects the "Remember Me" option and logs in successfully, they will be automatically logged in on their next visit to the site.