PHP Share Variable Among Different Users/Sessions

Can I share Session variables between multiple clients with php Sessions?

Answering your last question first:

The Session PgSQLDocs you linked is the PostgreSQL Session Save Handler. It is a Session Save Handler you can configure to use instead of the default session save handler. The default session save handler in PHP is storing sessions to disk (files). If you use the save handler for PostgreSQL sessions are saved into a PostgreSQL database instead (pgsql).

Saving sessions inside a database can make sense if you want to allow access to the session store from multiple webservers (scaling an application) or in your case (probably) to access all sessions with SQL queries albeit normally a tailored session save handler is defined for that (which could be based on the PgSQL session save handler functions).

To answer your first question then:

Yes you can do so as long as you've got a reference to the object you relate to and you know how to access it. This can be either done by manually accessing the session storage or by sharing a session on it's own and switching sessions to access other session data. It depends on your needs, in your case it's probably more easy to just access serialized data that is stored by the ID in some extra table that has nothing to do with sessions. You should think about how to take care of the data if you don't need it any longer, e.g. remove it after some time of inactivity. In the end you're writing your own session implementation that way which is do-able. PHP before version 4 had no session support out of the box and the session support it has nowadays is very lightweight so if you need to do more specific stuff like you need to do, you normally write your own.

So multiple clients can use the same session (share a session) which is actually as well a way to attack webapps (session hijackingAttack) but as long as the "hijack" is intended inside your application data-flow, I do not see anything technically wrong with it. In PHP that means you need to close the current session, open the other one (sessions are identified by their name and ID), read the value, close the other session and re-open the current one. It technically works in PHP however write solid code when you do this because session problems are quite hard to debug.

This is also often a good reason to write your own object-sharing mechanism between multiple clients instead of re-using PHP's session featureDocs for that.

PHP Session-like storage global across all users

Take a look at shared memory functions. There are two libraries that can be used to access shared memory:

  • Semaphores
  • Shared Memory

For storing binary data or one huge String, the Shared Memory library is better, whereas the Semahpores library provides convenient functions to store multiple variables of different types (at the cost of some overhead, that can be quite significant especially for a lot of small-sized (boolean for example) variables.

If that is too complex, and/or you don't worry about performance, you could just store the data in files (after all, PHPs internal session management uses files, too....)

PHP global variable available to application and other users

This concept is called "application scope." PHP is better equipped for a scope of session, request or page. To place data within application scope in PHP, it's more common to make a database call to get the value.

We can use superglobals like $_SESSION, $_REQUEST or $_POST and $_GET to assign the variable to live beyond just one page, inside the session, request or page scopes. PHP doesn't have a common, similar method for assigning a value to a variable in application scope. Instead, application scopes are often imitated by calling an outside source of data, like a database or file. Earlier versions of PHP had some features that leaned toward the concept of application scope, but they have been deprecated.

For your questions:

  1. The question shows the idea of an application scope variable. For example, if you wanted every user to enter a value on index.php and write it to database, then it would be possible to make a call during the creation of file.php that would retrieve that value.

Instead of doing this for all users by one user, we often write this kind of code as a $_SESSION variable, usable by one user at a time. It is common for us to use a database write and a database read to solve this kind of problem. Simply put down the value by the user who writes it. Get it for the readers.


  1. This is the concept of a static application-scoped variable. To achieve this, simply have both users make similar database calls to retrieve the same value.

There is a super-global named $GLOBALS, but it is more of a shortcut for annotating how a variable is named. It's not a feature that will bring application scope to the variable.

JSP allows the use of application scoped variables by simple declaration. ASP allows the use of application scoped variables through application configuration with XML. PHP doesn't directly use application scope in the context of a variable, object or class.

php man scope
php man reserved

jsp application scope
jsp application scope

ms kb asp application scope variables
php man superglobals

Is a PHP session variable shared across running scripts?

If you have the same browser call 2 pages that set the same session variable, whichever one gets processed last will set the value.

If you have 2 separate browsers/users accessing the same 2 pages, they will set unique values.

PHP - Sharing session between multiple subdomains

Problem found:

My subdomains are on different apache servers so sessions vars can't be shared. I'll use database storage with unique vars in cookies to share my variables.

Passing variables from one php file to another with sessions and resetting the session

Remove the include 'enter-content' on the 'content' page.

The include calls session_start(), that would be the second call on the same page.



Related Topics



Leave a reply



Submit