Reputation: 23634
When I login a new session is generated. How can I later know for which login the session was generated?
I am getting the session value, but how do I know which user the session is for and redirect him to that page?
Upvotes: 0
Views: 403
Reputation: 17846
You do not want to create a (new) session when the user is logging in. You create/resume the session on every page.
Here some example broken down to the essentials.
login.php
<?php
session_start();
if ($_POST['user'] == 'john' && $_POST['pwd'] == 'password') {
$_SESSION['loggedIn'] = true;
$_SESSION['firstname'] = 'John';
}
?>
admin.php
<?php
session_start();
if (!isset($_SESSION['loggedIn']) || !$_SESSION['loggedIn']) {
header('location: login.php');
exit();
}
echo 'Hello ' . $_SESSION['firstname'] . '!';
?>
session_start()
creates a new session. All data ($_SESSION) is stored on the server. A new cookie with the session's id is stored client-side.$_SESSION['loggedIn']
key set to true
session_start()
revives the session by the cookie sent by the browser$_SESSION
array we note this.Upvotes: 2