Category Archives: php

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. Read more »

Share

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. Read more »

Share

Speeding up client/server response times

In the past 6 months I’ve switched jobs from being a web developer to being a server side game developer.  So far it’s been an excellent career shift.  I get to focus on my true passions, intelligent back-end code and no longer having to waste my time with frustrating design challenges (there is a separate team that does that).

Having said that, when developing large Facebook virtual worlds, there are a lot of client/server communication.  For example, each time someone buys something, each time you buy something, etc…  Currently, a lot of games wait for the server to respond, but why should we?  There are a lot of server calls that are done for informational purposes; just to keep the database up-to-date.  So I ask you, why should the end user wait for the server to catch up?  Let’s examine a simple approach to alleviate the need for the client to wait. Read more »

Share

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. Read more »

Share

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. Read more »

Share

How to add comments with AJAX in CakePHP

In today’s article we are going to create a very basic blog that allows people to create a post and posts comments on that post via AJAX.  We are going to keep it extremely basic and just focus on that actual AJAX functionality.

Ready? Let’s begin.  We are going to start by creating two database tables: posts and posts_comments.  Below is a sample create statement for the posts table: Read more »

Share

How to advance our CMS in CakePHP

At the end of part one, we had a working CMS.  Well, at least it was saving files and they could be displayed to the public.  In today’s article, we are going to further advance our CMS.  We will cover the following items:

  1. fckEditor
  2. Revisions

Read more »

Share

How to create a CMS with CakePHP

I really enjoying writing code and I find that snippets just don’t always cut it for me.  So in today’s article, I am going to describe the process of creating a CMS (Content Management System) with CakePHP.

This will be a two part article, in part one we will focus on getting the basics working.  The basics will include ability to add, edit, and delete static content pages.

Part two will advance on our basis and allow us to create drafts and revert back to previous versions.

To begin, let’s download the latest release of CakePHP.  After you’ve downloaded it, extract it to a folder of your choice.  In my case it will be c:\xampplite\htdocs\CMS.  I can now access my new web site by browsing to http://localhost/CMS. Read more »

Share

How to create a socket server in PHP

Ever want to build a chat application or perhaps even a game? If so, a socket server will help you get started. Once you understand the underlying functionality of creating the server, enhancing it is just as easy.

The way a socket server works is, it will be running continously and waiting for a client to connect to it. When a client connects, our server will add it to our list of clients and begin waiting for messages from that client.

Without further adieu, here is the full source code:

Read more »

Share

Buy one of my books