Category Archives: php

Building a php extension using phpize

Run piece of PHP code in command line

php -r "echo(mcrypt_module_open('rijndael-256', '', 'ofb', ''));"

Then got an error message about Mcrypt:

Fatal error: Call to undefined function mcrypt_module_open() in Command line code on line 1

Install mcrypt for PHP53 on centos by phpize

cd php-5.5.9/ext/mcrypt/
make install

Add the myrypt extension config /etc/php.ini containing:

And check if the mcrypt module loaded or not by php -m | grep "mcrypt".

You may get some errors like the following

PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/local/php5/lib/php/extensions/no-debug-non-zts-20121212/' - cannot open shared object file: No such file or directory in Unknown on line 0

To fix it, enter yum install libmcrypt-devel

php is NULL

I’ve some codes like:

// do something...

$json = json_decode($data);
if ($json == NULL) {
    $json = array("message" => "data null");
// do some out print
// ....

But, i hadn’t got the empty json result with message “data null” when $data is empty array();

After some google searches. references to

Note: empty array is converted to null by non-strict equal ‘==’ comparison. Use is_null() or ‘===’ if there is possible of getting empty array.

$a = array();
$a == null  <== return true
$a === null <== return false
is_null($a) <== return false

Fast, Effective PHP Compression

Normally, we just need config the apache setting to enable Gzip compression features.

# Enable compression (gzip compression) – Apache Server (httpd)
# (start)
<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    # file-types indicated will not be compressed
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|rar|zip|pdf)$ no-gzip dont-vary
    <IfModule mod_headers.c>
        Header append Vary User-Agent
<IfModule mod_log_config.c>
    <IfModule mod_deflate.c>
        DeflateFilterNote Input instream
        DeflateFilterNote Output outstream
        DeflateFilterNote Ratio ratio
        SetEnvIf Request_URI \.(?:gif|jpe?g|png|rar|zip|pdf)$ ignore-log
        SetEnvIf Request_URI \.html image-request
        LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
        CustomLog /var/log/apache2/deflate.log deflate env=!ignore-log

<IfModule mod_expires.c>
    # enable expirations
    ExpiresActive On
    # expire GIF images after a month in the client's cache
    ExpiresByType image/gif "access plus 1 month 15 days 2 hours"
    # test that cached version is not used for the request
    ExpiresByType text/html "access plus 30 seconds"
    # check with cached version
    #ExpiresByType text/html "access plus 30 days"

##### gzip configuration - (end)

Alternate Method

Place the following code before the (X)HTML content in any PHP script:

<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>

In this case, the ob_flush() command is unnecessary as PHP inherently flushes the buffer. The script delivers gzipped content to capable browsers and uncompressed content to incapable browsers.