Skip to main content
Sep 5, 2017 6:35 AM UTC

PHP 7 character set issues

Hi. I’ve encountered a couple of issues with the handling of ASCII and EBCDIC in PHP 7 (these examples work correctly in PHP 5). I have PHP 7 CGIs working successfully using the requires environment variables and IHS definitions but these specific situations don’t seem to correctly handle the conversion of character sets.

The first is the exec() function. This can return an array of output generated by the externally called program. I have a PHP script calling a REXX script with the REXX returning multiple lines of output via the SAY statement. When the output arrives back to PHP it appears to be ASCII encoded EBCDIC and the array is not correctly populated.

Here is the PHP script called exec1:

#!/ported/php/bin/php-cgi
<?php
if($_GET["conv"]=="yes")
 $p=" | iconv -f IBM-1047 -t ISO8859-1";
else
 $p="";
$output=array();
exec("./exec2 input one".$p,$output,$rc);
echo "RC=$rc";
echo "<br />output[0]=".$output[0];
echo "<br />output[1]=".$output[1];
echo "<br />output[2]=".$output[2];
echo "<br />output[0]=".iconv("IBM-1047","ISO8859-1",$output[0]);
echo "<br />output[1]=".iconv("IBM-1047","ISO8859-1",$output[1]);
echo "<br />output[2]=".iconv("IBM-1047","ISO8859-1",$output[2]);
?>

Here is the external REXX called exec2:

/*rexx*/
parse arg in
say "hello world"
say in
say "last line"
exit 4

Navigating to exec1 in the browser shows that all of the exec2 output lines are placed as a single string in the first element of array $output and the string is EBCDIC so doesn’t display very well. Adding query string exec1?conv=yes passes exec2 output through iconv and $output is then populated correctly (this is not a very good solution). It seems as though the exec() function isn’t handling character conversion correctly as the output generated by REXX exec2 is EBCDIC.

The second situation involves POSTing form data back to the PHP script. It is also coming back as EBCDIC (probably due to IHS processing) and consequently the PHP $_POST superglobal is not populated correctly. Here is an example PHP script called testpost:

#!/ported/php/bin/php-cgi
<?php
$cginame=$_SERVER["PHP_SELF"];
echo "<form action=\\"$cginame\\" method=\\"post\\" name=\\"form1\\">\\n";
echo "<br /><strong>Edit1:</strong>";
echo "<input type=\\"text\\" value=\\"".$_POST["edit1"]."\\" ".
     "name=\\"edit1\\" size=\\"20\\">\\n";
echo "<br /><strong>Edit2:</strong>";
echo "<input type=\\"text\\" value=\\"".$_POST["edit2"]."\\" ".
     "name=\\"edit2\\" size=\\"20\\">\\n";
echo "<br /><input type=\\"submit\\" value=\\"Refresh\\" name=\\"refresh\\">\\n";
echo "</form>\\n";
echo "<br />Edit1: ".$_POST["edit1"];
echo "<br />Edit2: ".$_POST["edit2"];
echo "<br />";
print_r($_POST);
foreach($_POST as $n=>$v)
 echo "<br />".iconv("IBM-1047","ISO8859-1",$n).",$v";
?>

It’s not beautiful to look at but when run in the browser it shows that $_POST contains all of the variable information as a single string in a single array element. The script also passes $_POST through iconv to show that the data is actually EBCDIC by converting it to ASCII.

It’s most likely that something is misconfigured somewhere but it’s not clear what that might be so any guidance is appreciated.

Richard.

31 replies

Tatiana Balaburkina
Rocketeer
Sep 19, 2017 12:01 PM UTC

Hi

I managed to run PHP 7 in “CGI” mode, but I have an error in “FastCGI” mode that I can not solve:

[error] [client 126.32.101.80] FastCGI: incomplete headers (115 bytes) received from server “/local/basepro/m4stats/php/php.fcgi”, referer: http://tmvs:8080/

In Apache conf file I have :

CharsetSourceEnc IBM-1047
CharsetDefault ISO8859-1
AddType application/x-httpd-php .php

In the shell that runs the PHP script I have (both for CGI mode and FastCGI mode) :

export _BPXK_AUTOCVT=ON
export CGI_HEADER_ENCODING=EBCDIC
export CGI_BODY_ENCODING=EBCDIC

PHP scripts and php.ini are IBM-1047 encoded.

