0

I try to run a PHP script from command line to check if a specific webserver is running, but it doesn't work.

This one works:

$to = 'mail@address.com';
$subject = 'LSWS Check';
$message = 'Apache is Running';
$headers = 'From: root' . "\r\n" . 'Reply-To: mail@address.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

$serverSoftware = $_SERVER['SERVER_SOFTWARE'];
$isWebserver = 'Apache';

mail($to, $subject, $message, $headers);

This one doesn't work:

$to = 'mail@address.com';
$subject = 'LSWS Check';
$message = 'Apache is Running';
$headers = 'From: root' . "\r\n" . 'Reply-To: mail@address.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

$serverSoftware = $_SERVER['SERVER_SOFTWARE'];
$isWebserver = 'Apache';

if (strpos($_SERVER[$serverSoftware], $isWebserver) !== false) {
  mail($to, $subject, $message, $headers);
}

strpos($_SERVER[$serverSoftware] doesn't work, but why?

  • Would the code in https://stackoverflow.com/questions/933367/php-how-to-best-determine-if-the-current-invocation-is-from-cli-or-web-server help solve this? – Nigel Ren Apr 02 '20 at 08:12
  • 2
    Not all $_SERVER variables are accessible from command line, do a var_dump($_SERVER); just to be sure. Also your `strpos($_SERVER[$serverSoftware], $isWebserver)` should be `strpos($serverSoftware, $isWebserver)` – Bert Maurau Apr 02 '20 at 08:14
  • how do you run the script from command line? – Gihan Apr 02 '20 at 08:19
  • Does this answer your question? [Accessing $\_SERVER variables from command line](https://stackoverflow.com/questions/24287716/accessing-server-variables-from-command-line) – Gihan Apr 02 '20 at 08:19
  • I checked variables with var_dump($_SERVER) and $_SERVER['SERVER_SOFTWARE'] isn't available. Is there any workaround? – Serpentdriver Apr 02 '20 at 08:25

0 Answers0