Writing a custom unix style tail in PHP using Libevent API on Mac OS X 10.5.x and other platforms

Posted 16 CommentsPosted in PHP
Libevent is a library which provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Many famous applications/frameworks/libraries like memcached are using libevent. In this blog post, I will demonstrate how to write a custom unix style tail script using Libevent API in PHP. Setting up the environment: Setting up libevent with PHP is a little tricky. Below are the steps, I followed to make it work on Mac OSX 10.5. However the steps should be same for any other OS you choose to code on....

Use crumbs to protect your Ajax calls from Cross-site request forgery (CSRF/XSRF)

Posted 18 CommentsPosted in PHP
Have your API calls ever being used directly by someone without your permission? If yes, read on to find out how can we protect our API's from such spammers and hackers. Before we go ahead and see a possible solution for this, lets try to list out a few cases, when our API's can be accessed without our permissions. Common cases of vulnerable API/Ajax calls Ajax calls having no user authentication: This is the first place where a spammer will try to find out a loop hole. Take this example, suppose I created a group chat plugin for my blog....

Building a Custom PHP Framework with a custom template caching engine using Output Control functions

Posted 10 CommentsPosted in PHP
In past 1 year or so, I had opportunities of using a lot of php frameworks including zend, symfony, cakephp, codeigniter. All frameworks have their pros and cons, however that is out of scope of this blog post. You may want to checkout this comparison list of php frameworks here. In this blog post I will build a custom PHP framework (MVC Architecture). Then go on to discuss in brief about the output control functions and finally show how to build a custom template caching engine using these functions for our framework. Source Code You may want to download the...

XML Parsing in PHP, XPATH way – The best I know so far

Posted 34 CommentsPosted in PHP
If you are a PHP developer, you surely must have done XML parsing at some stage or the other. Over the years I myself have implemented XML Parsing in atleast 3-4 different ways. Finally I have stuck to this approach which I personally find far more better than the rest, Not only because it's quite simple but also because it's extendable. By extendable I mean, you don't have to touch your code if the XML structure changes at a later stage or if you need to parse a new node at a later stage in the project. In this blog...

Facebook type image rotation and more using PHP and Javascript

Posted 30 CommentsPosted in PHP
If you are a facebook geek like me, you must have noticed till now the image rotate functionality in the photo albums. Facebook allows you to rotate images 90 degree clockwise and anti-clockwise after image upload. If you haven't tried that till now, below is a screenshot for your convenience. Question: But the question is how does facebook team succeed doing this in one click. Today I tried looking around for a solution over internet and I came across the inbuilt imagerotate functionality in PHP. Problem: Unfortunately the problem is that even if you have GD Library extension enabled in...

Memcached and “N” things you can do with it – Part 1

Posted 19 CommentsPosted in PHP, Scalability
In my last post, MySQL Query Cache, WP-Cache, APC, Memcache - What to choose, I discussed in brief about 4 caching technologies which you might have used knowingly or unknowingly. Towards the end, we came to the conclusion that Memcached is the best caching solution when you are looking for speed and number of hits per second. By my experience, Memcached is capable of handling more than 100 Million PV's per month without any problem. However, towards the end, I did also discuss why memcached is unreliable and insecure. In this post, I will dig a level deeper into memcached....

MySQL Query Cache, WP-Cache, APC, Memcache – What to choose

Posted 44 CommentsPosted in PHP, Scalability
Hello Cache Freaks, Ever since I changed my job (from Business Intelligence to Web development) and started working with my present employer, I have had a chance to work on a lot of scalable projects. From making my project to scale from 20 Million PV's to 100 Million PV's to development of an internal tool, the answer to all scalable applications have been caching. There are a lot of caching techniques which are being employed by sites worldwide. WP-Cache used in wordpress - a file system based caching mechanismAPC Cache - an opcode based caching systemMemcache - an in memory...

Behind the scenes – How and What XML’s are exchanged by JAXL

Posted Leave a commentPosted in Open Source, PHP, XMPP
Pre-requisite Though it’s not mandatory but will be helpful if you have given a casual reading to the following RFC’s: Extensible Messaging and Presence Protocol(XMPP):Core Extensible Messaging and Presence Protocol(XMPP):Instant Messaging and Presence Case Study: Google Talk Server JAXL Sends >> <!--?xml version="1.0"?--> <stream:stream xmlns:stream="http://etherx.jabber.org/streams" version="1.0" xmlns="jabber:client" to="gmail.com" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace"></stream:stream> Gtalk Acknowledges With >> <!--?xml version="1.0" encoding="UTF-8"?--> <stream:stream from="gmail.com" id="981E0522D7363BDF" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"> <stream:features>   <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls">     <required>   </required></starttls>   <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">     <mechanism>X-GOOGLE-TOKEN</mechanism>   </mechanisms> </stream:features></stream:stream> JAXL Sends >> <starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls> Gtalk Acknowledges With >> <proceed xmlns="urn:ietf:params:xml:ns:xmpp-tls"></proceed> JAXL Sends >> <!--?xml version="1.0"?--> <stream:stream xmlns:stream="http://etherx.jabber.org/streams" version="1.0" xmlns="jabber:client" to="gmail.com" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace"></stream:stream> Gtalk Acknowledges With >> <!--?xml version="1.0" encoding="UTF-8"?--> <stream:stream from="gmail.com" id="C01610C43D6A37A2" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"> <stream:features>   <mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">     <mechanism>PLAIN</mechanism>     <mechanism>X-GOOGLE-TOKEN</mechanism>   </mechanisms> </stream:features></stream:stream> JAXL Sends >> <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AGFjY192nbRbm3J1cGvxYWRzAG15QVRNcGlukT9zcpMxAjI=</auth> Gtalk Acknowledge With >> <success xmlns="urn:ietf:params:xml:ns:xmpp-sasl"></success> JAXL Sends >> <!--?xml version="1.0"?--> <stream:stream xmlns:stream="http://etherx.jabber.org/streams" version="1.0" xmlns="jabber:client" to="gmail.com" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace"></stream:stream> Gtalk Acknowledge With...

PHP Extensions – How and Why?

Posted 39 CommentsPosted in PHP
In this short post we will quickly see: How to write PHP extensions? Why to write PHP extensions? However before you could understand what we are going to disucss, I will recommend you to read one of  my previous post How does PHP echo’s a “Hello World”? - Behind the scene . In this post I discussed in brief the backend architecture of PHP. Assuming you have read the previous post, lets discuss on how to build our first PHP extension: Every PHP extension is built out of minimum of 2 files. Configuration file (config.m4) which tells us what files...

How does PHP echo’s a “Hello World”? – Behind the scene

Posted 24 CommentsPosted in PHP
Have you ever wondered how PHP echo's a "Hello World" for you on the browser? Even I didn't until I read about the PHP internals and extensions. I thought may be a few out there will be interested in exploring the other side of PHP, so here we go. In my last post I discussed in brief "How your browser reaches to my server when you type http://abhinavsingh.com in address bar?". Read through if you have missed out on that. Here I will discuss in brief "How does PHP churns out the content requested on the webpage?" An Overview Here...