Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve detection of PHP CLI #19

Open
bobbingwide opened this issue Mar 10, 2017 · 10 comments
Open

Improve detection of PHP CLI #19

bobbingwide opened this issue Mar 10, 2017 · 10 comments

Comments

@bobbingwide
Copy link
Owner

bobbingwide commented Mar 10, 2017

When trying to run some new code under oik-batch to set letter taxonomy terms I discovered that the value of the PHP constant PHP_SAPI was not constant!

Whereas php_sapi_name() was correctly returning "cli", PHP_SAPI was changing from "cli" to "cgi-fcgi".

My code, which tested the value of PHP_SAPI was not being invoked.

Is this truly unexpected behaviour?

Current situation

Using a workaround but not a happy bunny.

Could be a hosting problem?

@bobbingwide
Copy link
Owner Author

This is my code that checks if it's running under CLI.

function is_cli() { 
	echo PHP_EOL;
	echo "php_sapi_name: ";
	echo php_sapi_name();
	echo PHP_EOL;
	echo "PHP_SAPI: ";
	echo PHP_SAPI;
	echo PHP_EOL;
	echo "constant(): ";
	echo constant( 'PHP_SAPI' );
	echo PHP_EOL;

	$is_cli = false;
	switch ( PHP_SAPI ) {
		case 'cli':
			$is_cli = true;
		  break;
		
		case 'cgi-fcgi':
			$is_cli = true;
			bw_trace2( PHP_SAPI, "PHP_SAPI unexpected?", false );
			break;
		
		default:
	}
	return( $is_cli );
}

This is the output I see

php_sapi_name: cli
PHP_SAPI: cgi-fcgi
constant(): cgi-fcgi

and this is the output from tracing.

/home/bworguk/public_html/wp-content/plugins/oik-bwtrace/includes/bwtrace-anychange.php(53:64) bw_trace_anychange(4373) 4847 2017-03-11T14:11:20+00:00 7.862819 0.000207 cf=run_oik-shortcodes.php,save_post,query 52 0 20971520/20971520 2048M F=312 anychange current value for: PHP_SAPI cli
/home/bworguk/public_html/wp-content/plugins/oik-bwtrace/includes/bwtrace-anychange.php(53:64) bw_trace_anychange(4374) 4848 2017-03-11T14:11:20+00:00 7.864455 0.001636 cf=run_oik-shortcodes.php,wp_insert_post 53 0 20971520/20971520 2048M F=312 anychange current value for: PHP_SAPI cli
/home/bworguk/public_html/wp-content/plugins/oik-a2z/oik-a2z.php(186:0) is_cli(1) 4849 2017-03-11T14:11:20+00:00 7.864606 0.000151 cf=run_oik-shortcodes.php,wp_insert_post 53 0 20971520/20971520 2048M F=312 PHP_SAPI unexpected? cgi-fcgi
/home/bworguk/public_html/wp-content/plugins/oik-bwtrace/includes/bwtrace-anychange.php(53:64) bw_trace_anychange(4375) 4850 2017-03-11T14:11:20+00:00 7.864887 0.000281 cf=run_oik-shortcodes.php,wp_insert_post,query_post_type_letter_taxonomy_filters 53 0 20971520/20971520 2048M F=313 anychange current value for: PHP_SAPI cli
/home/bworguk/public_html/wp-content/plugins/oik-bwtrace/includes/bwtrace-anychange.php(53:64) bw_trace_anychange(4376) 4851 2017-03-11T14:11:20+00:00 7.865438 0.000551 cf=run_oik-shortcodes.php,wp_insert_post,oik_a2z_query_terms_post_letters 53 0 20971520/20971520 2048M F=313 anychange current value for: PHP_SAPI cli

@bobbingwide
Copy link
Owner Author

bobbingwide commented Mar 11, 2017

I'm not going to give up with this just yet.

I can't clone the code to different folders and reproduce the problem in those folders.
But I have managed to reproduce it in a different environment from where I first noticed it.
Which is a start.

The problem can be simplified to.

  1. Invoking routine ( called /oik-a2z/issue-19.php )
<?php // (C) Copyright Bobbing Wide 2017
echo "PHP_SAPI: " .  PHP_SAPI. PHP_EOL;   
if ( !function_exists( "add_action" ) ) { function add_action() {} }
require( "../oik-a2z/oik-a2z.php" );
$cli = oik_a2z_get_php_sapi();
echo "cli: " . $cli . PHP_EOL;
  1. New function in /oik-a2z/oik-a2z.php
function oik_a2z_get_php_sapi() {
	return PHP_SAPI;
}
  1. Execute php issue-19.php
bworguk@c17064 [~/public_html/wp-content/plugins/oik-a2z]# php issue-19.php
PHP_SAPI: cli
cli: cgi-fcgi

The expected output is:

PHP_SAPI: cli
cli: cli

@bobbingwide
Copy link
Owner Author