Thanks,

Denis

Hi Denis,

Could you provide detailed description, I mean step by step?
Also could you create separated topic for your issue?

Thanks,
Tatyana

Tatiana Balaburkina
Rocketeer
Sep 21, 2017 3:51 AM UTC

I’m not sure if it’s related to this particular issue but I’ve also found that file uploads don’t appear to work in PHP 7. This can be observed by adding enctype=“multipart/form-data” to my POST example above along with an input field of type=“file”. Adding a vardump($_FILES) returns an empty array as does vardump($_POST) so not even ASCII characters are getting back to the PHP script. This same scenario works correctly with Rocket PHP 5.4.4 so it seems something in PHP 7 is flushing the multipart payload.

None of these specific issues were encountered in Rocket PHP 5.4.4.

Hi,

We think there is a problem in your environment, because now we are not able to reproduce this issue. I added short config, which we use in our work. Could you try to use it?

config.tar (10 KB)

Sep 21, 2017 3:55 AM UTC

Hi. I’ve encountered a couple of issues with the handling of ASCII and EBCDIC in PHP 7 (these examples work correctly in PHP 5). I have PHP 7 CGIs working successfully using the requires environment variables and IHS definitions but these specific situations don’t seem to correctly handle the conversion of character sets.

The first is the exec() function. This can return an array of output generated by the externally called program. I have a PHP script calling a REXX script with the REXX returning multiple lines of output via the SAY statement. When the output arrives back to PHP it appears to be ASCII encoded EBCDIC and the array is not correctly populated.

Here is the PHP script called exec1:

#!/ported/php/bin/php-cgi
<?php
if($_GET["conv"]=="yes")
 $p=" | iconv -f IBM-1047 -t ISO8859-1";
else
 $p="";
$output=array();
exec("./exec2 input one".$p,$output,$rc);
echo "RC=$rc";
echo "<br />output[0]=".$output[0];
echo "<br />output[1]=".$output[1];
echo "<br />output[2]=".$output[2];
echo "<br />output[0]=".iconv("IBM-1047","ISO8859-1",$output[0]);
echo "<br />output[1]=".iconv("IBM-1047","ISO8859-1",$output[1]);
echo "<br />output[2]=".iconv("IBM-1047","ISO8859-1",$output[2]);
?>

Here is the external REXX called exec2:

/*rexx*/
parse arg in
say "hello world"
say in
say "last line"
exit 4

Navigating to exec1 in the browser shows that all of the exec2 output lines are placed as a single string in the first element of array $output and the string is EBCDIC so doesn’t display very well. Adding query string exec1?conv=yes passes exec2 output through iconv and $output is then populated correctly (this is not a very good solution). It seems as though the exec() function isn’t handling character conversion correctly as the output generated by REXX exec2 is EBCDIC.

The second situation involves POSTing form data back to the PHP script. It is also coming back as EBCDIC (probably due to IHS processing) and consequently the PHP $_POST superglobal is not populated correctly. Here is an example PHP script called testpost:

#!/ported/php/bin/php-cgi
<?php
$cginame=$_SERVER["PHP_SELF"];
echo "<form action=\\"$cginame\\" method=\\"post\\" name=\\"form1\\">\\n";
echo "<br /><strong>Edit1:</strong>";
echo "<input type=\\"text\\" value=\\"".$_POST["edit1"]."\\" ".
     "name=\\"edit1\\" size=\\"20\\">\\n";
echo "<br /><strong>Edit2:</strong>";
echo "<input type=\\"text\\" value=\\"".$_POST["edit2"]."\\" ".
     "name=\\"edit2\\" size=\\"20\\">\\n";
echo "<br /><input type=\\"submit\\" value=\\"Refresh\\" name=\\"refresh\\">\\n";
echo "</form>\\n";
echo "<br />Edit1: ".$_POST["edit1"];
echo "<br />Edit2: ".$_POST["edit2"];
echo "<br />";
print_r($_POST);
foreach($_POST as $n=>$v)
 echo "<br />".iconv("IBM-1047","ISO8859-1",$n).",$v";
?>

It’s not beautiful to look at but when run in the browser it shows that $_POST contains all of the variable information as a single string in a single array element. The script also passes $_POST through iconv to show that the data is actually EBCDIC by converting it to ASCII.

