tags:

views:

242

answers:

4

Hi, 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.

and 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

THnks

A: 

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.

Roger
so there is no way to run a script automatically as soon as apache and php is up. I have to do it manually to run a script.
coool
+7  A: 

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.

Ayman Hourieh
A: 

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.

pts
+2  A: 

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.

James Socol