Category — php
Must-have tools for php developers
The following are tools that are invaluable to any php developer:
1) PHP Development Tools Project (eclipse)
Link: http://www.eclipse.org/pdt/
The PDT project provides a PHP Development Tools framework for the Eclipse platform. This project encompasses all development components necessary to develop PHP and facilitate extensibility. It leverages the existing Web Tools Platform (WTP) and Dynamic Languages Toolkit (DLTK) in providing developers with PHP capabilities.
2) ez_sql/ez_results – mysql wrapper library (Free)
Location: http://www.jvmultimedia.com/portal/
A great mysql library that features:
-Disk caching
-It is one php file that you include at the top of your script. Then, instead of using standard php database functions listed in the php manual, you use a much smaller (and easier) set of ezSQL functions
-It has excellent debug functions making it lightning-fast to see what’s going on in your SQL code
-Most ezSQL functions can return results as Objects, Associative Arrays, or Numerical Arrays
-Works with Smarty templating language
3) Smarty Template Engine (Free)
Location: http://smarty.php.net/
An excellent template engine for php.
One of Smartys primary design goals is to facilitate the separation of application code from presentation. Typically, the application code contains the business logic of your application, written and maintained in PHP code. This code is maintained by programmers. The presentation is the way your content is presented to the end user, which is written and maintained in template files. The templates are maintained by template designers.
4) mysql gui bundle 5.0 (Free)
Link: http://dev.mysql.com/downloads/gui-tools/5.0.html
A suite of GUI tools that includes: MySQL Administrator, MySQL Query Browser, MySQL Migration Toolkit, and MySQL Workbench.
Here is a list of some more tools that can be very helpful for php developers.
Editors
Pnotepad: http://www.pnotepad.org/
Pspad: http://www.pspad.com/
notetab: http://www.notetab.com/
notepad++: http://notepad-plus.sourceforge.net/uk/site.htm
conTEXT: http://www.context.cx/
php Designer: http://www.mpsoftware.dk/
Dev-PHP: http://devphp.sourceforge.net/
notepad2: http://www.flos-freeware.ch/notepad2.html
crimson editor: http://www.crimsoneditor.com/
textpad: http://www.textpad.com/
ultraedit (commercial): http://www.ultraedit.com/
Mysql GUI clients
Navicat (commercial) : http://www.navicat.com/
phpmyadmin: http://sourceforge.net/projects/phpmyadmin/
sqlyog: http://code.google.com/p/sqlyog/
Libraries
Propel: http://propel.phpdb.org/trac/wiki/Users/Introduction
Html purifier (Great for preventing XSS attacks): http://htmlpurifier.org/
Templates/Frameworks
phptal: http://phptal.motion-twin.com/
cakePHP: http://www.cakephp.org/
prado: http://www.xisc.com/
codeIgniter: http://codeigniter.com/
savant: http://phpsavant.com/
bTemplate: http://www.massassi.com/bTemplate/
vLibTemplate: http://vlib.clausvb.de/
Blitz (a php extension): http://alexeyrybak.com/blitz/blitz_en.html
php template engine:
fryPHP: http://fry.sourceforge.net/
October 21, 2009 3 Comments
How to create a multi-file uploader for your website
If you own a website of any kind and have ever wanted to allow your users to upload multiple files from a single screen, you have a few choices:
- a java applet
- an active X control
- A Flash app
- multiple file input elements (which is messy and not very efficient)
The following is a much easier way to allow multiple file uploads. Using DOM (The Document object Model), one file upload box can be created making it much easier and more user-friendly.

