I have a login.php and a few pages for userarea. After login, it works and it goes to user.php and I use some $_SESSION
variables to show the name and username. It works, also my $_SESSION
variables are set at first time too and it will stay on the user.php.
But when I go to another page of userarea, it returned me to login.php, but if I login again, then it is OK and no problem and I can go to different pages without problem. Why?
I tried these things:
I changed $_SESSION['log_in']
from boolean to string or use another session like $_SESSION['username']
for !isset($_SESSION['username'])
(top of each pages). This username session will be show in the user.php but after that I go to another page doesn't work.
I have session_start()
top of every page.
<?php
session_start();
if(!isset($_SESSION['log_in'])){
header("Location: login.php");
}
// I have this code top of every page.
?>
This is login.php:
<?php
session_start();
if(isset($_SESSION['log_in'])){
header("Location:user.php");
}
if( password_verify($_POST['password'],$user['password'])) {
$_SESSION['name'] = $user['name'];
$_SESSION['email'] = $user['email'];
$_SESSION['username'] = $user['username'];
//to know is user login or not
$_SESSION['log_in'] = TRUE;
echo "<meta http-equiv=Refresh content=2;url=user.php>";
//if I user header("Location: user.php") it doesn't go to user, but with meta it goes to user.php
?>
This is user.php:
<?php
session_start();
if(!isset($_SESSION['log_in'])){
header("Location: login.php");
}
// this is top of my user.php and top of another pages too.
?>
This is logout:
<?php
session_start();
if(isset($_SESSION['log_in'])) {
session_unset();
session_destroy();
header('location: login.php');
} else {
session_unset();
session_destroy();
header('location: http://www.mywebsite.com');
}
// this is my logout.php
?>
I don't get any error, and I have another session part too for admins, but I wrote totally different session, for example $_SESSION['admin_log_in']
it has just one page and it works good.