PHP

Posts about the server side scripting language PHP

PHP Password Functions In 5.5

4th March 2014 - 5 minutes read time

New in PHP 5.5 is a group of functions that deal with password hashing and verification. This is such a common thing for PHP applications to do that it was decided to include it into the core of PHP. They effectively solve the problem of hashing and comparing passwords that just about every PHP developer has implemented at one point or another.

There are only a few functions available but they provide all of the functionality needed to create a hash value from a password, check if the hash is valid and to check if the password hash needs to be recreated.

To create a hash value from a password use the password_hash() function. The first parameter is the password string and the second value is the hashing algorithm to use. The value PASSWORD_DEFAULT here is a PHP constant that is currently set to the bcrypt algorithm and will be changed to better algorithms when and if they are found in newer versions of PHP.

PHPNW13: A Review

14th October 2013 - 18 minutes read time

PHPNW13 is the 6th annual PHPNW conference, organised by members of the PHPNW community and Magma Digital. This year the conference saw around 420 people (with myself as a helper) at the conference, which was held in the Manchester Conference Centre.

My involvement in PHPNW13 started a few months before the actual conference. When the call for papers closed back in June I spent a weekend reading the submissions so that we could pick which sessions would be at the conference. Out of the 183 papers submitted (20 more than last year) we had to pick just 35 or so sessions that would be presented at the weekend. The final selection of talks was really good and judging by the comments and rating on joind.in they were well received by the other conference attended as well.

The Monty Hall Problem In PHP

24th September 2013 - 12 minutes read time

The Monty Hall problem is a counter intuitive problem in probability mathematics that deals with picking the right prize from a set of three doors. The problem is named after the television celebrity Monty Hall and is loosely based on the USA game show Let's Make a Deal.

This has become a popular problem in programming as it is a good exercise in thinking through a problem to prove what outcome actually occurs. Lots of examples have been posted online so I thought I would sit down and attempt to solve it myself. The problem is most commonly summarised as follows (this example was taken from Rosetta Code):

Sieve of Eratosthenes In PHP

5th July 2013 - 4 minutes read time

The sieve of Eratosthenes is named after Eratosthenes of Cyrene who was a Greek mathematician who devised a mechanism to find a sequence of prime numbers using a simple algorithm.

Normally, looping through a list of numbers and finding the primes can be an expensive process. The seive of Eratosthenes is one of the most efficient way of working out all of the smaller prime numbers below (below 10 million or so).

The sieve works by looping through a list of consecutive numbers, starting at 2. For each number in the sequence the multiples of that number are marked to be removed from the list of numbers. When finished the numbers that are not marked are prime numbers.

This algorithm is pretty straightforward, but from that it is possible to create a simple PHP function that will generate all the prime numbers up to a given number.

Using XPath With HTML Files In PHP

14th March 2013 - 10 minutes read time

I recently have started looking into making myself a PHP Zend Certified Engineer and after doing a bit of research I found that the standard PHP string and array functions appear to be a large part of the exam material. So as a starting point (and for future revision) I decided it might be a good idea to create a revision sheet for those functions.

How I Learned To Stop Using strtotime() And Love PHP DateTime

1st March 2013 - 7 minutes read time

The DateTime classes in PHP have been available since version 5.2, but I have largely ignored them until recently. This was partly due to the fact that I was working in PHP 5.1 environments a lot (don't ask) but mostly because I was just used to using the standard date functions that have always been a part of PHP (well, since version 4). I wanted to explain why I will be using the new DateTime classes more from now on and why you shouldn't be hesitant to use them.

Using a combination of strtotime() and date() can handle most things and is a good method to quickly grab a date.

Bookmarklet To Run XDebug Profiler

5th February 2013 - 2 minutes read time
XDebug is a great PHP debugging tool, but it also comes with a very useful profiler that can tell you all sorts of information about your PHP application. This includes things like memory footprint and CPU load but will also have detailed information about the entire callstack of the code that was run. To enable the profiler part of XDebug you just need to set up a few rules in your xdebug.ini file.

Xdebug Debugging On A Remote Server

20th January 2013 - 3 minutes read time

I have started to use virtual machines to develop sites rather than installing a local web server. This allows me to replicate the exact setup of the server I will be deploying to with ease. For each virtual machine I set up a shared folder which allows me to store the files locally whilst being able to run the code on the virtual machine. One thing I missed was the ability to use xdebug to debug the sites through Netbeans, so I set about trying to set up the virtual hosts to allow me to use xdebug remotely.

All that is needed was to add a xdebug.remote_connect_back clause to the xdebug.ini file found in the PHP configuration. Set this value to 1 to automatically connect back to any xdebug session that is created on the server.

Automating Headless Selenium PHPUnit Tests

15th January 2013 - 7 minutes read time

I have talked before about running Selenium tests in PHPUnit but I have only recently come to properly automate things. Getting a Selenium server to start and stop in a script is relatively easy and can be done in a simple script. My original script for running a directory of PHPUnit tests was as follows. I will explain more about how this all works later on in this post.

Storing Automated YSlow Tests With Show Slow

3rd December 2012 - 8 minutes read time

I was at a meeting of the Manchester Web Performance Group the other day where Tom Taylor gave a talk about some of the performance testing tool he uses at Laterooms.com. He used a ruby script to set up some preferences in Firefox which then ran Selenium to open some web pages and test them with YSlow. The results of the YSlow inspection are then sent to a Show Slow server where the results can be graphed over time. I realise that I've just mentioned a whole stack of technologies there, so let me pick out the important ones:

Selenium is a remote control agent for web browsers, although it is most stable in Firefox. I have written about this tool before but it allows us to automate interaction with a website via a series of selenium scripts. These scripts can be exported into different code formats, including PHP.