Giveaway Contest : PHP and MongoDB Web Development Beginner’s Guide

I’m happy to announce that Packt Publishing has arranged a giveaway contest for the book PHP & MongoDB Web Development: Beginner’s Guide. All you have to do is participate in a small programming challenge. The winners of the contest will be receive a copy of the book each, free of charge!

The challenge is to build a standalone, re-useable HTTP session manager using PHP and MongoDB.

The details of the contest are available here.

We hope that this contest will spark your interest in building web apps using PHP and MongoDB. Happy Coding!

PHP and MongoDB Web Development Beginner’s Guide – Thoughts of a first-time author

PHP and MongoDB web development

Social networking doesn’t always make you procrastinate, sometimes it pays off! When @packtauthors tweeted that they were looking for someone to author a book on PHP and MongoDB, I made contact. Few weeks later I signed a contract for writing the book. And six months after that, I am pleased to announce that PHP and MongoDB Web Development Beginner’s Guide is published and out for sale!

In this post I intend to share a few words about the motivation behind the book and the journey of a first time author.

The Motivation

I’m a supporter of the idea the MongoDB can potentially be the new M in LAMP. The web application data storage requirements have changed a lot during the past 4-5 years. Instead of producing contents of their own, the most popular websites are hosting contents created by their users. These contents are diverse in nature and humongous in volume. Mapping the diverse data into a rigid data structure gets harder as the volume grows. This is where the ‘Flexible Schema’ nature of MongoDB fits really well. Also MongoDB is easy to learn, developers with relational database experience should find little trouble adapting to it. There is a lot of similarity between the underlying concepts of an RDBMS and MongoDB (think documents for rows, and collections for tables). Developers don’t need to wrestle with radical ideas such as column-oriented or graph theory based data structures as some other NoSQL databases require them to. Finally, it is open-source, freely available (Creative Commons License), supports multiple platforms (Windows/Linux/OS X), have great documentation and a very co-operative community, and plays nicely with PHP! All these have lead me to believe that in near future MongoDB will be where MySQL is right now, the de facto database for web application development (I would urge you to read Stephen O’Grady’s article which makes more persuasive arguments). And since PHP is the dominating language for web programming, writing a book on web development with PHP and MongoDB felt just right.

The intended audience for this book are web developers who are completely new to MongoDB. It focuses on application development with PHP and MongoDB rather than focusing only on MongoDB. The first few chapters will try to ease the reader into understanding MongoDB by building a simple web application (a blog) and handling HTTP sessions with MongoDB as the data back-end. In the next chapters he will learn to solve ‘interesting’ problems, such as storing real-time web analytics, hosting and serving media content from GridFS, use geospatial indexing to build location-aware web apps. He will also brainstorm about scenarios where MongoDB and MySQL can be used together as a hybrid data back-end.

The Inspiration

Scott Adams, the creator of the famous Dilbert comic strip, wrote an inspirational article on Wall Street Journal. I’m going to quote a few lines here:

“I succeeded as a cartoonist with negligible art talent, some basic writing skills, an ordinary sense of humor and a bit of experience in the business world. The ‘Dilbert’ comic is a combination of all four skills. The world has plenty of better artists, smarter writers, funnier humorists and more experienced business people. The rare part is that each of those modest skills is collected in one person. That’s how value is created.”

These words moved me. I like programming and I like writing, and although there are smarter programmers and better writers out there, by combining these two passions I could potentially produce something. Besides I had an amazing learning experience with MongoDB. I built an API analytics solution with MySQL which became difficult to handle as the volume of the data grew. I started playing with MongoDB as a potential alternative. A month later I moved the entire data from MySQL to a more solid and scalable solution based on MongoDB. I wanted to share this learning experience through a series of blog posts but lacked the personal discipline and commitment to do so. Being obligated a deliver a book within tight deadlines solved that problem!

I also must thank Nurul Ferdous, my friend and former colleague who is a published tech author himself. His guidance and influence has been instrumental.

The Journey

My journey as an author writing a book for the first time has been an exhaustive yet amazing one! I work in a tech startup, which naturally requires longer than usual hours and harder than usual problems to solve. I would come home late and tired, research on MongoDB topics, plan how to deliver the message to the reader, write code, test and debug the code, write the content on a text editor, fight with Microsoft Word so the content has proper formatting as required by the publisher. Then on weekends I would revise and rewrite most of what I have done over the week and hustle to make the deadline. Nevertheless it all had been a rewarding experience.

