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
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!
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.
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.
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.
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.
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]).
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