Jump to content

Using Sessions (should be simple)


cmazur

Recommended Posts

Ok, I need to keep the username and user_id of my user who has logged in.
This is my first attempt at using sessions, thus I am not sure why this is not working.
The first time the user logs in, any old sessions are destroyed and a new session is started setting the username and id.
When the user returns to the page , the username and id should be stored in the session array.

Needless to say, I think it's my syntax having to do with the sessions...

[code]
if (isset($_POST["login"]))
{
//Get form data values
$username = $_POST["username"];
$password = $_POST["password"];

//Validate the user
$id = validate_user(get_user($username, $password), $username);
if($id != -1)
{
//Unset any prior session variables
unset($_SESSION['username']);
unset($_SESSION['id']);

//Destroy any prior session
session_destroy();

//Start new session
session_start();
$_SESSION['username'] = $username;
$_SESSION['id'] = $id;
}
}
else
{
//Get the logged in user's information
session_start();
$username = $_SESSION['username'];
$id = $_SESSION['id'];
                                      print("user: $username <br />id: $id<br />");
                          }
[/code]
Link to comment
https://forums.phpfreaks.com/topic/29465-using-sessions-should-be-simple/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.