Reputation: 8297
Is there a setting by which I can get some scripts to load/execute automatically once the PHP starts like getting a few constant up into global memory.
Also is there a global memory which is accessible across users? Also about memory is there no memory which is accessible by all users? One person set that another person access's it or should I have to read and write to file every time for some thing to be shared across or rather a database temp table.
Surprised PHP doesn't have this ?
Thanks.
Upvotes: 3
Views: 2645
Reputation: 76
If you use some subsystem (like APC shared memory, or even memcached), you may run data-generator script automatically on apache start by editing it's start-up script (/etc/init.d/[apache2|httpd]
). As example, you can add curl or wget request to the generator script right after start|restart.
There is a little probability of not-initialized state on first 'external' request, but I'n sure that you can handle that.
Upvotes: 0
Reputation: 1795
If I understand correctly, you want PHP to execute some script when you start Apache, to store some globally shared values. If I'm wrong, please edit/comment.
The short answer is: no, you can't do that. PHP isn't exactly a server that stays running waiting for client requests. It handles each HTTP request individually.
The long answer is: well... you could do auto_prepend_file to do it on each request. You could create some simple bash script and use that to start Apache then call a PHP script, but it wouldn't execute in Apache.
As for shared memory, there are a few choices. Using flat files, a database, or memcached is probably the most portable. Some installs have the Shared Memory functions enabled, but it's not guaranteed.
Upvotes: 3
Reputation: 87201
Each running PHP script instance has its own global variables, functions, constants etc., there is no way to copy a variable from one running script instance to another directly. However, there are indirect methods like using files, a database or memcached.
Upvotes: 0
Reputation: 137116
PHP does not provide a global cache that is shared across scripts. You can work around this in different ways depending on your requirements.
If you want to setup global constants that are not expensive to compute, you can write a script that defines these constants, and then automatically get this script to run before the requested file. This is done using the auto_prepend_file option in php.ini.
If you are computing expensive values, you can use memcached as a global cache that is accessible by all PHP scripts. There are multiple client APIs to connect to memcached using PHP.
Of course, you can also store global values in the user session if these values are per-user. Or even use MySQL.
Upvotes: 7
Reputation: 302
If you are using some sort of MVC framework you could define your variables there and ensure that file is loaded each time. You can use $GLOBALS['var_name'] = $value; and access this in some other file during that request.
Upvotes: 0