Category Archives: php

Install mcrypt PHP extension on Mac OS X Lion

Important: This article was originally published in 2011, and I wouldn’t recommend you use mcrypt in your PHP project any more (reasons why). If you absolutely need it… then read on….

mcrypt php extension

If you have a need to install the mcrypt extension for PHP, for example if you need to use phpMyAdmin, Magento or Laravel, then these instructions are for you.

Thankfully, it is becoming simpler to install PHP extensions than with previous versions of OS X.

Xcode

The Xcode package installs the necessary tools which are needed during the PHP extension compilation process. Make sure you have at least Xcode v4.1 installed; the install package (“Install Xcode”) is available in your “Applications” folder.

If you are running Xcode 4.3 or above, please check you have the command line tools installed before proceeding.

Autoconf

The most recent version of Xcode does not include autoconf which is a pre-requisite. To check that you have it, run the following command in your Terminal:


autoconf --version

If you see output similar to the following, then it means you have it installed — skip to the libmcrypt section.


autoconf (GNU Autoconf) 2.61
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License .
There is NO WARRANTY, to the extent permitted by law.

Written by David J. MacKenzie and Akim Demaille.

If you get a command not found error, then you will need to perform the following to install autoconf:


cd /tmp
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
tar zxvf autoconf-latest.tar.gz
cd autoconf-2.69
./configure; make
sudo make install

libmcrypt

libmcrypt is the library that provides the encryption functions. We need to install this before building the PHP extension.

  • Open up Terminal.app
  • Download libmcrypt from sourceforge
  • Unpack the archive somewhere
  • cd libmcrypt
  • ./configure
  • make
  • sudo make install

PHP Extension

Once we have libmcrypt installed, we can build the PHP extension. One important point: even though PHP 5.3.8 is now the default since Mac OS X 10.7.3, attempting to compile with these sources results in the following error:

php-5.3.8/ext/mcrypt/mcrypt.c:283: error: ‘PHP_FE_END’ undeclared here (not in a function)

Therefore, we need to use PHP 5.3.6 sources which compiles fine and still works with 10.7.3.

You should see output similar to the following:

Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
  • ./configure
  • make
  • sudo make install

On successful install, you will see the following message:

Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20090626/

To enable the module, include the following line in your /etc/php.ini file:

extension = mcrypt.so

Finally, restart your Apache server:

sudo apachectl restart

And that’s it: you should now be able to utilise the functions, as detailed in the PHP mcrypt documentation.

P.S. – Looking for a reliable hosting provider for your PHP projects? I recommend Clook:

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Mac OS X Lion PHP upgrade – php.ini and Suhosin

If you have upgraded from Snow Leopard to the new OS X Lion, you will notice PHP has also been upgraded – from 5.2 to 5.3.

A couple of points that I noticed post-install. Firstly, my existing /etc/php.ini file was moved to /etc/php.ini-5.2-previous. Restoring this was trivial:

sudo cp /etc/php.ini-5.2-previous /etc/php.ini

However, I noticed that extensions previously installed under /usr/lib/php/extensions/no-debug-non-zts-20090626/ had been removed. So, if you have extensions that you wish to still use with 5.3, they will need to be re-built.

I also noticed that Apple have included the Suhosin patch and extension. Suhosin is part of the Hardened-PHP project which aims to protect PHP applications against buffer-overflow and format string vulnerabilities. In theory, this functionality should be transparent to your application – no configuration or code changes are required.

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Install mcrypt PHP extension on OS X Snow Leopard

Lock away the evil...

Please note: The following instructions apply to Mac OS X 10.6 (Snow Leopard). I have an updated guide for how to install mcrypt on 10.7 (Lion).

mcrypt is a useful extension to PHP if you would like to support a wide range of encryption algorithms within your code.

This guide explains how you can enable install mcrypt, along with the PHP extension, on Mac OS X 10.6.

Xcode

The Xcode package installs the necessary versions of tools like autoconf which is needed during the PHP extension compilation process. Make sure you have at least Xcode v3.2 installed; the install package is available on the Snow Leopard install DVD under the “Optional Installs” folder.

libmcrypt

libmcrypt is the library that provides the encryption functions. We need to install this before building the PHP extension.

  • Open up Terminal.app
  • export CFLAGS="-arch x86_64"
  • Download libmcrypt from sourceforge
  • Unpack the archive somewhere
  • cd libmcrypt
  • ./configure --disable-shared
  • make
  • sudo make install

