PHP: Difference between revisions

From Leo's Notes
This page was last edited on 12 September 2019, at 02:10.
Removed old installation content
imported WP content for mail issues
Line 4: Line 4:
== Installation ==
== Installation ==
Refer to the Dockerfiles at https://git.steamr.com/docker/php-fpm if you wish to install PHP-FPM manually.
Refer to the Dockerfiles at https://git.steamr.com/docker/php-fpm if you wish to install PHP-FPM manually.
== Stuff ==
=== Error Reporting ===
To always show all errors and warnings:
{{highlight|lang=php|code=
error_reporting(E_ALL);
ini_set('display_errors', 1);
}}
In more detail:
{{highlight|lang=php|code=
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR {{!}} E_WARNING {{!}} E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR {{!}} E_WARNING {{!}} E_PARSE {{!}} E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
}}
=== UTF-8 to hex ===
There is basically NO good way.
* http://www.tokyomuslim.com/2008/01/php-convert-utf-8-to-hex-codepoint-values-unicode-hexidecimal/
* http://hsivonen.iki.fi/php-utf8/


=== Troubleshooting ===
=== Troubleshooting ===
Line 63: Line 106:
}}
}}


==== UTF-8 to hex ====
==== {{code|mail()}} issues ====
There is basically NO good way.
If {{code|mail()}} returns {{code|sh: -t: command not found}} when attempting to run the {{code|mail()}} function, you probably did not set the {{code|sendmail_path}} variable in your {{code|php.ini}}.


* http://www.tokyomuslim.com/2008/01/php-convert-utf-8-to-hex-codepoint-values-unicode-hexidecimal/
Change:
* http://hsivonen.iki.fi/php-utf8/
{{highlight|lang=text|code=
; sendmail_path =
}}


==== Error Reporting ====
To:
To always show all errors and warnings:
{{highlight|lang=text|code=
{{highlight|lang=php|code=
sendmail_path = /usr/sbin/sendmail -t -i
error_reporting(E_ALL);
ini_set('display_errors', 1);
}}
}}


In more detail:
If you get {{code|Recipient names must be specified}} when calling {{code|mail()}}, you need to set the {{code|-t}} flag in {{code|sendmail_path}} in {{code|php.ini}}.
{{highlight|lang=php|code=
// Turn off all error reporting
error_reporting(0);


// Report simple running errors
error_reporting(E_ERROR {{!}} E_WARNING {{!}} E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR {{!}} E_WARNING {{!}} E_PARSE {{!}} E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
}}


{{Navbox Linux}}[[Category:Linux]]
{{Navbox Linux}}[[Category:Linux]]
[[Category:Code]]
[[Category:Code]]

Revision as of 02:10, 12 September 2019

PHP is a server-side scripting language.


Installation

Refer to the Dockerfiles at https://git.steamr.com/docker/php-fpm if you wish to install PHP-FPM manually.

Stuff

Error Reporting

To always show all errors and warnings:

error_reporting(E_ALL);
ini_set('display_errors', 1);

In more detail:

// Turn off all error reporting
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

// Report all PHP errors
error_reporting(-1);

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);


UTF-8 to hex

There is basically NO good way.


Troubleshooting

HTTP POST Array Empty

I had an issue where a PHP POST array was empty when a POST request was made. It turns out that PHP now needs the Content-Type header set for it to be populated regardless of the HTTP method being used.

In detail, when the following request was made, $_POST was empty:

POST /print HTTP/1.1
Connection: Keep-Alive
User-Agent: WebSlipPrinting
Content-Length: 56
Host: 172.20.1.151

template_data=Hello There!&Username=test&Password=123456

The data though was still sent since $_SERVER['CONTENT_LENGTH'] was 56 as in the original request. The data was also present in the php://input stream. ie:

var_dump(file_get_contents("php://input"));
# Returned:
# string(56) "template_data=Hello There!&Username=test&Password=123456"

Adding the Content-Type value to the header fixes the issue, with the final request as:

POST /print HTTP/1.1
Connection: Keep-Alive
Content-Type: application/x-www-form-urlencoded
User-Agent: WebSlipPrinting
Content-Length: 56
Host: 172.20.1.151

template_data=Hello There!&Username=test&Password=123456


This may have something to do with the enable_post_data_reading option in PHP. From the config file:

647 ; Whether PHP will read the POST data.
 648 ; This option is enabled by default.
 649 ; Most likely, you won't want to disable this option globally. It causes $_POST
 650 ; and $_FILES to always be empty; the only way you will be able to read the
 651 ; POST data will be through the php://input stream wrapper. This can be useful
 652 ; to proxy requests or to process the POST data in a memory efficient fashion.
 653 ; http://php.net/enable-post-data-reading
 654 ;enable_post_data_reading = Off

PDOException : could not find driver

You need to have the pdo driver installed. In my case, I was trying to use the pdo-mysql connector which wasn't compiled with PHP. To fix this, install the PDO driver or compile it manually with:

--with-pdo-<driver>

In my case, I needed mysql:

--with-pdo-mysql

mail() issues

If mail() returns sh: -t: command not found when attempting to run the mail() function, you probably did not set the sendmail_path variable in your php.ini.

Change:

; sendmail_path =

To:

sendmail_path = /usr/sbin/sendmail -t -i

If you get Recipient names must be specified when calling mail(), you need to set the -t flag in sendmail_path in php.ini.