Simple PHP HTTP Requests Echo Server

Sometimes you just need to see what you are sending as part of your HTTP request, to make sure the wrappers and the libraries you are using are not malfunctioning.
A simple echo server is very useful for this.

Here is a very simple one, which works with Apache Webserver and it prints out the raw Body of request.

Hope it will help someone.

$headers = apache_request_headers();
echo "-- Headers --<br/>\n";
foreach ($headers as $header => $value)
    echo "$header: $value <br />\n";
// Printing out the Body of the Request
echo "-- Body -- <br/>\n";
$raw_input = file_get_contents('php://input');

Lazy PHP

I would say I love this site. It’s a very simple to use and handy but on the other hand, knowledgeable and informative website containing tons of interesting articles, tutorials and forum discussions.

When I was about to start learning PHP and MySQL I was introduced to an article by the name of “Build you own Database Driven Website Using PHP and MySQL”. Now this is the closes thing I can find to that article, which is sample PDF of a book. I believe even this sample is enough to get you started.

All I wanted to say is this site has it all, various resources for all levels.

Today I came across another interesting article about PHP, how it works and how to make its performance better. The article is quite old but still applicable and useful.

It is very useful to know behind the scenes of the technology you’re using. The low-level knowledge of “How things work” will give you better understanding of how to improve you application and how to increase the performance. And the more you know the tools provided, the better and faster you can develop.

The way PHP scripts are being executed and interpreted, gives you an idea of how you can improve the performance. In this article it speaks about the 4 stage execution of each request by WebServer/PHP :

  1. Incoming Request : When WebServer receives the Request for the script
  2. Prepare OPCODEs : When the PHP script is being parsed and OPCODES are being generated.
  3. Execution of script and responding back to user
  4. Trash the lot including all the OPCODEs, variables and etc

We all know that now a days there are several different tools to improve the stages 2 and 4 which are the most resource consuming and duplicated tasks of the PHP Interpreter. PHP Accelerators Like APC , eAccelerator,XCache and so on help a lot to eliminate the phases #2 and #4.

Most PHP accelerators work by caching the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request[1].

Also in this great series of articles “Anonymous functions” known as “Closures” and “Variable Functions” are being discussed and there are some simple but very helpful samples of how they work and how they can help.

Also techniques like ‘Lazy includes’ which seems to be pretty obvious but many of us forget it, have been discussed.

I will let you read the articles by yourself ; Part-1 , Part-2, Part-3 and enjoy it 🙂

[1] :

1. Incoming Request:Incoming Request

CodeIgniter’s log system issue

First of all I have to admit that I love this framework. I will write more about this fantastic framework later.

But one issue I faced this morning and I was pulling my hair was its logging system.

I was editing the module which has few Ajax calls, some part was not working properly and it was absolutely because of the SQL. We have the main SQL commands being outputed in the CI log system as well. So I SSHed to server and open the log file in vi , searched for the desired point and was trying to figure out what’s the SQL problem.

At this point I got a message from a colleague asking whether our server is down or what ?

That was so confusing, some parts of the application were working and some parts were not. All the sections which were working like a Swiss watch few seconds ago. So what was wrong!
As usual everything was working properly on localhost.
I didn’t even give it a small chance that having the log file open for reading [I’m stressing this out : JUST FOR READING] could be the problem. CI couldn’t proceed with logging in the sections where we had used it. I just quit the vi editor and Voila. Coming back to life.

This is not a proper approach for log system, if it can not write to the log file it shouldn’t prevent the whole system from working. Or may be it should ! Depends on how logging is important for your system.

WAMP server: right-click and select your version

Have you ever been in a situation that you need to install PHP5 to have one application which is spcifically written in PHP5 and you have also some legacy applications which are written in PHP4 and are not compatible with PHP5 but you want to have them both.

I used to hate all those software bundles installing Apache/MySQL/PHP for you easily. I prefered to have them separately installed by myself to have more control over it. But considering WAMP server with an amazing feature of having many versions of Apache/MySQL/PHP and just switch from one to another by right-clicking and selecting it, is awesome.

I’ll definitely consider it for my local machine. [First need to be done with my exam on Wednesday]

MySQL – group results by second/minute/hour

This is a simple MySQL tip, but it helped me a lot.

We have a traffic table which monitors every single coming traffic to our applciation. I wanted to get the real values for how many requests I we have per Second / Minute / Hour and also what times of the the day are high traffic and what times are low traffic, to do some load-balancing on the application and server.

The query is very simple, just COUNT the number and do a GROUP BY and extract the minute/hour/…

Like :

SELECT tr_date_time,count(*) AS NUM FROM `traffic`
    WHERE DATE(`tr_date_time`) = '2009-04-25'

The script above extracts the number of hits per minute during the day.
Don’t forget that if you want to have the result by minutes, you have to add the hour as well to make it unique.

p.s: I hope your Date/Time/DateTime field is human friendly not Unix_timestamp, which will add some more extra calcuations on the result set.

CodeIgniter xss_clean semicolon addings

When you use that perfect xss_clean function from CodeIgniter’s library to make your content more robust, sometimes you may face a strange content being updated in DB having “;” after “&” or before “=” .

That “;” is added by xss_clean, to make the link more robust, but if in your case it will add some malfunctioning in your system you’d better use something else in your validations like “htmlentities” :

$rules['link']                =     "required|trim|htmlentities";

For more information regarding that you can refer to CI forums. [thread1] [thread2]

IDEs for PHP development

It’s been a long time that I used different IDEs for PHP development. I started with DreamWeaver 4.0 and grew up with the next versions.
Still I believe that none of the IDEs is as handy and easy to use as DreamWeaver, but unfortunately it didn’t update itself with developers’ needs. Integration with code repositories and code navigation are the basic needs for today’s development.

Well it has some features to support SVN in the CS4 version, but I prefer not to mention it because it has lots of flaws. And code navigation and class/method listing is not there, there are some extensions to give you the same concept but they’re not that much useful.

Once in a while I tried the other IDEs. I tried ActiveState Komodo long time ago but it didn’t work out for me at all, lots of problems with searching and searching through the folders and serious problems with unicode. They might have debugged all those things but that experience was enough for me.

NotePad++ is a great editor but you can not mention it as an IDE.

When PDT came out I was excited about it but the more I worked with that the more I disliked it. Very slow, lots of bugs and suddenly freezes and lack of so many features.

I also tried Aptana Studio for a while, but the same thing as PDT,, pretty slow and suddenly freezes. Which I eventually understood that it’s because of Eclipse.
Oh God! Eclipse ! It has a wonderful plugin-based software architecture, amazing integration and lots of plugins, but it just have 2 flaws :

  1. VERY slow
  2. Suddenly freezes without any reason

I don’t know why they love it so much at schools.

I moved to PHPed and I still use it for a some projects. One of the most important things that I really like from PHPed is the DB client, which you can Drag/Drop DB fields instantly from the IDE without considering any typo or need to go to another application to copy/paste them. The other thing that I really like is Shell-Menu access, it means you can have the menu which you have my right-clicking on the item in windows-explorer, so it doesn’t matter if it doesn’t support SVN or not [which it does] you can easily do whatever you want by accessing the shell-menu.
But on the other hand it has it’s own strangnesses, the most important of all shortcut keys are totally different [ctrl+Y does a line delete instead of Redo and so on…]
I still use it for some of my old projects though.

I’m not a Java fan at all, and I only use it when I’m completely obligated to use it at school [still wondering why do they love Java that much! ] . But for a school project I had to download Java and install it so I can study, and I was really shocked when I found that Eclipse has not been mentioned anywhere in Sun MiscroSystem‘s website at all. But they suggest NetBeans for Java Development, I downloaded the package containing Netbeans and JDK, I studied but you know, when you want to study you do everything else rather than studying 😛 it was exactly the case for me and I ended of finding an official plugin developed by NetBeans team for PHP development.

I can say that’s IT… That’s what a developer exactly needs for PHP development, it’s fast and smart. That is really it… Free and supports all the features you need. Give it a try and you’ll definitely like it.

As a MacroMedia/Adobe fan I’m still looking forward for them to provide a great IDE for PHP developemnt, but they should hurry.  They’re loosing so many of their users for the lack of the features I mentioned above.

While we’re looking forward for Adobe, let’s take a look at NetBeans and enjoy it.