3

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.

1
  • This could probably be split into two questions for better answers. Commented May 6, 2009 at 19:33

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

0

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.

1 Comment

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.
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.