In the rewrite phase I had a lot of help from the technical reviewers – Sam Millman, Sigert De Vries, Vidyasagar N V and Nurul Ferdous. They corrected my errors, showed me what more could be added to the content and what should be gotten rid off, helped me communicate complicated topics to readers in a clearer way. I convey my sincere appreciations to them!

Time to end this lengthy blog post. I hope you find this book enjoyable and use it to build some really cool PHP-MongoDB apps! I will then consider my endeavor to be a success.

Modifying PDF files with PHP

Last week, a friend of mine asked me to help him with a programming problem that he had been wrestling with for some time. The problem sounds simple:

  1. Take a PDF file
  2. Write something at the footer of each page of that file

And this had to be done with PHP.

Although there are several libraries available in PHP for dealing with PDF files, none seem to have capabilities to modify the contents of an existing PDF file. Their manuals/tutorials are full of examples on how to create PDF on the fly. After spending few fruitless hours trying to get the much recommended PDFLib installed in my Mac and have it work with MAMP, I painfully realized this library is for commercial use only. The free version leaves a horrible watermark of their site address on the generated PDF documents.

My search for a solution took me to FPDF, an open-source library for PDF file generation in PHP. In their FAQ section, I found the link to an extension of the library, named FPDI. This one was seemingly capable of ‘manipulating’ PDF files in an ad hoc fashion. It extracts the contents of each page in the file, uses it as a template, lets you put texts/shapes on the template and then outputs the modified file. Excited, I got into coding and after an hour of labor, finally succeeded to achieve my goal! Thank God for creating open source!

Enough talk, now lets get our hand dirty!

First we need to have following libraries downloaded and unzipped. They are just packages of PHP scripts that you just require/include in your own script. No need to deal with .dll/.so extensions.

  1. FPDF
  2. FPDI

Keep them in the same directory of your script, or in the include path. The following code snippet gives a basic idea of how to get started with it:


$pdf =& new FPDI();

//Set the source PDF file
$pagecount = $pdf->setSourceFile("my_existing_pdf.pdf");

//Import the first page of the file
$tpl = $pdf->importPage($i);
//Use this page as template

#Print Hello World at the bottom of the page

//Go to 1.5 cm from bottom
//Select Arial italic 8
//Print centered cell with a text in it
$pdf->Cell(0, 10, "Hello World", 0, 0, 'C');

$pdf->Output("my_modified_pdf.pdf", "F");

The above code takes a PDF file “my_existing_pdf.pdf”, and creates a copy of it “my_modified_pdf.pdf” with “Hello World” printed at the centre bottom of the first page.

That’s it! To achieve my goal, which I outlined at the start of this post, I extended the FPDI class, and overrode the Footer() method to print a customized footer in each page.

I only wish that the PHP online manual did NOT have an entire section dedicated to PDFLib, a non-free and commercial library, and rather point to free ones such FPDF or TCPDF. It could have saved me hours.

Working on Netbeans 6.5, and loving it


Netbeans has recently released version 6.5 of their powerful IDE. It comes with PHP support which can either be downloaded as a plug-in or installed as a stand-alone module. I’ve started working with it few days ago and am really impressed with it. Here are a a few things that I like about it:

User friendly

Creating new projects is really easy. They can be created either from scratch or from existing source files. The interface is clean and useful. It has file and project explorer, a navigation panel to quickly access the methods/members of your class files, a pallet to create HTML pages in drag-and-drop manner and lots more.

Code Assistance

It provides your basic PHP/JavaScript/HTML/CSS code auto-complete features like most other IDEs. Interestingly you can add your PHP framework/library files in the include path of your project and have the IDE suggest methods/members of the classes you have included in your script. It also supports code assistance for JQuery, Prototype and Scriptaculous.


It has a smaller footprint on system resource compared to Eclipse PDT and Aptana Editor, and even its predecessor Netbeans 6.1. It loads much faster than those on my Windows XP machine. I am yet to try it out on Ubuntu.

It free!

Yes. The last but not the least!

The thing that I don’t like about it, that it no longer separates the project specific files from the source files, as the 6.1 did. I work on Windows Desktop connected to a Linux server through Samba share. So I am left with ‘nbproject’ folders on the development server, which I have to manually remove when migrating the sources to production server.

Still Netbeans 6.5 is a decent 8 out of 10 on my book.

Download Link:

Here’s a couple of screencasts to get started with the IDE: