PHP login page with class

 Creating a PHP login form with a class involves creating a class that handles the authentication process and integrates it into your HTML form. Below is a basic example of how you can achieve this. In this example, we'll create a User class to handle user authentication.

Create a User class (user.php):

php code

<?php

class User {

    private $username = "your_username"; // Change to your actual username

    private $password = "your_password"; // Change to your actual password

    public function authenticate($input_username, $input_password) {

        if ($input_username === $this->username && $input_password === $this->password) {

            return true;

        }

        return false;

    }

}

?>

Create the HTML form (index.php):

php code

<?php

session_start(); // Start the session

require_once 'user.php'; // Include the User class


if (isset($_POST['login'])) {

    $username = $_POST['username'];

    $password = $_POST['password'];


    $user = new User();

    if ($user->authenticate($username, $password)) {

        $_SESSION['username'] = $username;

        header('Location: welcome.php'); // Redirect to a welcome page upon successful login

        exit();

    } else {

        $error_message = "Invalid username or password";

    }

}

?>

<!DOCTYPE html>

<html>

<head>

    <title>Login</title>

</head>

<body>

    <h1>Login</h1>

    <?php if (isset($error_message)) { ?>

        <p><?php echo $error_message; ?></p>

    <?php } ?>

    <form method="post" action="index.php">

        <label for="username">Username:</label>

        <input type="text" id="username" name="username" required><br>

        <label for="password">Password:</label>

        <input type="password" id="password" name="password" required><br>

        <input type="submit" name="login" value="Login">

    </form>

</body>

</html>

Create a welcome page (welcome.php):

php code

<?php

session_start();


if (!isset($_SESSION['username'])) {

    header('Location: index.php');

    exit();

}


$username = $_SESSION['username'];

?>


<!DOCTYPE html>

<html>

<head>

    <title>Welcome</title>

</head>

<body>

    <h1>Welcome, <?php echo $username; ?>!</h1>

    <p>This is a protected page.</p>

    <a href="logout.php">Logout</a>

</body>

</html>

Create a logout page (logout.php):

php code

<?php

session_start();

session_destroy();

header('Location: index.php');

exit();

?>

This code demonstrates a simple login system using a PHP class for user authentication. Users need to enter their username and password, and if they match the predefined values, they are redirected to the welcome page. When users log out, their session is destroyed. Please remember to replace "your_username" and "your_password" in the User class with your actual username and password for a more secure implementation.

Comments

Popular posts from this blog

WORDPRESS: Content optimization and keyword research

Dependency Management: Using tools like Composer to manage dependencies in PHP projects.

Rating system in PHP with MYSQL

Caching mechanisms in MYSQL

HTML Comments: Adding comments to your HTML code