Session

login.php


 <!DOCTYPE html>

<html>

<head>

    <title>Login</title>

</head>

<body>

    <h2>Login Form</h2>

    <form method="POST" action="login_process.php">

        Username: <input type="text" name="username" required><br><br>

        Password: <input type="password" name="password" required><br><br>

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

    </form>

</body>

</html>



Login_process.php


<?php

session_start();


$username = $_POST['username'];

$password = $_POST['password'];


// Simple check: username must equal password

if ($username === $password) {

    $_SESSION['user'] = $username;

    header("Location: secured.php");

} else {

    header("Location: login.php");

}

exit();

?>





Secured.php



<?php

session_start();


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

    header("Location: login.php");

    exit();

}

?>


<!DOCTYPE html>

<html>

<head>

    <title>Secured Page</title>

</head>

<body>

    <h2>Welcome, <?php echo $_SESSION['user']; ?>!</h2>

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

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

</body>

</html>



Logout_process.php


<?php

session_start();

session_destroy();

header("Location: login.php");

exit();

?>






https://chatgpt.com/share/6840f99f-e818-800e-9765-21429d7cc06f


Comments