PHP Extension

Once we have libmcrypt installed, we can build the PHP extension.

You should see output similar to the following:

Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
  • ./configure
  • make
  • sudo make install

On successful install, you will see the following message:

Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20090626/

To enable the module, include the following line in your /etc/php.ini file:

extension = mcrypt.so

Finally, restart your Apache server:

sudo apachectl restart

And that’s it: you should now be able to utilise the functions, as detailed in the PHP mcrypt documentation.

mcrypt php extension

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Normalize URL’s with PHP

I’ve posted to GitHub a PHP class that I’ve written which can handle URL normalization, as specified by RFC 3986.

https://github.com/glenscott/url-normalizer

Specifically, the following normalization steps are performed:

  1. Normalize case
  2. Decode unreserved characters
  3. Remove dot segments

An example of use:

require_once 'URLNormalizer.php';
 
$url = 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d';
$un = new URLNormalizer();
$un->setUrl( $url );
echo $un->normalize();

// result: "example://a/b/c/%7Bfoo%7D"

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Install memcached PHP extension on OS X Snow Leopard

Memories

memcached is a very useful memory object caching system, which can be used to increase the performance of your dynamic scripts by caching database calls.

This guide will explain how to install the memcached system, including the PHP extension, on Mac OS X 10.6.

Xcode

The Xcode package installs the necessary versions of tools like autoconf which is needed during the PHP extension compilation process. Make sure you have Xcode 3.2 installed; the install package is available on the Snow Leopard install DVD under the “Optional Installs” folder.

libevent

libevent is a pre-requisite for memcached.

  • cd /tmp; curl -O http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz
  • tar zxvf libevent-1.4.12-stable.tar.gz
  • cd libevent-1.4.12-stable
  • ./configure; make
  • sudo make install

memcached

memcached is the daemon responsible for actually storing and retrieving arbitrary objects for your applications.

  • cd /tmp; curl -O http://memcached.googlecode.com/files/memcached-1.4.1.tar.gz
  • tar zxvf memcached-1.4.1.tar.gz
  • cd memcached-1.4.1
  • ./configure; make
  • sudo make install

libmemcached

libmemcached is the shared library that will allow clients, in this case PHP, access the memcached daemon.

  • Download libmemcached, move to /tmp and unpack
  • cd libmemcached-0.31
  • ./configure; make
  • sudo make install

php extension

Now we are ready to prepare the PHP extension to memcached, which is available from pecl.

  • cd /tmp; pecl download memcached
  • gzip -d < memcached-1.0.0.tgz | tar -xvf -
  • cd memcached-1.0.0; phpize

You should see output similar to the following:

Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626

  • ./configure; make
  • sudo make install

On a successful install, you will get the following message:

Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20090626/

Modify your php.ini configuration file and make sure you have the following line included:

extension = memcached.so

You can then restart your Apache server:

  • sudo apachectl restart

to make the memcached functionality available in your scripts.

memcached php extension

References

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Fix PHP timezone warnings in OS X Snow Leopard

The standard Mac install of PHP has always been somewhat quirky, and 10.6 is no exception. One of the most obvious issues occurs when attempting to use date/time functions. PHP 5.3 requires that the date.timezone setting is available. Without this, you will receive a warning similar to the following:

Warning: getdate() [function.getdate]: It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'Europe/London' for 'BST/1.0/DST' instead in /Users/gscott/Sites/hello-world.php on line 9

The solution is to edit (or create, if it doesn’t exist) /private/etc/php.ini and make sure a setting exists for date.timezone. For example:

date.timezone = Europe/London

After making the change, restart Apache

sudo apachectl restart

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

PHP 5.3: The Good, the Bad and the Ugly

PHP 5.3 was released today; here are my positive and negatives:

The Good: Closures

Anonymous functions created with create_function have always been a bit messy. With 5.3 comes support for closures with a much cleaner syntax:

    $greet = function($name)
    {
        printf("Hello %s\r\n", $name);
    };

    $greet('World');
    $greet('PHP');

The Bad: Backwards incompatible changes

Although not a massive change from 5.2, there are enough differences to break existing code.

The Ugly: Namespaces

There has been quite a discussion about PHP’s new namespace syntax.

$c = new \my\name\MyClass;

It’s just damn ugly. The more sensible choice, :: is already used as the scope resolution operator and was therefore dismissed.

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

