简体   繁体   English

Windows计划任务的PHP $ _SERVER []变量适用性

[英]PHP $_SERVER[] variable avilability to a Windows Scheduled Task

Short Story: I'm looking for a work around to PHP $_SERVER[] variables not being available (only) in a case where a PHP script file is being run from a Windows Task Scheduler scheduled event. 简而言之:在Windows Task Scheduler计划的事件中运行PHP脚本文件的情况下,我正在寻找解决PHP $ _SERVER []变量不可用(仅)的方法。

I have two Windows 2008 R2 Servers with PHP version 7.1 running on both servers. 我有两个Windows Server 2008 R2服务器,两个服务器上均运行PHP版本7.1。 I've been calling server #1 'Production' and server #2 'Development'. 我一直将服务器#1称为“生产”,将服务器2称为“开发”。 Each server has it's own MySQL db. 每个服务器都有自己的MySQL数据库。

On the pages I've been writing, for MySQL db read/writes, I've been using $_SERVER['SERVER_NAME'], to identify what server is running the script (if the page is running on the development server, use the IP address of the MySQL db on the development server. If the current server is the production server, use the production database IP). 在我正在写的页面上,对于MySQL数据库的读/写,我一直在使用$ _SERVER ['SERVER_NAME']来确定哪个服务器在运行脚本(如果页面在开发服务器上运行,请使用开发服务器上MySQL数据库的IP地址(如果当前服务器是生产服务器,请使用生产数据库IP)。

That works perfectly fine for me. 这对我来说很好。 The exception is when I use the Windows Task Scheduler to schedule an automated task. 例外是当我使用Windows Task Scheduler计划自动任务时。 In this case, I'm creating a single stand alone PHP script file to send an email with some information from that database. 在这种情况下,我将创建一个独立的PHP脚本文件,以发送包含该数据库中某些信息的电子邮件。

PHP script file to send an email with some information from the db results in: PHP脚本文件从数据库发送带有一些信息的电子邮件,结果是:

  • Running the page in a browser results in $_SERVER['SERVER_NAME'] being defined (works good). 在浏览器中运行页面会导致$ _SERVER ['SERVER_NAME']被定义(运行良好)。
  • If Windows Task scheduler runs the PHP page this results in $_SERVER['SERVER_NAME'] not being defined 如果Windows Task Scheduler运行PHP页面,则会导致未定义$ _SERVER ['SERVER_NAME']

Would anyone know how I could make the $_SERVER[] variables available to a .php file being run by a Windows Scheduled Taks (where the task scheduler is of course pointing towards the PHP.exe with a referee to this script .php file)? 有谁知道我如何使$ _SERVER []变量可用于Windows Scheduled Taks(任务计划程序当然指向带有该脚本.php文件裁判的PHP.exe)运行的.php文件。 ? Or, is there another way besides the $_SERVER[] variable to detect the name of the server the script is being run on? 或者,除了$ _SERVER []变量之外,还有其他方法可以检测正在脚本运行的服务器的名称吗?

You can use the environment variables: 您可以使用环境变量:

echo $_ENV['COMPUTERNAME'];

Just make sure that the variables_order is set correctly in php.ini to include E such as EGPCS . 只需确保在php.ini中正确设置了variables_order即可包含E例如EGPCS

Or: 要么:

echo getenv('COMPUTERNAME');

here is my centos solution. 这是我的centos解决方案。 will be a little different from the windows solution. 与Windows解决方案略有不同。 but in the same spirit. 但本着同样的精神。 basically i just tag the cron job with the subdomain (vs server name). 基本上我只是用子域(与服务器名称)标记cron作业。 Then I check if this is being run on localhost or from apache and substitute $cron_host for the server variable as needed. 然后,我检查这是在localhost还是从apache运行,并根据需要用$ cron_host替换服务器变量。

Cron: Cron:

#crontab -u apache -e
30  3 * * * php /var/www/vhosts/the/script.php -h=staging.domain.com -t=11:30

cron_file.php: cron_file.php:

// get hostname from varg
$opts = getopt("h:t:");
$cron_host = $opts['h']; // which host to load from creds file
$time_max  = $opts['t']; // time parameter for database call

creds_file.php: creds_file.php:

$localhost = array( '127.0.0.1', '::1' );
$is_localhost = ( !is_array($_SERVER) || !isset($_SERVER['REMOTE_ADDR']) || in_array( $_SERVER['REMOTE_ADDR'], $localhost) ) ? true : false;
$cron_host    = (isset($cron_host)) ? $cron_host : false;
$host_name    = ($is_localhost) ? $cron_host : ((isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM