Category: PHP

  • CakePHP Version Comparison with PHP Version Comparison

    I’ve seen a few recent blog articles comparing the new version of PHP 5.4 to its predecessors and I thought I should get involved with this a bit as well.

    To perform this test, I will layout the conditions I have chosen.  I’m trying to keep this as simple as possible.  I currently run a Dell Laptop with Windows 7 on it:

    Windows NT 6.1 build 7601 (Unknown Windows version Business Edition Service Pack 1) i586

    Because I often do a lot of .NET development recently I have PHP running as a CGI under IIS 7.5.

    I then created a very simple .NET application that performs 100 requests of the same web page and tracks the response time.  These lists of response times are sorted and the highest and lowest responses are dropped.  The average is then calculated from this.

    (more…)

  • Building A Scalable Queueing System With PHP

    In today’s article we are going to cover building a queueing system with PHP.  Before we begin, let’s define what a queueing system is.  The best place to start is the dictionary:

    “to line up or wait in a queue”

    Now that we have our definition, let’s define why we would want to build a queueing system.  A queueing system is an excellent tool that will allow us to take a specific process and perform the functionality “offline”, e.g. the process will line up and we will process them one at a time at a later date.  This will probably be easier to explain with an example.

    Imagine an admin area of a website that allows the administrator to send out a mass email to all of their users.  The simple process to building this functionality would be as follows:

    1. Build a form that accepts a subject and a body for the email.
    2. Retrieve the list of users from your database.
    3. Loop through the users and send each person an individual email.

    The above example works nice and fast when there are only a few hundred users.  However, imagine trying to send this email to 10,000 users.  The administrator would be waiting a long time for this process to finish.  Not only that, if they closed the browser, it probably would not finish properly.

    So, the goal of our queueing system is to remove a specific process from running “online” (in a web browser) and running it “offline” with a scheduled task. (more…)

  • Maintaining a session in a session-less environment

    Confused?  I know I was at first, but let me explain.  First, why would there be a session-less environment?  I thought this was a HUGE plus to server-side development languages over basic HTML that is session-less?  Well, you would be right in that sense; however, as I mentioned in a recent blog that I’ve switched careers and I am currently doing server-side game development for large Facebook Virtual Worlds.  The client/server relationship in these games are completely session-less.  Each time the client performs an action, the server doesn’t “know” who they are because it’s not a consistent relationship like a browser and a web server.

    Don’t worry, there is a simple solution to this problem, let’s explore it now. (more…)

  • The flaws of using isset()

    I am starting to really dislike the PHP function isset().  Today, I was working on a registration system in CakePHP and my password validation was not working.  If I left the password field blank and clicked submit, it would come back with other errors, but then the password would come back populated with a long string – a hashed version of an empty string!

    After some investigation, I discovered that the AuthComponent in CakePHP was doing an isset() check on the username and password fields.  If isset() returned true for both, it would hash the password. (more…)

  • Re-map key/value array data in PHP

    I was recently tasked with a situation where I needed to populate about 10 different “Settings” for every user in the current database.

    This example is specifically for CakePHP, however, it could easily be used elsewhere.  My goal was the following, I had an array that was key value paired as follows:

    array(
     [0] => array(
      ‘User’ => array(
       ‘id’ => 1)
      ),
     [1] => array(
      ‘User’ => array(
       ‘id’ => 2)
      ),
     [2] => array(
      ‘User’ => array(
       ‘id’ => 3)
      ),
     [3] => array(
      ‘User’ => array(
       ‘id’ => 4)
      ),
    )

    The result I needed was as follows:

    array(
     [0] => array(
      ‘UserSetting’ => array(
       ‘user_id’ => 1)
      ),
     [1] => array(
      ‘UserSetting’ => array(
       ‘user_id’ => 2)
      ),
     [2] => array(
      ‘UserSetting’ => array(
       ‘user_id’ => 3)
      ),
     [3] => array(
      ‘UserSetting’ => array(
       ‘user_id’ => 4)
      ),
    )

    With a few simple lines of code, I was able to quickly and easily achieve this, let me show you how. (more…)