Hazukiy Posted April 22, 2013 Share Posted April 22, 2013 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'; Quote Link to comment https://forums.phpfreaks.com/topic/277183-php-sessions/ Share on other sites More sharing options...
lemmin Posted April 22, 2013 Share Posted April 22, 2013 $_SESSION is a super global associative array. Any index key you set becomes a session-accessible variable. In other words, you can change 'name' to whatever you want. http://php.net/manual/en/language.variables.superglobals.php Quote Link to comment https://forums.phpfreaks.com/topic/277183-php-sessions/#findComment-1425989 Share on other sites More sharing options...
gizmola Posted April 22, 2013 Share Posted April 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277183-php-sessions/#findComment-1425997 Share on other sites More sharing options...
jugesh Posted April 23, 2013 Share Posted April 23, 2013 go through the links given below to study in details: http://php.net/manual/en/book.session.php http://www.tizag.com/phpT/phpsessions.php Quote Link to comment https://forums.phpfreaks.com/topic/277183-php-sessions/#findComment-1426052 Share on other sites More sharing options...
Hall of Famer Posted April 23, 2013 Share Posted April 23, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/277183-php-sessions/#findComment-1426070 Share on other sites More sharing options...
mac_gyver Posted April 23, 2013 Share Posted April 23, 2013 the session handler class has nothing to do with the subject of this thread. Quote Link to comment https://forums.phpfreaks.com/topic/277183-php-sessions/#findComment-1426072 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.