Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Global Session for all users

I am using php 5.4

Session variables $_SESSION['name'] are used to store data so that the data can be access in any future request but is unique to a user only

Is it possible to create something similar to a session variable which is accessible to all request coming in but no matter which user it is? In other words a session variable which is not unique to users

Currently I am using a MySQL db to store temporary data but I think if this Global Session for all users is possible, it will give some performance improvement

i want to store something very small like a 4 digit number

like image 834
Krimson Avatar asked Mar 15 '14 23:03

Krimson


People also ask

Is session unique for every user?

Besides session hijacking, one session is always tied to just one user. Do I have to link the session somehow with the username on login If you don't store which user is using that session, is there any point in having user accounts? @Epodax It is not!

Are sessions variables shared global between users?

Is it that the global variables are shared across different users.? Global variables are not common for the whole application: they don't persist across different pages (they're destroyed every page change like any other variable that's not in the session). Plus, they're not shared between users.

How to create sessions in PHP?

Starting a PHP Session Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user.

Does every page need a session start?

It must be on every page you intend to use. The variables contained in the session—such as username and favorite color—are set with $_SESSION, a global variable. In this example, the session_start function is positioned after a non-printing comment but before any HTML.


2 Answers

By default session data is serialized and saved to a temporary file associated with the user's session, which is tracked by a cookie. You can configure session data to be saved in the database as well. This data is available via the $_SESSION superglobal per user.

So if you follow that logic, then either store serialized data in a file or store it in the database and to access it, it won't be a superglobal, but something almost a superglobal if you read it in as a global array such as $GLOBALS['all_peeps'].

You can do the same with an object or static class. It's really the same as config variables that you would use for your application regardless of the user.

like image 184
AbraCadaver Avatar answered Sep 25 '22 02:09

AbraCadaver


According to wikipedia:

Session is a semi-permanent interactive information interchange (...) between a computer and user.

Well, you probably missunderstand session meaning. Session shouldn't be global.

Anyway if you want some mechanism of sharing information between users, you should use database or files (standard php session is stored in files [one for each instance]).

like image 37
Scony Avatar answered Sep 27 '22 02:09

Scony