Thursday, December 3, 2009

Debugging PHP apps

A major issue I faced in Ajax Development was with debugging the Ajax Request and the Response. A raw method I used for debugging was adding alerts, checking the return value and guessing what maybe going on with the Ajax request until I came across FireBug. Then I started being more wise and started dumping logs and info pertaining to Ajax Requests and Responses using JSON. While using FireBug (a mozilla add on) I stumbled upon FirePHP and that improved my life. So what is FirePHP all about?


FirePHP is an add-on for an add-on: it extends the popular in-browser web development tool called Firebug with an API for PHP web application developers. FirePHP is free and easily attainable via the Mozilla Add-Ons section on the official Mozilla site. The official FirePHP site can be found via this web address: www.firephp.org.


How to get started with FirePHP? 


To get started with FirePHP we need the following -
1) FireFox
2) FireBug
3) FirePHP
4) FirePHP Core (a PHP class to send the headers)


How to install the FirePHP Core? 
Checkout @ http://www.firephp.org/HQ/Learn.htm


Logging messages and information in Firebug Console. 


Once everything is setup you are ready to debug your PHP Apps. To learn and test the workings of FirePHP simply create a index.php in your root folder and add the following lines -




require_once('FirePHPCore/FirePHP.class.PHP');
$firephp = FirePHP::getInstance(true);
$firephp->fb("Starting ... "); 
$firephp->log("This is a Log Message");


$fruits = array("Apple", "Banana", "Mango", "Orange", "Pear");
$firephp->log($fruits, "Dumping Fruits Array");


$firephp->info("This is an informational message");
$firephp->error("This is an error message");
$firephp->fb("End!");
Run the above index.php and check the firebug console!!!





Move your cursor on the array Dump ... 


A WORD of caution
1) Do not forget to disable FirePHP on LIVE site as it may expose sensitive data of your website to anyone who has FirePHP & FireBug installed. To disable firePHP use - 
  $firephp->setEnabled(false);


Further reading: http://www.christophdorn.com/ 

Wednesday, October 14, 2009

Supercharge Website Performance With AWS S3 and CloudFront


We live in a world where people are increasingly expecting more and faster speeds. In fractions of a second, your website can lose valuable visitors and in turn, money. Although most people think CDNs are for the "big dogs", they're actually super cheap and incredibly easy to use these days.
This tutorial shows you how to setup and use Amazon's Web Services S3 and CloudFront to decrease website load time as well as show the performance differences.

Tuesday, October 13, 2009

Creating FTP User in Plesk

How to create additional FTP user in Plesk?


You simply CANNOT create additional FTP user in Plesk as of date. What a pity! The workaround is -
You need to have shell access so that you can create the user from shell. Assuming that we already have a domain (domain-name.com) with primary FTP user (harry) with home directory as /var/www/vhosts/domain-name.com and you now want to create user sally

1) Login to shell with root access and issue
# id harry
It will show something like
# uid=10001(harry) gid=2523(psacln) groups=2523(psacln)
We will need the uid of harry later - so remember it  !

2) create user via the command below and use harry's uid like following:
# sudo useradd -u 10001 -o -d /var/www/vhosts/domain-name.com/upload_folder -g psacln -s /bin/false sally
This will let sally access the sub-folder which in our example is upload_folder


3) Now create password for sally
#sudo passwd sally
This will ask you to input password for sally this can be any password - not necessarily the same as harry

You should now be able to use an FTP client to login with that user’s name and password. This is the only good solution for Plesk for now.

Monday, September 14, 2009

Overview of few PHP Frameworks to choose from

What is a framework and why to use a framework? 

A framework is "A structure supporting or containing something". Frameworks give us a structure to work within with lots of functionality build in. For ex. A database abstraction layer.

Frameworks help us avoid re-inventing the wheel again and again. It also enables rapidly develop an application. Some of the advantages of using a framework are : modularity, anticipation of change, abstraction, low coupling, and high cohesion (all the properties that styles aim at). 

List of Frameworks to choose from - 

1) CakePHP - A rapid development framework for PHP that provides an extensible architecture for developing, maintaining, and deploying applications

2) ZendFramework - Zend The PHP Company..! The framework from Zend itself which is open source. It is quite loosely coupled and framework don't force you to follow any coding standards. You can continue with your own convention and still use Zend.

3) Symphony - Almost a 2year old project. Quite rich documentation and user community

4) CodeIgniter - Easy to start with. It is easier for someone who is just starting with MVC and PHP. It's got good Documentation and quite comprehensive User Guide.

5) Kohana (Smaller footprint of CodeIgniter) - Extremely lightweight MVC framework

6) Prado - is a component-based and event-driven programming framework for developing Web applications in PHP 5. PRADO stands for PHP Rapid Application Development Object-oriented.

7) Agavi - Agavi is a powerful, scalable PHP5 application framework that follows the MVC paradigm. It enables developers to write clean, maintainable and extensible code. Agavi puts choice and freedom over limiting conventions, and focuses on sustained quality rather than short-sighted decisions.

8) Rapyd - a minimalistic and rapid PHP framework. 

9) ELF PHP - Extra Light PHP Framework 

10) BaseApp - Simple, Small & Swift

11) SuitFramework - SUIT (Scripting Using Integrated Templates) is an open-source templating framework that allows you to define your own syntax through nodes. It is available in both PHP and Python. It is extremely lightweight, yet powerful.

12) Madeam - Madeam is a PHP framework for small agile teams and freelancers.

A good site to checkout before choosing a framework - http://www.phpframeworks.com/ 

Lastly, a good article on No-Framework PHP MVC Framework

Tuesday, September 8, 2009

Username as subdomain on windows

While working on http://designerscouch.org worked on allowing users to have usernames as subdomains.


All went well on the server. But when I was trying to run the codebase on my local machine which had Apache2 running on windows I was not able to browse the site. After some research on the net and trials and errors succeeded in running the codebase on loacl machine.

Steps to set up named subdomains using Apache on Windows platform.

1) Open up your httpd.conf file in a text editor (wordpad or notepad) and add the following line at the bottom of the document.

Include conf/hosts.conf

The file, from the default installation can be found in the X:\Program Files\Apache Group\Apache2\conf folder.

Save the file.

2) Now create the hosts.conf file. Using your text editor create a new document and copy the virtual host example that is commented out in httpd.conf.

#NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#

Uncomment the text, remove the #’s and change to fit your need. In the example below there are settings on a made up domain name, localhost.com.

<VirtualHost *:80>
ServerAdmin admin@localhost.com
DocumentRoot "X:/Program Files/Apache Group/Apache2/htdocs"
ServerName *.localhost.com
ServerAlias *.localhost.com
</VirtualHost>
The above configuration allows you to put “anything” before the domain provided that the host exists in your HOSTS file.
Save the file as hosts.conf in X:\Program Files\Apache Group\Apache2\conf.

3) Now we modify the hosts file on windows OS to inform which hosts exists -

The HOSTS file is usually located in %SystemRoot%\system32\drivers\etc folder.
Open the file with a text editor and then add the following to match your virtual host settings above.

# PC (LOCAL)
127.0.0.1 localhost.com
127.0.0.1 www.localhost.com
127.0.0.1 suchetal.localhost.com
127.0.0.1 username2.localhost.com

Save the file and restart Apache. You should then be able to navigate to any of the URLs listed above.
Ex: http://suchetal.localhost.com

Friday, March 27, 2009

Configuring SVN server on Windows as a service.



Double click the installer and run through it. You can change the path to something shorter like say C:\svn. Once installed add an environment variable for SVN_EDITOR. You can do this by right-clicking on MyComputer -> Properties. Go to advanced. Hit the Environmental Variables button. Just add another in there called SVN_EDITOR with a value of C:\windows\notepad.exe (or path of your favorite editor)




2. Create the Repository.


Goto the Command prompt (start -> run -> cmd) and change the path to where your SVN is installed. In my example it will be c:\svn>. Now fire the svnadmin create command to create the repository.
c:\svn > svnadmin create "c:\svn\mysvnrepos" (Change the path to whatever path you want to use.)
The repository is basically a folder that contains all your SVN files and their various config options.





3. Modifying security and authentication settings



For this, you need to modify the permissions and users. Navigate to the folder you just created. In my example, I used "c:\svn\mysvnrepos". In this directory, you will find a few folders. One folder will be called conf. Navigate to it and double-click on the svnserv.conf file. The svnserv.conf file is the configuration file for the svnserv executable. For our purposes, just uncomment out the following lines:
[general]
anon-access = read
auth-access = write
password-db = passwd
That is just remove the pound character (#) from the line.
Save your changes. And double-click on the passwd file in the same directory. This file stores all usernames and passwords in the format:
username= password
Make sure the [Users] line is uncommented and you can just add a new line for your username and password. For example, if you wanted your username to be john and your password to be doe, just create a line like this:


john= doe


Save it and you're done configuring.

Online Reference for Subversion : http://svnbook.red-bean.com/