8

Is there a way in PHP to use "out of session" variables, which would not be loaded/unloaded at every connexion, like in a Java server ?

Please excuse me for the lack of accuracy, I don't figure out how to write it in a proper way.

The main idea would be to have something like this :

<?php
    ...
    // $variablesAlreadyLoaded is kind of "static" and shared between all PHP threads
    // No need to initialize/load/instantiate it.
    $myVar = $variablesAlreadyLoaded['aConstantValueForEveryone'];
    ...
?>

I already did things like this using shmop and other weird things, but if there is a "clean" way to do this in "pure PHP" without using caching systems (I think about APC, Redis...), nor database.

EDIT 1 :

Since people (thanks to them having spent time for me) are answering me the same way with sessions, I add a constraint I missed to write : no sessions please.

EDIT 2 :

It seems the only PHP native methods to do such a thing are shared memory (shmop) and named pipes. I would use a managed manner to access shared objects, with no mind of memory management (shared memory block size) nor system problems (pipes).

Then, I browsed the net for a PHP module/library which provides functions/methods to do that : I found nothing.

EDIT 3 :

After a few researches on the way pointed out by @KFO, it appears that the putenv / setenv are not made to deal with objects (and I would avoid serialization). Thus, it resolves the problem for short "things" such as strings or numbers but not for more large/comples objects.

Using the "env way" AND another method to deal with bigger objects would be uncoherent and add complexity to the code and maintenability.

EDIT 4 :

Found this : DBus (GREE Lab DBus), but I'm not having tools to test it at work. Has somebody tested it yet ?

I'm open to every suggestion.

Thanks

EDIT 5 ("ANSWER"):

Since DBus is not exactly what I'm looking for (needs to install a third-party module, with no "serious" application evidence), I'm now using Memcache which has already proven its reliability (following @PeterM comment, see below).

Benj
  • 1,184
  • 7
  • 26
  • 57

4 Answers4

5
// First page
session_id('same_session_id_for_all');
session_start();
$_SESSION['aConstantValueForEveryone'] = 'My Content';

// Second page
session_id('same_session_id_for_all');
session_start();
echo $_SESSION['aConstantValueForEveryone'];

This works out of the box in PHP. Using the same session id (instead of an random user-uniqe string) to initialize the session for all visitors leads to a session which is the same for all users.


Is it really necessary to use session to achieve the goal or wouldn't it better to use constants?

There is no pure PHP way of sharing information across different threads in PHP! Except for an "external" file/database/servervariable/sessionfile solution.


Since some commentators pointed out, that there is serialize/unserialize functionality for Session data which might break data on the transport, there is a solution: In PHP the serialize and unserialize functionality serialize_handler can be configured as needed. See https://www.php.net/manual/session.configuration.php#ini.session.serialize-handler It might be also interesting to have a look at the magic class methods __sleep() and __wakeup() they define how a object behaves on a serialize or unserialize request. https://www.php.net/manual/language.oop5.magic.php#object.sleep ... Since PHP 5.1 there is also a predefined Serializable interface available: https://www.php.net/manual/class.serializable.php

powtac
  • 40,542
  • 28
  • 115
  • 170
  • 1
    Same comment : I want it to be usable for every PHP thread, independently of the session, in a Servlet-like manner. Moreover, using sessions is not what I want sinc it serializes/unserializes data each time. // see @PeterM comment. – Benj Feb 26 '13 at 10:19
  • I'm aware of forcing session ID, but it isn't what I'm looking for (wrote "No need to initialize/load/instantiate it"). – Benj Feb 26 '13 at 10:23
  • 1
    I updated the question, thanks to you I saw something I missed to write. – Benj Feb 26 '13 at 10:27
  • 1
    Ok, I see. There is no pure PHP way of sharing information across different threads in PHP! Except for an "external" file/database/servervariable/sessionfile solution. – powtac Feb 26 '13 at 10:27
  • Constants are not what I'm looking for, the objects must be mutable (I'm aware synchronization problems). – Benj Feb 26 '13 at 10:33
  • I updated my question : any PHP/C/whatever module/library to handle this ? – Benj Feb 26 '13 at 10:47
  • @Benj sorry no, I don't know DBus. – powtac Feb 26 '13 at 23:24
2

You can declare a Variable in your .htaccess. For Example SetEnv APPLICATION_ENVIRONMENT production and access it in your application with the function getenv('APPLICATION_ENVIRONMENT')

KFO
  • 328
  • 4
  • 15
  • Seems an interesting way. Is there performance issues with that ? Some docs about benchmarks or something ? – Benj Feb 26 '13 at 10:21
  • If you just set one variable with setenv you get no performance problems. But if you want to configure your complete application, you better create a configfile (or Class) and load this file on every request. – KFO Feb 26 '13 at 10:26
  • 1
    Here is some use case with zend framework which uses env to choose configuration: http://stackoverflow.com/questions/2510556/zend-framework-auto-switch-production-staging-test-etc –  Feb 26 '13 at 10:27
  • @KFO : "and load this file on every request" nope nope nope boy, I wrote "not be loaded/unloaded at every connection". It is very important ;) +1 for the setenv/getenv – Benj Feb 26 '13 at 10:35
  • Can you explain, what is your specific problem? I don't get it, why you just create a new file, write down all your settings and include this file into your project. – KFO Feb 27 '13 at 07:17
2

Another solution is to wrap your variable in a "persistent data" class that will automatically restore its data content every time the php script is run. Your class needs to to the following:

  • store content of variable into file in __destructor
  • load content of variable from file in __constructor

I prefer storing the file in JSON format so the content can be easily examined for debugging, but that is optional.

Be aware that some webservers will change the current working directory in the destructor, so you need to work with an absolute path.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
1

I think you can use $_SESSION['aConstantValueForEveryone'] that you can read it on every page on same domain.

Consider to refer to it's manual.

Habibillah
  • 27,347
  • 5
  • 36
  • 56
  • I want it to be usable for every PHP thread, independently of the session, in a Servlet-like manner. Moreover, using sessions is not what I want sinc it serializes/unserializes data each time. – Benj Feb 26 '13 at 10:18
  • 1
    @Benj asked for variable for *everyone* not current user –  Feb 26 '13 at 10:19