Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing session variables in PHP

Tags:

php

session

Lets say I'm storing an array or object called $_SESSION["logged_in_user"].

If I need to refer to it repeatedly throughout the script, which of the following is the "best practice"?

  • Use $_SESSION["logged_in_user"] each time (i.e. $_SESSION["logged_in_user"]["first_name"])?

  • Copy the object into a new variable like $logged_in = $_SESSION["logged_in_user"]?

  • Create a reference to the session variable like $logged_in =& $_SESSION["logged_in_user"]

I'm probably overthinking this, but my main concerns are script overhead and readability. I'm not sure if referring to a session variable repeatedly is slower than referring to an inline-declared variable. I also don't know if copying a session variable into a "regular" variable adds more overhead than is necessary. I do like the readability of $logged_in["first_name"] over $_SESSION["logged_in_user"]["first_name"].

So is there a best practice here, or does it really not matter?

like image 752
David Grenier Avatar asked Nov 13 '11 17:11

David Grenier


People also ask

How can I access session variable in PHP?

To start PHP sessions, you must use the function session_start() . To set session variables, you will need to apply a global PHP $_SESSION variable . Note: The PHP session_start() function has to be the first thing in your document: all HTML tags come after.

How are session variables handled in PHP?

Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a session. The following example starts a session then register a variable called counter that is incremented each time the page is visited during the session.

Where does PHP store session variables?

PHP Session Start When you start a session, the web server generates a session identifier that uniquely identifies the visitor. By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier).

What is PHP session_start () function?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.


2 Answers

$_SESSION is a special PHP superglobal array. So you technically can refer to it whenever you want by just using it:

$_SESSION['logged_in_user']

However, it's special as well because it can change. The following example makes this visible:

$alias =& $_SESSION;
session_start();
$alias['foo'] = 'bar';

This code won't set $_SESSION['foo']. The $alias is pointing to a previous $_SESSION, session_start() has created a new session.

If you know the caveats like these you can naturally create your own $_SESSION abstraction.

like image 134
hakre Avatar answered Oct 07 '22 13:10

hakre


You're not overthinking it.... thinking is good!

I personally use symfony 1.4 to code in, which solves problems like these, but you may choose to stay lean :-)

I'd think OO (object oriented). Create a user class. Then make calls like UserFactory::getCurrentUser() or User::getCurrentUser() which will return the currently logged in user object. Among the member variables of that class would be the user_id. However, you'll be able to add addition functionality and data to that class.

Remember, thinking in OO means using abstract terms that are very close to the problem domain. User, Car, Order, etc. You don't have to be specific and you don't have to include all available information in that class for it to be "complete", whatever that is. Only include the data that you need at the time (keep yagni in mind). The classes are illusions of the concrete, real-world things. Just as 3D modelling is an illusion of a real world thing.

Hope that helps...

like image 39
Homer6 Avatar answered Oct 07 '22 12:10

Homer6