PHP 自动启动并加载脚本
是否有一个设置可以让我在 PHP 启动后自动加载/执行一些脚本,例如将一些常量放入全局内存中。
还有一个可供用户访问的全局内存吗? 还有关于内存,是否存在所有用户都可以访问的内存? 一个人设置另一个人访问它,或者我应该每次都必须读取和写入文件才能在数据库临时表中共享某些内容。
惊讶 PHP 没有这个?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
PHP 不提供跨脚本共享的全局缓存。 您可以根据您的要求以不同的方式解决此问题。
如果要设置计算成本不高的全局常量,可以编写一个定义这些常量的脚本,然后自动让该脚本在请求的文件之前运行。 这是使用 php.ini 中的 auto_prepend_file 选项 完成的。
如果您正在计算昂贵的值,您可以使用 memcached 作为所有 PHP 都可以访问的全局缓存脚本。 有多个客户端 API 可使用 PHP 连接到 memcached。
当然,您也可以在用户会话中存储全局值,如果这些值是针对每个用户的。 或者甚至使用MySQL。
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.
如果我理解正确的话,您希望 PHP 在启动 Apache 时执行一些脚本,以存储一些全局共享的值。 如果我错了,请编辑/评论。
简短的回答是:不,你不能这样做。 PHP 并不完全是一个保持运行等待客户端请求的服务器。 它单独处理每个 HTTP 请求。
长答案是:嗯...您可以执行 auto_prepend_file 对每个请求执行此操作。 您可以创建一些简单的 bash 脚本并使用它来启动 Apache,然后调用 PHP 脚本,但它不会在 Apache 中执行。
至于共享内存,有几种选择。 使用平面文件、数据库或 memcached 可能是最便携的。 某些安装启用了共享内存功能,但不能保证。
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.
如果您使用某种 MVC 框架,您可以在那里定义变量并确保每次都加载该文件。 您可以使用 $GLOBALS['var_name'] = $value; 并在该请求期间在其他文件中访问它。
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.
每个正在运行的 PHP 脚本实例都有自己的全局变量、函数、常量等,无法将变量从一个正在运行的脚本实例直接复制到另一个正在运行的脚本实例。 但是,还有一些间接方法,例如使用文件、数据库或内存缓存。
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.
如果您使用某些子系统(例如 APC 共享内存,甚至 memcached),您可以通过编辑 apache 的启动脚本(
/etc/init.d/[apache2|httpd] 在 apache start 上自动运行数据生成器脚本)。 例如,您可以在启动|重新启动后立即将curl 或wget 请求添加到生成器脚本中。
第一个“外部”请求时有可能出现未初始化状态,但我确信您可以处理该问题。
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.