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?
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.
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.
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).
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.
$_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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With