Installation and usage
Installation is pretty easy. The download includes sample code that you can use on your website.
All files and example of usage can be downloaded Here
October 15, 2009 No Comments
How to cache smarty templates
What is caching?
Caching is used to speed up a call to display() or fetch() by saving its output to a file. If a cached version of the call is available, that is displayed instead of regenerating the output. Caching can speed things up tremendously, especially templates with longer computation times. Since the output of display() or fetch() is cached, one cache file could conceivably be made up of several template files, config files, etc.
Since templates are dynamic, it is important to be careful what you are caching and for how long. For instance, if you are displaying the front page of your website that does not change its content very often, it might work well to cache this page for an hour or more. On the other hand, if you are displaying a page with a weather map containing new information by the minute, it would not make sense to cache this page.
Setting it up
The first thing to do is enable caching by setting $caching = 1 (or 2).
Example: setting up caching
$smarty->caching = true;
$smarty->display('index.tpl');
?>
With caching enabled, the function call to display(’index.tpl’) will render the template as usual, but also saves a copy of its output to a file (a cached copy) in the $cache_dir. Upon the next call to display(’index.tpl’), the cached copy will be used instead of rendering the template again.
Technical Note: The files in the $cache_dir are named similar to the template name. Although they end in the “.php” extention, they are not really executable php scripts. Do not edit these files!
Each cached page has a limited lifetime determined by $cache_lifetime. The default value is 3600 seconds, or 1 hour. After that time expires, the cache is regenerated. It is possible to give individual caches their own expiration time by setting $caching = 2. See $cache_lifetime for more details.
example: setting up cache lifetime
// set the cache_lifetime for index.tpl to 5 minutes
$smarty->cache_lifetime = 300;
$smarty->display('index.tpl');
// set the cache_lifetime for home.tpl to 1 hour
$smarty->cache_lifetime = 3600;
$smarty->display('home.tpl');
// NOTE: the following $cache_lifetime setting will not work when $caching = 2.
// The cache lifetime for home.tpl has already been set
// to 1 hour, and will no longer respect the value of $cache_lifetime.
// The home.tpl cache will still expire after 1 hour.
$smarty->cache_lifetime = 30; // 30 seconds
$smarty->display('home.tpl');
?>
Caching Tips
Smarty caching can improve performance tremendously, but it doesn’t always work the way that you think it should. In the following situation (which I have seen recently):
example: within (main.tpl) (this is a file named main.tpl)
{include file="menu1.tpl"}
{elseif some other action}
{include file="menu2.tpl"}
{/if}
If the main template file (main.tpl) is cached, the includes template files will not get called properly. My solution is to separate the main file into cached and non-cached parts:
(within the PHP file that calls your smarty templates)
$smarty->caching = true;
//the fetch command puts the output of a template file into a variable rather than //the screen
$menu1 = $smarty->fetch('menu1.tpl');
$smarty->assign('menu1',$menu1);
$menu2 = $smarty->fetch('menu2.tpl');
$smarty->assign('menu2',$menu2);
//set the main caching file to false
$smarty->caching = false;
$smarty->display('main.tpl');
(within the new main.tpl)
{$menu1}
{elseif some other action}
{$menu2}
{/if}
July 14, 2009 2 Comments
Using smarty templates
Why use smarty templates?
One of Smartys primary design goals is to facilitate the separation of application code from presentation. Typically, the application code contains the business logic of your application, written and maintained in PHP code. This code is maintained by programmers. The presentation is the way your content is presented to the end user, which is written and maintained in template files. The templates are maintained by template designers.
At its most basic function, the application code collects content, assigns it to the template engine and displays it. The content might be something like the headline, tagline, author and body of a newspaper article. The application code has no concern how this content will be presented in the template. The template designer is responsible for the presentation. They edit the template files, adding markup and bringing it to completion. This typically involves things like HTML tags, cascading style sheets and other tools provided by the template engine.
This paradigm serves several purposes:
1) Designers can’t break application code. They can mess with the templates all they want, but the code stays intact. The code will be tighter, more secure and easier to maintain.
2) Errors in the templates are confined to the Smartys error handling routines, making them as simple and intuitive as possible for the designer.
3) With presentation on its own layer, designers can modify or completely redesign it from scratch, all without intervention from the programmer.
4) Programmers aren’t messing with templates. They can go about maintaining the application code, changing the way content is acquired, making new business rules, etc. without disturbing the presentation layer.
5) Templates are a close representation of what the final output will be, which is an intuitive approach. Designers don’t care how the content got to the template. If you have extraneous data in the template such as an SQL statement, this opens the risk of breaking application code by accidental deletion or alteration by the designer.
6) You are not opening your server to the execution of arbitrary PHP code. Smarty has many security features built in so designers won’t breach security, whether intentional or accidental. They can only do what they are confined to in the templates.
Although application code is separated from presentation, this does not necessarily mean that logic is separated. The application code obviously has logic, but the templates may have logic based on the condition that it is for presentation only. For example, if the designer wants to alternate table row colors or upper-case some assigned content, they can. This is presentation logic, something the programmer should not be concerned with. How often have you had some presentation displayed in a single column and then you wanted it in two or three columns, so the application code needs adjusting to accomodate this? A better approach is to assign the content in one single array and let the template handle the presentation. This will simplify your application and keep your templates flexible. Smarty supplies the tools to handle this kind of situation.
Getting Started with smarty
The first step is to download the smarty template engine Here
1. copy the following to a new file named “smartywrapper.php”
require_once('Smarty.class.php');
// extend the Smarty class
class smartywrapper extends Smarty {
//this will only work with php 5.X
function function __construct() {
// create the Smarty object
$this->Smarty();
// make sure these folders exist and the permissions are set accordingly
$this->template_dir = '/www/example.com/webapp/template/';
$this->compile_dir = '/www/example.com/webapp/compile/';
$this->config_dir = '/www/example.com/webapp/config/';
$this->cache_dir = '/www/example.com/webapp/cache/';
}
}
2. copy the following to a file named “smartytest.php”
require_once('smartywrapper.php');
// create the Smarty_WebApp object
$smarty = new smartywrapper();
// assign a variable, first parameter is the var name, second is the value
$smarty->assign('test_var_1','this is a test');
//display the template file (all html can now be placed here instead of in your php files)
$smarty->display('my_template.tpl');
create a file called my_template.tpl and place it in the directory in $this->template_dir = ‘/www/example.com/webapp/template/’; from above:
This is a test page from smarty, my variable is: {$test_var_1}
launch smartytest.php from your browser and if it is successful, you should see the following
Output
This is a test page from smarty, my variable is: this is a test
Conclusion
Smarty templates can not only be used to increase the overall speed of your php scripts through caching (more about caching can be found Here), but make it easier to develop large-scale applications through the separation of HTML code and php script.
July 9, 2009 2 Comments
Better ways to improve php application performance
Google recently released a document on how to improve PHP performance here. Most of the tips listed here will not help you improve performance by any significant amount. The following is a list of things that you can do that will improve the performance of your web apps significantly.
1) object code caching
Each time a request comes to your server for a php script, it has to go through the compiler and then execute the object code. If this is cached, the 1st step is skipped and you end up with a faster and more responsive script.
There are many object code caching packages available on the Internet:
A) Ioncube: http://www.ioncube.com/
B) Zend Encoder: http://www.zend.com/products/zend_safeguard
2) Template systems
Template systems provide a different type of caching. Content caching. Template systems work well in a situation where there is static data on one or many of your pages that doesn’t have to be reloaded. Caching systems also provide a separation of code and html, which will not only improve completion time of the overall project, but make it easier for future improvments. Most template systems for php are available for free:
A) Smarty Templates: http://smarty.php.net/
B) Pear Templates: http://pear.php.net/package/html_template_it/redirected
C) PHP savant: http://phpsavant.com/yawiki/
3) Distributed object caching systems
The most widely used system of this type is memcached (http://www.danga.com/memcached/).
This type of system makes your overall site faster by caching the majority of your database data into a large memory pool.
more on memcached:
“Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss.”
4) PHP variables that can be set
variables_order = ‘GPC’
register_argc_argv = ‘Off’
register_globals = ‘Off’ (this is a good idea to keep off for security purposes as well)
always_populate_raw_post_data = ‘Off’
magic_quotes_gpc = ‘Off’
Disable Error Logging. This is a good idea to keep on when you are developing your scripts, but it has been known to decrease overall performance.
Use IP addresses, rather than host names to access your database. Although this is sometimes not possible, you will get a slight boost in lookup speed if the IP address is used to access your database rather than its hostname.
5) Output Compression
Almost all browsers these days support something called gzip compression. Gzip compression can decrease the overall size of your output by up to 80%, but with a tradeoff: cpu usage will go up by around 10%. The benefit of using this compression type is the fact that not only will your bandwidth be decreased, but your pages will load faster.
enabling it in php (add the following lines to php.ini):
zlib.output_compression = On
zlib.output_compression_level = (level) (where level is 1-9. Youy may want to try different values to see what is best for your system).
if you are using apache, you can also enable the mod_gzip module. It is highly configurable, with the ability to modify output based on MIME types, files, or browser settings.
6) Other things that may help
when using a database, only retrieve the data that you are actually going to use. This may sound like a no-brainer, but I have often times worked on projects where the original programmer used (select * from mytable) when they could have used (select fieldIneed from mytable).
index database tables whenever possible
Learn more about this Here
June 25, 2009 13 Comments