bobbingwide commented Mar 12, 2017

This is the simplest demonstration of the problem.

Setup

  1. PHP Environment with OPcache enabled, Linux server.
  2. Two files in a folder called path/issue-19.

browsed.php

<?php
function get_php_sapi() {
	return PHP_SAPI;
}

issue-19.php

<?php
echo "PHP_SAPI: " .  PHP_SAPI. PHP_EOL;   
require( "browsed.php" );
$cli = get_php_sapi();
echo "cli: " . $cli . PHP_EOL;

Steps

Step Where Action
1 Browser Visit http://example.com/path/issue-19/issue-19.php
2 CLI cd path/issue-19
3 CLI touch issue-19.php
4 CLI php issue-19.php

For step 1. the result is expected to: PHP_SAPI: cgi-fcgi cli: cgi-fcgi
For step 4. the result changes to: PHP_SAPI: cli cli: cgi-fcgi

To get the other combinations simply touch a file and try again.
You should be able to get all 4 combinations in both the browser and on the Command Line.
The value of PHP_SAPI depends on which routine loads the changed file into the OPcache.

So is it a constant or isn't it?

@bobbingwide
Copy link
Owner Author

Note: I can't reproduce the problem in my Windows server running Apache.
In the browser the output is: PHP_SAPI: apache2handler cli: apache2handler
From CLI it's: PHP_SAPI: cli cli: cli

@bobbingwide
Copy link
Owner Author

This problem could also explain the "That's odd" message from oikb_check_time_limit() which was being displayed every now and then for no obvious reason.

@mattheu
Copy link

mattheu commented Mar 17, 2017

Tested this on a chassis vagrant box (nginx, php7.1-fpm) and worked fine.

In the browser: PHP_SAPI: fpm-fcgi cli: fpm-fcgi
In the command line; PHP_SAPI: cli cli: cli

@bobbingwide
Copy link
Owner Author

bobbingwide commented Mar 19, 2017

Thanks Matt.Alain Schlesser got the same result as you. SiteGround are going to try as well. I'm going to check which php executable gets called. Your result may be different because of Xdebug.

@bobbingwide
Copy link
Owner Author

In my .bashrc file php is set as an alias to php-cli

alias php='/usr/local/php70/bin/php-cli'

I get the same problem with both PHP 7.0.x and PHP 7.1.x

php -v gives:

PHP 7.0.16 (cli) (built: Feb 20 2017 11:16:52) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v6.0.8, Copyright (c) 2002-2016, by ionCube Ltd.
with Zend OPcache v7.0.16, Copyright (c) 1999-2017, by Zend Technologies

@bobbingwide
Copy link
Owner Author

bobbingwide commented Mar 30, 2017

On different hosting I got the same results as Matt and Alain.
Note: OPcache is not enabled on this hosting.

[oikcouk@host58 public_html]$ php issue-19.php
PHP_SAPI: cli
cli: cli
[oikcouk@host58 public_html]$ php -v
PHP 7.0.15 (cli) (built: Jan 19 2017 14:20:34) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
[oikcouk@host58 public_html]$

@bobbingwide
Copy link
Owner Author

This is a belated follow up to the issue that I reported at WordCamp London
and which Anton from SiteGround has investigated.

I have today reproduced the problem on https://rowlandscastle.org.uk/path/issue-19.php

See below for more details.

Extract from Anton's response, dated 4th May 2017.

Dear Herb Miller,
My name is Anton and we met at WordCamp London this year.
First I would like to apologize for the late reply, I had a small incident with my arm which rendered me unable to type for a while.
The reason for this email is our conversation about an issue you have described on this page:
#19

I tried to replicate the issue on my end to no avail. I was using my shared hosting account as well as a Cloud plan and I get consistent results both when executed through browser and shell.

Today's tests ( 23 Jun )

Replication of problem on rowlandscastle.org.uk

With PHP 7.0.16

Implemented the code referenced in #19 (comment) under account rcorguk

./  ../  browsed.php  issue-19.php

set up a php alias for PHP 7

alias php='/usr/local/php70/bin/php-cli'

Run tests. The results demonstrated the same problem as before.

Note: php -v gives

PHP 7.0.16 (cli) (built: Feb 20 2017 11:16:52) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v6.0.8, Copyright (c) 2002-2016, by ionCube Ltd.
    with Zend OPcache v7.0.16, Copyright (c) 1999-2017, by Zend Technologies

With PHP 7.1.2

alias p71='/usr/local/php71/bin/php-cli'

Used cpanel to change PHP version of the path folder to 7.1.2

PHP 7.1.2 (cli) (built: Feb 20 2017 17:16:31) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.2, Copyright (c) 1999-2017, by Zend Technologies

I was able to reproduce the problem.
Currently, when I visit https://rowlandscastle.org.uk/path/issue-19.php
the result is

PHP_SAPI: cgi-fcgi cli: cli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants