PHP

From Leo's Notes
Revision as of 05:08, 1 September 2019 by Leo (talk | contribs) (Text replacement - "Category:Linux" to "Category:Linux{{Navbox Linux}}")
This page was last edited on 1 September 2019, at 05:08.

PHP is a server-side scripting library.

Installation

The instructions here will compile the latest version of PHP as an apache module for a prepackaged apache installation under a RedHat based system (such as CentOS, Fedora, ScientificLinux). The steps below will make use of apxs2 from the http-tools package. If you wish to install PHP with suPHP, see suPHP.

## Install rpmforge repo
# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
# rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt
# rpm -K rpmforge-release-0.5.2-2.el6.rf.*.rpm
# rpm -i rpmforge-release-0.5.2-2.el6.rf.*.rpm

## Install all prerequisites.
# yum -y install make autoconf g++ gcc-c++ libtermcap-devel libgcc-c++ libtool gcc libpng-devel libjpeg-devel libxml1-devel libxml2-devel curl curl-devel libmcrypt libmcrypt-devel libtool-ltdl-devel libxml2.x86_64 libxml2-devel.x86_64 curl-devel.x86_64 openssl-devel httpd-devel 

## Compile PHP
## Go to the PHP source directory, then run:
# ./configure --with-apxs2 --with-config-file-path=/etc --with-gd --enable-shmop --enable-ftp --enable-sockets --enable-exif --enable-zip --enable-sysvsem --enable-sysvsem --enable-sysvshm --with-jpeg-dir --with-png-dir  --enable-mbstring --enable-embedded-mysqli=shared --with-curl --with-mcrypt --with-mysql --with-mysqli --with-openssl --with-zlib --with-pdo-mysql --with-libdir=lib64


Add --with-libdir=lib64 if you need to reference 64bit libraries. By default, configure will only search in /usr/lib rather than /usr/lib64.

Run make and make install. If you had enabled apache apxs2 above, then it should have enabled the PHP module in apache automatically in Template:/etc/httpd/conf/httpd.conf. If that failed for whatever reason, you could create an included config at /etc/httpd/conf.d/php.conf with the following contents:

#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
<IfModule prefork.c>
  LoadModule php5_module modules/libphp5.so
  # Of if you use PHP 7, use: LoadModule php7_module modules/libphp7.so
</IfModule>
<IfModule worker.c>
  LoadModule php5_module modules/libphp5-zts.so
</IfModule>


# Cause the PHP interpreter to handle files with a .php extension.

AddHandler php5-script .php
AddType text/html .php


#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
AddType application/x-httpd-php-source .phps


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

UTF-8 to hex

There is basically NO good way.

Error Reporting

// 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);