-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

Anthony
  • 2,014
  • 2
  • 19
  • 29
  • 2
    @A.Adams you will have a link like `Logout` Then on logout.php you put that code – Masivuye Cokile Dec 12 '16 at 12:55
  • 1
    You'll have to logout in a separated file. – Anthony Dec 12 '16 at 12:56
  • 2
    @A.Adams you need to have session_start(); at the beginning of every php file in which you wish to access the $_SESSION superglobal ( in this case what you'll use to determine if a user is authenticated or not ) – Jez Emery Dec 12 '16 at 12:57
  • 1
    I added an example, you can use/display the username like that `echo SESSION['username']` – Anthony Dec 12 '16 at 13:13
  • 1
    You need a PHP file to execute PHP code. Also, you should learn PHP or at least read PHP documentation about session. – Anthony Dec 12 '16 at 17:18
-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
  • 12