Extremist Developer

RSS

PHP Nested Comments using Redis

Just some random fun, a nested comments system using Redis only :

https://gist.github.com/803251

Jan 6

Debugging Apache2 / PHP on Ubuntu

  Yesterday i came across a little issue, after installing the Memcache extension (through the package manager on Ubuntu 10.10), my Apache was segfaulting. Since i just finished the setup of this box, they was too many possible culprit to just go ahead (and come on sunday) and just disable/reinstall the extension. So i decided to run Apache through gdb. 

It’s not as easy as it sounds, if you just go with you instincts and run (we’ll assume from now on that you are root):

gdb apache2

This will start gdb correctly, and if in gdb you type run -X , you will get this little message :

apache2: bad user name ${APACHE_RUN_USER}

The trick there is before running gdb (still as root) you will need to export those envvars (from /etc/apache2/envvars ) :

export APACHE_RUN_USER=www-data

export APACHE_RUN_GROUP=www-data

export APACHE_PID_FILE=/var/run/apache2.pid

export LANG=C

export LANG

Now if you run gdb again and type run -X , everything will start correctly and you’ll be able to see what is going on under the hood. 

RedisExtended

I decided to share my small library that i plugged on top of PhpRedis :

https://github.com/Mikushi/RedisExtended

Basically offers some extra functionality using Redis. I’ll be pushing more stuff, stack and message queue as soon as i have it fully tested locally.

The fun of writing PHP Extensions

Lately i’ve been digging into writing PHP extension, for multiple reasons, first knowing the language from the inside can never hurt (and i would recommend for every PHP developer to take a look at the PHP internals, you’ll learn a lot), secondly, PHP being my primary language, i wanted to contribute, i’m not much of a QA person so writing test, no thanks, so i decided to take a look at writing PHP extension, and last, i’m a curious person by nature, i love getting my hands in everything that looks fun - which in programming resolve to pretty much everything - so why not take a look at the heart of the beast.

So i began to read some stuff, starting by notes i’ve taken at a previous Confoo talk on writing PHP Extension (by Marcus Boerger and Johannes Schluter), that got me started, but not too far, i then started lurking on the internet, only to realize two things :

- There is no useful documentation of the Zend Engine, PHP engine and such. All you will find is a handful of articles that will introduce you to the extension writing process. (They are useful, don’t get me wrong) After that you are on your own, reading the source code of PHP and other extensions to understand the mechanisms.

- Getting help is almost impossible, as comparison, i’ve been playing around with NodeJS for the past months, and just hanging on the IRC channel will get you farther than anything available for PHP.

The only bright light in this very dark sky, is this book: http://www.amazon.com/Extending-Embedding-PHP-Sara-Golemon/dp/067232704X , Extending and Embedding PHP by Sara Golemon. I’m still going through it, but i would recommend it to anybody who plans to write a PHP extension. It’s a very complete book, that will get you most of the information you’ll need. 

But still, I was really disappointed by this lack of documentation and help, the PHP community is great, when you don’t try to dig too far. For the language and the community, PHP needs to better expose itself, it would help getting people interested, involved, and it would do some good to the language itself.

As i don’t want to look like the guy who’s bitching but not doing anything, starting 2011, i will try, on this blog, to document 1 function of the PHP Engine, Zend_Engine, per two days or so. That will only cover my limited knowledge of it all, but may be it will get the attention of more knowledgeable people in this matter.

On this note, happy holidays to all of you my 10 to 15 readers. 

Dec 9

Asynchronous JavaScript Loading

Last week i stumble upon a JavaScript problem, how to load a JavaScript asynchronously across all platforms. Usually i would just put the script tag at the bottom of my page to prevent any blocking, but in some case (advertisement) the script tag needed to be on the top.

The default script we were given looks like this :

The problem with this is pretty simple, it only behaves as a non-blocking loading in IE browsers, quite problematic when over 50% of your users are on other browsers. Looking at my stats, it turns out that pretty much anybody who doesn’t use IE as a browser that support HTML5, giving me access to the new async property for the script tag.

The new script (that works for IE and the rest) looks like this :

Extracting infos from Apache access log

In the process of writing a “log watcher” to plug in with Live Graph, i needed to extract all the info from an access log. Assuming we have the standard access log from Apache:

172.16.10.23 - - [29/Oct/2010:10:39:51 -0400] “GET / HTTP/1.1” 200 10890 “-” “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7”

So assuming i want to extract: IP, Date, Method, HTTP Code, Bytes sent and Use-agent, here is the Regex that does the trick for me:

(\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b)\s-\s-\s(.*)\s”([A-Z]*)\s\/\s[A-Z]+\/[0-9].[0-9]”\s(\d{1,3})\s([0-9]+)\s”-“\s”(.*)”

Might not be the best Regex to do so, but does the trick pretty good. 

As a side note, i use NodeJS to write the log watcher, and i gotta admit, it’s pretty amazing. Gonna post soon about it.

Autocomplete with Redis

Here is a PHP implementation of Antirez’s autocomplete example in Ruby using Redis. The full explanation for this algorithm can be found here:

http://antirez.com/post/autocomplete-with-redis.html

I’m using the Redis php extension, you can download it here:

http://github.com/owlient/phpredis

Here is the code :

Sep 3

My small secret project, LiveGraph, is slowly coming along, i’m mainly done with the base features, and now working on front-end integration. Soon more info about it.

Gorillaz: Honk Kong

Probably one of my favorite piece of music out there.

CSS3 Gradients generator

If you’re like me, and you never remember the CSS instructions, here is a very cool tool to generate css3 gradients. It will generate the codes for WebKit based browsers (Safari and Chrome) and for Firefox. Enjoy.