Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best practices in naming session variables

Tags:

php

session

I was used to naming my session variables the "normal" way, kinda like when I want to keep track of user details, I name them:

  • $_SESSION['username']
  • $_SESSION['email']
  • $_SESSION['id']

I am worried that they may be in conflict with other session data when I am browsing sites in the same browser, or will there not be any conflict at all(once I tried to simultaneously run two of my projects with the same session variables, residing in the same server, and obviously, things got real messy).

like image 408
yretuta Avatar asked Jan 25 '10 00:01

yretuta


2 Answers

All of the session data is stored on the server. All the browser has is a cookie that references the session on the server. There can't be naming conflicts for this reason, and also because Cookies naming scope is domain based.

like image 117
Luca Matteis Avatar answered Oct 28 '22 16:10

Luca Matteis


Consider setting them in a subarray related to your application:

$_SESSION['myapp']['username']
$_SESSION['myapp']['id']

That should significantly help avoid conflicts.

EDIT: I misread your question, Luca Matteis has your answer. My solution above would be to avoid your multiple apps on the same domain session conflict.

like image 25
nortron Avatar answered Oct 28 '22 17:10

nortron