Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP autostart with loading script

Tags:

php

autostart

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.

like image 563
coool Avatar asked Aug 03 '26 11:08

coool


2 Answers

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.

like image 80
Ayman Hourieh Avatar answered Aug 06 '26 12:08

Ayman Hourieh


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.

like image 25
James Socol Avatar answered Aug 06 '26 13:08

James Socol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!