0

so this is my code:

<?php
if(isset($_COOKIE["usernameCookie"])){
    echo "welcome again, your username and password is " . $_COOKIE['usernameCookie'];
}else {

    if (isset($_POST['submit'])) {
        session_start();
        $_SESSION['user'] = [
            'username' => $_POST['username'],
            'password' => $_POST['password']
        ];
        echo $_SESSION['user']['username'] . " = " . $_SESSION['user']['password'];

        if (!empty($_POST['remember'])) {
            setcookie("usernameCookie", $_SESSION['user']['username'] . " " . $_SESSION['user']['password'], time()+3600);
        };

        if (isset($_POST['log'])) {
            setcookie("usernameCookie","", time()-3600);
            session_unset();
            session_destroy();
        };
    };
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title>Login Page</title>
    </head>
    <body>
    <?php
    if (!isset($_SESSION['user'])) {
        ?>
        <form method="post">
        username:<input type="text" name="username" required>
        <br><br>
        Password:<input type="password" name="password" required>
        <br><br>
        Remember me?:<input type="checkbox" name="remember">
        <br><br>
        <input type="submit" name="submit">

        <?php
    } else {
        ?>
        <br><br>
        <a href="LoginPage.php" name="log">Log out</a>
        </form>
        <?php
    }
    ?>
    </body>
    </html>
    <?php
}
?>

in pictures:

enter image description here

if the user inserted his username then password then it will take him to the same php page "as requested by my instructor" and print his name, password and a logout link like this:

simple login/logout page

as you can see, i wrote the code so that it will create the cookie "usernameCookie", so if the user checked the "remember me?" checkbox previously then it will print the same information as previously and store a cookie, if the user refreshed the page or let's say closed the page and opened it again within the cookie time, the it will show this page:

enter image description here

now, my problem is that if the user checked the box and pressed "log out" after that it will still show him as shown in the last picture "welcome again, your username and password is adam 12345" even though i clicked log out, here's the code that i wrote for logging out

 if (isset($_POST['log'])) {
            setcookie("usernameCookie","", time()-3600);
            session_unset();
            session_destroy();
        };

did i delete the cookie incorrectly? what's the solution?

and sorry if i rephrased the question badly, i'm new here :D

Community
  • 1
  • 1
ayayabood
  • 11
  • 6

0 Answers0