mcrypt support for PHP on 64 bit Mac OS X 10.5

Please note: The article below refers to installing mcrypt on Mac OS X 10.5. If you have 10.6 Snow Leopard, please see my updated guide: Install mcrypt PHP extension on OS X Snow Leopard

Adding additional functionality to the standard Apple-supplied PHP on Mac OS X 10.5 is a little tricky if you are running a 64 bit processor such as the Intel Core 2 Duo. The reason is that any dynamic extensions that you add will need to be 64 bit, and many shared libraries by default will compile as 32 bit binaries. Trying to use a 32 bit extension with a 64 bit PHP results in the following unfriendly error message:

PHP Warning: PHP Startup: Unable to load dynamic library './mcrypt.so' - (null) in Unknown on line 0

mcrypt is a good example of a useful extension that can be added to PHP with a little bit of effort:

  • Open up your Terminal.app
  • To explicitly build for 64 bit architecture export CFLAGS="-arch x86_64"
  • Download libmcrypt from sourceforge http://sourceforge.net/projects/mcrypt
  • Unpack the archive
  • cd libmcrypt
  • ./configure --disable-shared
  • make
  • sudo make install
  • download PHP 5.2.6 source from http://www.php.net/get/php-5.2.6.tar.bz2/from/a/mirror
  • unpack the archive and go into the php-5.2.6/ext/mcrypt/ dir
  • phpize
  • ./configure
  • make
  • sudo make install
  • verify the extension is 64 bit: file /usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so

/usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so: Mach-O 64-bit bundle x86_64

To actually use the extension, you can simply create a symbolic link to it. For example:

  • cd ~/Sites
  • ln -s /usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so

Example code: mcrypt.php

Drop the following code into your ~/Sites directory to verify everything is working:

    <?php
    
    if ( ! extension_loaded('mcrypt') ) {
        dl('mcrypt.so');
    }
    
    $key   = "this is a secret key";
    $input = "Let us meet at 9 o'clock at the secret place.";
    
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted_data = mcrypt_generic($td, $input);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    
    print_r($encrypted_data);
    
    ?>

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

PHP Best Practices #2: Strictures

Make sure PHP is reporting all errors and warnings.

Warnings and notices are PHP’s way of letting you know that you are utilising features in a non-standard way. If your code is omitting errors, then it should be fixed as soon as possible. Strict warnings are a class of errors that are turned off by default in most PHP installations. Turning on strict warnings gives you another level of error reporting, which is helpful to prevent you from using misusing functions, making sure your code is compatible with future PHP versions.

In the example here, using date() without explicitly setting a timezone will result in an E_STRICT warning. In this case, we are using a function called strictErrors to tell PHP to report all error types.

Example

    <?php
    
    function strictErrors() {
        $reporting_level = E_ALL;
        
        // if version of PHP < 6, explicity report E_STRICT
        if ( version_compare( PHP_VERSION, '6.0.0', '<' ) ) {
            $reporting_level = E_ALL | E_STRICT;
        }
        
        return error_reporting( $reporting_level );
    }
    

    // turn on strict error and warnings
    strictErrors();
    
    // produces strict warning
    echo date( "d-M-Y" );
    
    // no warning
    date_default_timezone_set( 'Europe/London' );
    echo date( "d-M-Y" );
    
    ?>

Further information

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

PHP Best Practices #1: Regular expressions

preg not ereg

This is the first in what I hope will be a regular series of posts on PHP best practices, inspired in part by Damian Conway’s Perl Best Practices book.

Historically, PHP has had two incompatible regular expression engines available, POSIX Extended and PCRE (Perl Compatible Regular Expressions). Arguably, Perl-compatible regular expressions are more powerful since they support non-greedy matching, assertions, conditional subpatterns and a number of other features not supported by POSIX Extended. More importantly, the POSIX Extended extension is deprecated as of PHP 5.3, meaning that a call to any of its functions, such as `ereg`, will emit a `E_DEPRECATED` notice. Therefore, for any regular expression functionality in your scripts, use the PCRE set of functions which are prefixed with `preg_`.

Example

    $pattern = '/Hello/x';
    $subject = 'Wake up and say Hello.';
    $matches = array();
    
    if ( preg_match( $pattern, $subject, $matches ) ) {
        print_r( $matches );
    }

Output:

    Array ( [0] => Hello )

Further information

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn