posts in the category: Programming

Posted by: bob on Monday October 17th 2011 2 comments
Tags: extension, geoip, php

During the past few weeks I have been working on a brand new PHP extension to provide an easy object oriented interface for the GeoIP library from MaxMind. The original module (that I did not write) had two main issues. First the directory to find the databases could not be changed except in PHP.INI before PHP loads (so not even ini_set would work). Second was that it was old school and not object oriented. Another issue (so that really makes three) is that it will throw E_WARNINGS for dumb things like records not being found. If there is no record found when you ask for it I would much rather just be handed a FALSE.

[update 2011/10/26] Official GeoIPo manual/documentation is here.

This new one is nearly complete, but I am still waiting to be accepted into PECL. However it works and I have the source available for use on GitHub. Here is a little how-to on how to set up the GeoIP database and use this module.

Why use GeoIP?

  • Perhaps your project has support for English, German, and Polish languages. With GeoIP you can try to guess what country your visitor is from and if they are from a country with a language you support you can set that language default for the user. Things like that go very far towards user experience.
  • If you have a website, I doubt many people can honestly tell me they are not curious what country their visitors are from. GeoIP is an important part of determining your reader demographics. You know all those maps that show dots about where visitors are from? You need GeoIP info to do that.
  • And the bane of all internet users – lets say you are the owner of YouTube/Spotify/iTunes and you need to prevent a certain country from viewing/watching/listening to specific content because of license restrictions. You are going to piss people off, but I guess for legal reasons it just needs to be done.

These are only three of the many valid reasons you might need GeoIP information. Over here I have a page demoing the readout of the server and visitor’s GeoIP info, with a link to the visible source at the bottom.

Continue reading about setting up and using php-geoipo

Posted by: bob on Thursday July 28th 2011 1 comment
Tags: fork, queue

The next logical step in the development of our queue service is to allow the application to push itself into the background of a system and run detached from a terminal.

What is forking?

Forking is when you take one process, and clone it to create another with the same state as the original. When our application executes we will clone it, let the new copy take ownership of itself, and terminate the original. This leaves a background process or daemon running in the background still ready to process our queue.

PHP by default does not have the functions we need enabled. In order to enable them PHP will need to be compiled with the option –enable-pcntl. This will make the pcntl_* function set available to us.

Wanna fork?

Forking is a fairly simple process. The best time to fork is early on in the program, because when we do it literally everything about the application state gets copied over. This means file handles, database pointers, everything… and this usually causes problems. Fork before you open your files and databases, and you will find your life running much smoother.

Continue reading about forking our process

Posted by: bob on Wednesday July 27th 2011 2 comments
Tags: named pipe, queue

In this part of the series we will add the ability to accept input from named pipes to the queue server, an important feature so that the queue can actually be given things to do. Again, I would like to point out that it is assumed we are running on a Unixesque operating system such as Linux, BSD, or Solaris.

What are Named Pipes?

Named Pipes are special files that the kernel can create allowing for buffered byte streams. They operate on the principle of FIFO, where the first thing put into it is the first thing it spits out. This is perfect for our queue system as we want to process the first thing put into it first. To store data into a named pipe, it is as easy as `echo lol > /path/to/pipe` and reading the data back out is as easy as `cat /path/to/pipe`. An interesting note about named pipes is that by default they block until there is both a reader and a writer. This means the echo command will hang until another terminal is opened and the cat command is executed.

Here is a quick video demonstrating how the queue system will work by the end of this post. We will be able to launch the server, and send input to it from another terminal via the named pipe.

Continue reading about using Named Pipes

Posted by: bob on Tuesday July 26th 2011 no comments
Tags: array, iteration, queue

To get this project rolling, we are going to start by building a really basic queue system structure. It will start of simple and not very cool, and over the next few blogs we will evolve it into awesomesauce.

What is a Queue?

A queue is just a list of things that need to get done, just like when you stand in line at the cafe and the counter girl can only take one order at a time. Imagine the line of people as an Array, and the counter girl some processor function, and the entire store itself as a giant loop that always exists. The only difference is instead of selling us coffee the counter girl is going to look up website IP addresses for us.

Here is what the initial queue system is going to look like before we evolve it in later posts.

 

Continue reading about building the base queue loop…

Posted by: bob on Tuesday July 26th 2011 no comments
Tags: daemon, fork, named pipe, queue

So to be hip, I am going to write one of these multipart blog things that describe building a simple project from start to finish. Are you ready? O.K. here we go.

What is a Queue Server?

A queue server is a server that processes a queue, which is why it is so aptly named. You send it a list of things to do and it does them over time based on your parameters, be it “as fast as possible” or “when system resources allow” or “once an hour” it is your call.

Over the course of the next week or so I am going to demonstrate building a simple queue server that will be able to fork itself off as a background daemon, accept input through a named pipe, and process things as fast as it can until it runs out of things to do – and when that happens it will sit there waiting with the upmost desire for you to provide it with more jobs.

Continue reading about this project…

Posted by: bob on Tuesday May 24th 2011 no comments
Tags: php, typecast

Twice now this week, which is impressive as it is only Tuesday, questions about variable types have popped up on IRC chat about how strings get evaluated and converted. PHP has the soft variable type system that allows you to do things like compare strings to integers, or take a variable that used to be an integer and store a string in it instead. This is unlike the strict typing of languages like C where when you create an integer variable, you better not be trying to store anything except an integer in it.

To hopefully clarify things for more people and also create a link I can send instead of re-explaining it every time, lets take a look at comparing and converting strings.

Continue reading about comparing and evaluating strings…

Posted by: bob on Tuesday May 10th 2011 no comments
Tags: function, namespace, performance, php

To continue on with the previous post, On the speed of functions and namespaces, it was brought to my attention that namespaces should be faster with aliasing. Also how does it compare to static functions? The news was not good for either. Another thing that was brought to my attention that to avoid a lookup when in a namespace functions like filter_var should be absolute referenced, so we/I tried that too.

Continue reading to see the additional test

Posted by: bob on Tuesday May 10th 2011 2 comments
Tags: function, namespace, performance, php

My day started off fairly simple, I wanted to write a function that could take a string of things like “True” and “Yes” and return boolean true, thusly “False” and “No” would return me boolean false. PHP has a function that can handle the basics of this already, filter_var, however it only works on variants of true and false, not yes or no. Keeping with PHP’s strtolower and strtoupper naming I named this function strtobool and stuck it in my utility function namespace.

I then compared it speedwise to the built-in filter_var function, even though filter_var cannot do what I want at the scope I want it – it would still be a good test to see if it was even worth supporting “Yes” and “No”. Here are the results of that test.

Continue reading about the speed of PHP functions and namespaces

Posted by: bob on Thursday March 31st 2011 3 comments
Tags: cairo, compile, php-gtk, ubuntu

This is an update to a document I wrote 4 years ago about compiling PHP-GTK on any Unixesque system you could get your hands on, but this one only for Ubuntu 10+. At the time of writing I was using a freshly installed Ubuntu 10.10 system to do this, so for the sake of completeness we are going to assume the system has nothing we need as though it was fresh off the installation disk a few minutes ago.

Here is an overview of what we have to do. The order is very important. If you do not follow the proper order things will not work right. This becomes very important when it comes time to build PHP-GTK itself.

  • Install subversion.
  • Install PHP5.
  • Install GTK development packages.
  • Install the Cairo module for PHP.
  • Get, patch, and compile PHP-GTK.

And here is how we do it. You are going to need a terminal window.

Continue reading about compiling PHP-GTK on Ubuntu

Posted by: bob on Thursday March 17th 2011 no comments
Tags: events, gtkprogressbar

Understanding the how to deal with pending events is an important part of programming with GTK. While not technically accurate (in any regards), it is useful to think of a PHP-GTK application as two threads or processes working together for one program. In one hand you have PHP running the program, on the other you have the GTK library running an idle loop to process its UI. If you hard lock PHP into a processing (for, while, foreach) loop GTK will be unable to process its updates until PHP finishes, and visa versa.

Here is an example where you might run into this issue. Lets say you have a directory full of files you need to read, and a window displaying a progress bar. These are two symptoms you might encounter:

  1. the appearance that your program has hardlocked and must be killed, and then at the last minute your progress bar jumps to 100%.
  2. your progress bar updates sporadically jumping large amounts instead of smoothly across.

… and both of these, generally, can be solved very easily. Most of the time just telling GTK to process events is enough to do it. And how do we do that?

Continue reading about GTK Pending Events…