So far, I have only the login.html files which has login form, redirects user once logged on and logout function. What I want to do is once a user logs in, they redirect to but their username is displayed on the top of the page. And with the file... I just want it to be able to logout the user. So far on my website, I can login as far as I am concerned, and it redirects once user logs in, but I can login as many times as I want, and I can logout as many times as I want.... It's complicated to sort out and I want to do this without SQL or any other server-side storage (since I am only using HTML local storage).
3 Answers
WRITE THIS ALL IN TOP PAGE
IN YOUR LOGIN PAGE
<?php
session_start();
if (isset($_POST["submit"])) {
$username = $_POST["username"];
$_SESSION["username"] = $username;
header('Refresh: 5; URL=GameWebsite.php')
}
?>
IN YOUR LOGOUT PAGE
if(isset($_SESSION['username']))
{
session_start();
session_unset();
session_destroy();
//Then you may redirect to the login page if you want after sometime.
echo " You have successfully logged out... You will be redirected back to the login page in a moment. ";
header('Refresh: 5; URL=login.php');
}
else
{
header("Location:login.php"); // HERE WHEN USER NO HAVE SESSION
}
IN ANOTHER PAGES YOU CHECK
if(isset($_SESSION['username']))
{
}else
{
header("Location:login.php"); // HERE WHEN USER NO HAVE SESSION
}
Comments
in your login page write on top
if(isset($_SESSION['username']))
{
header('Refresh: 5; URL=GameWebsite.html')
}
In your logout.php write
if(isset($_SESSION['username']))
{
session_start();
session_unset();
session_destroy();
//Then you may redirect to the login page if you want after sometime.
echo " You have successfully logged out... You will be redirected back to the login page in a moment. ";
header('Refresh: 5; URL=Login.html');
}
else
{
header("Location: login.php");
}
18 Comments
Aram Mnatsakanyan
when you loged in only that time you can see logout button you can check it like this
<?php isset($_SESSION['username']{?> HERE YOUR LOGOUT BUTTON <?php}?> When user click on logout button he redirect into logout php and int ther he see your messageAram Mnatsakanyan
<?php isset($_SESSION['username'] { ?> <a href="logout.php" style="position:absolute;top:120px;right:35px "> Click here to logout </a> <?php } ?>
Aram Mnatsakanyan
what you have in that line?
A.Adams
<?php isset($_SESSION['username'] { ?> <a href="logout.php" style="position:absolute;top:120px;right:35px "> Click here to logout </a> <?php } ?>
Aram Mnatsakanyan
can you send me 3 files login logout and GameWebsite php ?
|
if ( $_SESSION['username'] ) { //... redirect / show "already logged in" message };$_SESSION['username']