Jump to content

PHP Sessions.


Hazukiy

Recommended Posts

Hi, I'm currently trying to understand exactly how php sessions work? So far I've been told that you start a session with

  session_start()    
and then hold that data in a variable like
 $_SESSION['User_Id'];
but my problem is, is that I want it to hold the database user id so when the client logs in it keeps their session through their user_id. How would I execute this cause all the examples given to me look something like this:
session_start();
$_SESSION['name'] = 'John';
Link to comment
https://forums.phpfreaks.com/topic/277183-php-sessions/
Share on other sites

Although sessions often are used to support authentication, they are not the same thing. A session is simply a set of variables tied to a browser instance through a cookie. When you session_start() the server creates a session file on the server, generates a session id and issues a setcookie call with the session id it created. On subsequent calls, it will use that cookie to lookup the session id on the server, and if it exists, it will set the contents of $_SESSION array.

 

In other words, it just loads any session variables you might set and makes them available to your script.

 

Of course you can add whatever you want to to the array, so people typically will store user_id as a session variable to indicate a successful login, and can then check that variable to indicate login:

 

if (isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0) {
   // user logged in
} else {
   // redirect to login page
}
It's important not to confuse the session system with authentication. Every visitor will get a session once you issue session_start, regardless of whether the user is authenticated or not. Sessions facilitate authentication, but they are not equivalent to it.
Link to comment
https://forums.phpfreaks.com/topic/277183-php-sessions/#findComment-1425997
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.