-4

So far, i can log in the website, but i don't really know if i am actually creating a session. I heard about PHP being the solution to my question, but i am not really familiar with PHP. So can someone show me how i can log in properly, displaying the username when on the website when logged in, to indicate that the user has actually logged in, and also, the log out link, so when clicked, the user logs out!

A.Adams
  • 65
  • 2
  • 8

2 Answers2

4

You start session, redirect the user, but you do not set a session. You should use something like that

<?php
session_start();
if (isset($_POST["submit"])) {
    $username = $_POST["username"];
    $_SESSION["username"] = $username;
}
//Here you can refresh or redirect to an other page
?>

And for logout (put it in a separated file, like logout.php)

    <?php
    session_start();
    session_unset();
    session_destroy();
     //Then you may redirect to the login page if you want after sometime.
    ?>

In any other file, use your session

<?php
session_start();
echo $_SESSION['username'];

See session_unset and session_destroy

AnthonyB
  • 1,932
  • 1
  • 20
  • 28
-1

You have to first start the session

session_start();
$_SESSION['variable_name'] = $session_variable;

Now for Logout script use this

session_start();
session_unset();
session_destroy();
Parag Yelonde
  • 34
  • 1
  • 11