Jump to content


Photo

PHP Sessions.

php session

  • Please log in to reply
5 replies to this topic

#1 Hazukiy

Hazukiy

    Advanced Member

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

Posted 22 April 2013 - 01: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

  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';


#2 lemmin

lemmin

    Advanced Member

  • Members
  • PipPipPip
  • 1,906 posts

Posted 22 April 2013 - 02: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.

 

http://php.net/manua...uperglobals.php



#3 gizmola

gizmola

    Advanced Member

  • Administrators
  • 4,141 posts
  • LocationLos Angeles, CA USA

Posted 22 April 2013 - 02: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

jugesh

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts

Posted 23 April 2013 - 02:33 AM

go through the links given below to study in details:

 

http://php.net/manua...ook.session.php

 

http://www.tizag.com...phpsessions.php



#5 Hall of Famer

Hall of Famer

    OOP Fanboi

  • Members
  • PipPipPip
  • 315 posts
  • LocationIthaca

Posted 23 April 2013 - 05: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:

http://us1.php.net/m...sionhandler.php


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.

zog841.jpg


#6 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,712 posts

Posted 23 April 2013 - 06: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

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com