Jump to content


PHP Sessions.

php session

  • Please log in to reply
5 replies to this topic

#1 Hazukiy

  • Members
  • PipPipPip
  • Advanced Member
  • 125 posts
  • LocationEngland
  • Age:19

Posted 22 April 2013 - 06:55 PM

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

and then hold that data in a variable like
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['name'] = 'John';

#2 lemmin

  • Members
  • PipPipPip
  • Advanced Member
  • 1,906 posts

Posted 22 April 2013 - 07:06 PM

$_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.



#3 gizmola

  • Administrators
  • Advanced Member
  • 4,595 posts
  • LocationLos Angeles, CA USA

Posted 22 April 2013 - 07:39 PM

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.

#4 jugesh

  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 23 April 2013 - 07:33 AM

go through the links given below to study in details:





#5 Hall of Famer

Hall of Famer
  • Members
  • PipPipPip
  • OOP Fanboi
  • 315 posts
  • LocationIthaca

Posted 23 April 2013 - 10:49 AM

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:


Welcome to the world of OOPHP! In a perfect script, everything is an object. You cannot be perfect, but you can approach as close as can.


#6 mac_gyver

  • Staff Alumni
  • Staff Alumni
  • 4,048 posts

Posted 23 April 2013 - 11:14 AM

the session handler class has nothing to do with the subject of this thread.

multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users