Archive for month: June, 2012

The power of session_write_close()

29 Jun
June 29, 2012

If you’re anything like me, you’ve probably created an infinite loop or two in your day. One misconception that I had however, was that a single script that entered an infinite loop was causing my entire server to get locked up, because I couldn’t reload the page or load any other page. I always just restarted my web server (Doh!)

Recently, I learned that the cause for this behaviour was completely different then what I thought. PHP employs session locking, so if you have a script that uses sessions, only one script can run at a time for a single user, since that script locks the session file and the other script has to wait for the file to be unlocked. This is another reason why I experience slow loading times when I utilize PHP scripts to load images or other files.

This problem can be solved however. Once you are past the point in your application in which you need to modify the session, you can call the function session_write_close(). This frees up the session file for the next script, and is an invaluable function once you know about it.

Running background tasks in PHP

28 Jun
June 28, 2012

This post is mostly to introduce some cool ways of doing tasks in PHP, that you may not be aware of. The recommended approach for anything like this is to use a cron job however.

One of things I often do in PHP is create code that needs to run occasionally and perform some function that can take several minutes or more. A good example of this would be parsing an uploaded file into a structured format like a database. You don’t want to keep the user waiting for this, and you may not always have the option to do it properly via a cron job.

An alternative solution exists, and that is to call the task after the page has been loaded. You do this by ignoring a dropped browser connection, via the function ignore_user_abort(). PHP scripts are basically limited by two factors: The maximum execution time, which defaults to 30 seconds, and the connection to the web browser. Read more →

Using token_get_all() and better source highlighting

13 Jun
June 13, 2012

I recently discovered a very useful PHP function called token_get_all(), which allows you to tap into the Zend Engine that parses PHP (Written in C, so very fast). The function accepts a string, containing PHP code and will return the tokenized output as an array. The array will contain many elements, each of which may be a single character (Such as =, ;, or even “), or an array containing 3 values: The token type, represented as an integer, the token text itself (a T_COMMENT token would contain the actual comment), and the line number that the token started on. Hint: You can get the “nice” token name by calling the token_name() function on the token type.

This function allows you to do a lot of different things, such as built powerful debugging capabilities, source code coverage tools, or build an awesome syntax highlighting library like I did. Most libraries just use some basic regex, and you end up with OK syntax highlighting, but nothing special. Using the library I built, you can easily get syntax highlighting that rivals editors like Sublime Text 2 or Eclipse. I’ve put it on GitHub, so go check it out!

Or check out this example of it in action.

My next mini-project will be writing this as a WordPress plugin :)