Jump to content

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

If you are using PHP 5.4, Id recommend you to take a look at the session handler class. It is an object oriented way of playing with PHP sessions, you may extend its functionality by subclassing it:

http://us1.php.net/manual/en/class.sessionhandler.php

Link to comment
https://forums.phpfreaks.com/topic/277183-php-sessions/#findComment-1426070
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.