It’s most likely that something is misconfigured somewhere but it’s not clear what that might be so any guidance is appreciated.

Richard.

Thanks Tatyana, I’ll have a look and implement any differences.

When you say “we are not able to reproduce” do you mean that…

  1. Calling an external REXX via exec() from a PHP 7 CGI returns displayable characters in the output array?
  2. Posting values back to a PHP 7 CGI results in the $_POST superglobal being populated correctly?
  3. File uploads using PHP 7 result in the file being uploaded correctly and both the $_POST and $_FILES superglobals are correctly populated?
Sep 25, 2017 5:04 AM UTC

Hi,

We think there is a problem in your environment, because now we are not able to reproduce this issue. I added short config, which we use in our work. Could you try to use it?

config.tar (10 KB)

Hi Tatyana,
I tried out the httpd.conf (which is http 8.5 based) you provided. I created an http 8.5 based test server for this.

As we use a directory cgi-bin I added

ScriptAlias /cgi-bin/ "/u/testhttp/server/cgi-bin/"

For PHP I added
SetEnv PHPRC /u/testhttp/server/conf

To be able to start and stop the server using MVS commands, I added:

LoadModule zos_cmds_module modules/mod_zos_cmds.so

Then I tried a shell script, a REXX script and a simple PHP script. In all cases I get EBCDIC output in Firefox.

When trying out Richard’s exec1 I get

RC=4
output[0]=�����@����������@�������@����
output[1]=
output[2]=
output[0]=hello world input one last line
output[1]=
output[2]=

Trying Richard’s testpost I also get EBCDIC garbage in Firefox.

Hm, being confused now as you said your httpd.conf did work.

– Manfred

Tatiana Balaburkina
Rocketeer
Sep 25, 2017 7:16 AM UTC

Hi Tatyana,
I tried out the httpd.conf (which is http 8.5 based) you provided. I created an http 8.5 based test server for this.

As we use a directory cgi-bin I added

ScriptAlias /cgi-bin/ "/u/testhttp/server/cgi-bin/"

For PHP I added
SetEnv PHPRC /u/testhttp/server/conf

To be able to start and stop the server using MVS commands, I added:

LoadModule zos_cmds_module modules/mod_zos_cmds.so

Then I tried a shell script, a REXX script and a simple PHP script. In all cases I get EBCDIC output in Firefox.

When trying out Richard’s exec1 I get

RC=4
output[0]=�����@����������@�������@����
output[1]=
output[2]=
output[0]=hello world input one last line
output[1]=
output[2]=

Trying Richard’s testpost I also get EBCDIC garbage in Firefox.

Hm, being confused now as you said your httpd.conf did work.

– Manfred

Manfred,

One more detail. PHPRC should refer to php.ini file. This setting should be set in php.ini:

cgi.fix_pathinfo=0

Sep 25, 2017 8:42 AM UTC

Manfred,

One more detail. PHPRC should refer to php.ini file. This setting should be set in php.ini:

cgi.fix_pathinfo=0

That setting is in our php.ini.

I am pretty sure that if PHPRC points to a directory it is assumed there is a php.ini in that directory.

Tatiana Balaburkina
Rocketeer
Sep 25, 2017 9:09 AM UTC

That setting is in our php.ini.

I am pretty sure that if PHPRC points to a directory it is assumed there is a php.ini in that directory.

Yes, you are right.
Which version of http server and z/OS do you use?

Sep 25, 2017 9:11 AM UTC

Yes, you are right.
Which version of http server and z/OS do you use?

z/os 2.1, and in this case I used http 8.5 as your httpd.conf was built for 8.5

Tatiana Balaburkina
Rocketeer
Sep 26, 2017 6:36 AM UTC

Thanks Tatyana, I’ll have a look and implement any differences.

When you say “we are not able to reproduce” do you mean that…

  1. Calling an external REXX via exec() from a PHP 7 CGI returns displayable characters in the output array?
  2. Posting values back to a PHP 7 CGI results in the $_POST superglobal being populated correctly?
  3. File uploads using PHP 7 result in the file being uploaded correctly and both the $_POST and $_FILES superglobals are correctly populated?

Hi Richard,

I meant all these scenarios works correctly in our system.

Nov 7, 2018 9:20 AM UTC

Hi Richard,

I meant all these scenarios works correctly in our system.

Is there any news on this problem? I also have POST values not returned in the correct codepage.