Top posts

Latest articles


PHP and browser detection

Posted by Jongerius under Development, Webdevelopment, WordPress
1 Star2 Stars3 Stars4 Stars5 Stars6 Stars (No Ratings Yet)
Loading ... Loading ...

One of the things you might encounter as a web developer is detection of the browser that a visitor is using. In my specific case this was needed since I wanted to use some CSS3 styling, but only if the browser was capable. In all other cases the visitor had to see the CSS2 optimized page.

So I started looking into a way to let my web application determine what browser was visiting and what capabilities it had. Since this was for the new WordPress theme I’m developing it had to be in PHP and be sorta light-weight.

First thing was to figure out the basic browser that was visiting. This can be done by doing a simple strpos call in PHP on the user agent property. Having a list of the various user agents helped, I found just such a list at UserAgentString. A brief summary for some common browsers is below.

  $user_agent = $_SERVER['HTTP_USER_AGENT'];
  $IE        = strpos($this->_user_agent, 'MSIE') > -1;
  $IPad      = strpos($this->_user_agent, 'iPad') > -1;
  $Firefox   = strpos($this->_user_agent, 'Firefox/') > -1;
  $Chrome    = strpos($this->_user_agent, 'Chrome/') > -1;
  $Opera     = strpos($this->_user_agent, 'Opera/') > -1;
  $Android = strpos($this->_user_agent, 'Android') > -1;

As you can see determining the basic browser type is pretty simple. But to be able to determine if CSS3 is supported you will also need to locate the version of the browser. Keeping in mind that IE9+, Firefox 4+, Opera 10+ and Chrome are the only versions that support CSS3. The check for this is slightly different for each version. But you can do it easily with a regular expresion.

 // For all non IE browsers
 $locator;
 if ($Firefox) $locator = "Firefox\/";
 if ($Android) $locator = "Android\s";
 if ($Chrome) $locator = "Chrome\/";
 if ($Opera) $locator = "Version\/";
 preg_match('/'.$locator.'([0-9]{1,4}(.[0-9]{1,3})+)/', $user_agent, $version);

 // For IE
 preg_match('/MSIE\s([0-9]{1,2}.[0-9]{1,2})/', $user_agent, $version);
 if (count($version) > 1) $version = $version[1];
 else $version = 0;

Having the version and the type of browser allows us to do all kinds of fancy browser specific stuff. Mostly introduce hacks for IE7 (yes many are needed), or add CSS3 support.


PHP autoloader continued

Posted by Jongerius under Development, Webdevelopment
1 Star2 Stars3 Stars4 Stars5 Stars6 Stars (No Ratings Yet)
Loading ... Loading ...

A couple of days back I wrote something about some huge issues I had with the PHP autoloader technology. After reading up some more on the autoloader and some help from the PHP developers I finally tracked down the issue.

As it turns out the autoloader functionality is called when PHP is unable to find the class you are using automatically itself as a last effort. Though that wasn't the issue I encountered. It took me about 3 hours of debugging the function to find out there was a problem with the logic of the autoloader function I wrote myself, doh!

Something with breaking out of a loop to early :-( . After fixing this issue my autoloader was working beautifully. So I am happy to say that it is possible to write a modular PHP web application after all.


PHP autoloader issues

Posted by Jongerius under Development, General Rant, Webdevelopment
1 Star2 Stars3 Stars4 Stars5 Stars6 Stars (No Ratings Yet)
Loading ... Loading ...

Since PHP 5.x we are encouraged to develop in a more object oriented way. This is why they developed class support for us ;-) . Now to make our web applications even more modular you might consider putting each class in a seperate PHP file, just like Java and C++ developers have been doing for years now. But here is where it becomes tricky, how does PHP know which files to load automatically.

Of course you could include every single file that you might need manually at the top of your index.php. But what if you don't need every single class every time a page is loaded, do you really want to create that kind of an overhead in loading un-needed files? I didn't, so I started looking into the autoloader functionality of PHP. What this feature does is allow you to scan through files in the predefined include directories and attempts to locate the class.

So what I did was build a simple autoloader function to scan each file in predefined directories to find the classes, and if found to stop. To my surprise this was working rather well. All classes that where needed would automatically get loaded by my little autoloader function. However somehow one particular class was not able to be loaded for some reason. The setup was something simple, something like:

  • A predefined class A
  • A class B which extended class A
  • A class C which extended class B
  • A class D which extended class C, and was located in a separate directory as it was specific for the website

All of the classes loaded beautifully, except for class D. For some reason when I tried using it I got exceptions that class C was undefined. When I added some debugging to find out which files where loaded I did see every file for classes A, B and C being loaded, but the actual classes defined inside where not added to PHP. Somehow I have magically created a black whole in PHP that would include files and then forget to create the class definitions internally. To this day I still haven't figured out why PHP refused to store the actual classes in its memory.

The quick fix I applied was ugly and stupid, just manually load the classes and build a big if statement around them to prevent duplicate declarations using the class_exists function.