Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

Sessions can't be and shouldn't be used as GLOBALs.

  1. I am unable to understand why want to store a small number as 4-digit in a session var. I assume its a dynamic value (not static, which is why you aren't storing as a variable in one of your i.e. config files).

  2. Best way to achieve that goal is to use MySQL... other alternatives are SQLite.

  3. You can also achieve the same goal by storing that value in a local file.

to store value:

$fp = fopen("variable_name.txt", "w");
fwrite($fp, $value); // $value is the 4 digit value you wish to store.
fclose($fp);

to retrieve value:

$fp = fopen("variable_name.txt", "r");

$value = fread($fp, 1024);

fclose($fp);

if you could add more details / codes in question... it would be better.

As stated in other answers session shouldn't be global. But why?

Reason: Web server is usually multi threaded. I.e. multiple requests are served at the same time. If one process writes to the global session and other reads at the same time, it might read incomplete data. ( And more serious things happens)

If you are using files to share common data and the data may changes in requests, you should use operating system level locks on files. This is difficult and not recommended.

If you use database for the global season, If the session has only one piece of data, it should be fine as database will make sure there is no incomplete temporary state. If the season has more than one piece of data and they are related to each other, you should use transaction features of the database to make sure they are in valid state in all cases.

Yes, it can be used as global variable, you may even make a kind of chat, using only session, but this is not the best approach. Just set the same session_id for each user

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top