Monday, October 05, 2009

Sitepoint's twitaway experience for choosing right MySQL engine

Hi All,

Posting after a two month break sorry busy with new addition to our family my daughter(Manha). Here we go:

There is an interesting article about the experience Sitepoint developers had with twitaway application while distributing Free PDF who followed them. How they found the importance of choosing right MYSQL engine is how important :) please go through the full article for more details.

Wednesday, July 22, 2009

INSERT ON DUPLICATE KEY UPDATE and REPLACE INTO

While reading and article came across one of powerful feature REPLACE INTO,I had used INSERT ON DUPLICATE KEY UPDATE but never used REPLACE INTO.

REPLACE works in a way that if the ID column(Primary Column) doesn't have the given value it will create a new entry in the table, however, IF the value exists already then it updates the existing entry in the table.

REPLACE INTO sessions VALUES ('$id', '$data', $ts).

Read about REPLACE INTO and Difference about INSERT ON DUPLICATE KEY UPDATE vs REPLACE INTO

Sunday, July 19, 2009

PHP: exceptions vs errors?

Was just reading twitts got the nice link about the exceptions handling the post basically explains about handling errors in PHP nice one check out the code how the fatal error are handled and customize message is displayed for user. I have not tested the code but looks ok :) . Click for post here

Wednesday, July 15, 2009

Debug message in PHP?

I am using PHP from past 4 year and used to debug my code using die and echo statement and will log the debug message in file if running backend job(Cron) while using PHP 4 and then we started using Zend Framework and use Zend_log. But for those who won't use Zend Framework here is the function which will be useful in debug debug_backtrace this function is available from PHP4 didn't know i just came to know while i was browsing php.net, This information is for user like me who didn't know about it :)

Monday, July 06, 2009

Install PHP 5.3 on WAMP Server

Just installed PHP 5.3.0 on WAMP, Nice addon facility in WAMP now can switch between 5.2.x and 5.3.0. It was so simple that even a person with little knowledge can do it.

First install WAMP.
Then download version of php you want and install from here.
Done.

Now go to the wamp server icon in tray and switch to the version you want :)

PHP Security

Good article written by Joel Reyes about PHP Security, Author has explained some of the security holes we have to take care also given link for security tool download. Go through the article worth reading.

Thursday, June 11, 2009

Keywords in the URL

Reading the post 8 Tips to Get Domain Diversity came across important tips about keywords in the url. As most of us uses tinyurl or bit.ly for making our long url to short url we forget the feature tinyurl and bit.ly provide "Optional custom name/alias" this is important for SEO.

E.g., for the page http://ansarahmed.blogspot.com/2009/06/fingerprinting-to-dynamically-enable.html, you should probably try to use bit.ly/dynamic-caching

Tuesday, June 09, 2009

Fingerprinting to dynamically enable caching/ Caching dynamic content

While reading the caching tutorial from google came across with the section about using fingerprinting concept for caching content which changes regularly. Below is the explanation from google team.

"For resources that change occasionally, you can have the browser cache the resource until it changes on the server, at which point the server tells the browser that a new version is available. You accomplish this by embedding a fingerprint of the resource in its URL (i.e. the file path). When the resource changes, so does its fingerprint, and in turn, so does its URL. As soon as the URL changes, the browser is forced to re-fetch the resource. Fingerprinting allows you to set expiry dates long into the future even for resources that change more frequently than that. Of course, this technique requires that all of the pages that reference the resource know about the fingerprinted URL, which may or may not be feasible, depending on how your pages are coded."

I am writing the code in php to implement this concept and will share with you all soon.

Friday, June 05, 2009

Coding Standard

While going through the post from Lukas Kahwe Smith on coding standard came across the link for coding standard of various other project and framework.

Horde Project
PEAR
Zend Framework
PHP Team

Tuesday, June 02, 2009

include_path in php

I was just going through the post of Sam Hennessy about Zend Framework Location, I found a section which really surprised me about the include_path(performance). I know that what include_path is and how to modify it, i remember we used .htaccess way to change the include path in my previous organization, but didn't know about the performance effect if we didn't use it correctly, ok my old pal reading this post will surely correct it :)

What’s it look like?

A typical value of Linux include_path setting looks like:

.:/usr/share/php:/usr/share/pear

It breaks down into two parts, the path and the path separator.
The path separator is “:” on UNIX style systems and “;” on Windows and acts as a delimiter between multiple paths. You can also simple use the PHP constant PATH_SEPARATOR from within your PHP scripts.

You paths should be absolute and you should be careful not to add a trailing slash to the end. In the context of an include_path, a path of “.” is considered the current directory.

How do I change it?

The three common ways to add a new path to your include_path are, changing the php.ini, adding a value in an .htaccess file or from within a PHP script.
php.ini

Open your php.ini in a text editor and find “include_path” then add your Zend Framework path.

Before:

include_path=".:/usr/share/php:/usr/share/pear"

After:

include_path=".:/usr/share/php:/usr/share/pear:/path/to/zf"
.htaccess

In an .htaccess file, add:

php_value include_path ".:/usr/share/php:/usr/share/pear:/path/to/zf"
In a PHP File

Before you include any Zend Framework code add the following to your PHP code:

$path = '/path/to/zf'; set_include_path(get_include_path() .PATH_SEPARATOR. $path);

Performance

The earlier on in the include_path a path
you are intending to use occurs, the faster the lookup will be. So for performance reasons you should put the paths most used by your application at the beginning of the include_path

Let’s say Zend Framework is your applications most commonly used external library. We should then change our above example for php.ini file, from:

include_path=".:/usr/share/php:/usr/share/pear:/path/to/zf"

To:

include_path=".:/path/to/zf:/usr/share/php:/usr/share/pear"

This will reduce number of folders PHP has to check, and thus file system calls, by half.

I was under the impression that the performance gain from this optimization would only be minor if using an Op-code cache like APC or Zend Optimizer+. However Matthew O’Phinney informs me:

In the profiling and benchmarking I’ve done, it still makes a difference, as the opcode cache will first hit the realpath cache, which is when the path lookup will usually occur; once it determines the path, it then checks to see if it has opcodes for that path, and then merrily goes on its way. The sooner it finds a match, the faster it can identify and use the opcodes. That said, the performance difference is minor — you only notice it when you have many class files on any given request, and if you’re under heavy load. (And those were the conditions I was profiling.)

Saturday, May 30, 2009

Built in Breadcrumb in Zend Framework

Wenbert Del Rosario has posted a tutorial explaining how to use build in breadcrumb(Zend_navigation) facility in zend framework.

Friday, May 29, 2009

Managing Secure Protocol in Apache-Based Websites using PHP

In this post author explain of https protocol, how to effectively manage it, he is helping you to have good rank from google. Nice, see about use of rel="canonical" tag. He helps how to avoid your content being duplicated by googlebot as it will index your both http and https version of your website which will cause you to loss the page rank more details.

Strategy pattern in PHP

Very nice post explaning strategy pattern, i don't have much idea about it, But reading the post i liked it. Also see the way he has used interface in php.

Wednesday, May 27, 2009

Slides from international php conference(IPC) 2009

Will update the list as i get more.

IPC09 - Why Architecture in Web Development matters by Lars Jankowfsky

Evil WebDAV, XML and XPath at IPC SE 2009 by Tobias Schlitt

PHP Compiler Internals

Sebastian Bergmann explaining about php internal in the slides he goes in depth how the php code is compiled by zend engine and other step, if wanted to know how internally php code is worked on take a look at his slides

Monday, May 25, 2009

Slides for php|tek talk 09

Below are few slides i got will update if found more:

Ben Ramsey as he takes you on a journey through RFC 2616 to discover some of the gems of HTTP

Linux-Fu for PHP Developers by Lorna Mitchell

“No Really, It’s All About You” by Chris Cornutt


Getting IT Done by Wez Furlong

Video Interviews at php|tek 2009

Friday, May 15, 2009

Build a Facebook application with Zend Framework

Thanks Madarco, Nice post i faced lot of problem while developing facebook application earlier in my previous company. This tutorial is really nice for new bies who wants to develop facebook application.

Thursday, May 14, 2009

Web Evolution


Picked this slide from Michael Sutton(VP, Security Research, Zscaler) presentation. Very nice explaining briefly about web evolution Web 1.0 to Web 3.0.

URL Rewrite Module in IIS

When i was giving TecHTalk in my previous company on .htaccess,SEO were i explaned about url rewriting(Short URL/Search Engine Friendly URL), one of miscosoft team member asked me is there a apache like mod_rewrite module for IIS. Today while reading blog in reader came across this and posting this for all IIS lovers here is a IIS URL rewrite module.

Wednesday, May 13, 2009

How To Debug Web Applications With Firefox

I used firefox plugin like web developer, live http header earlier now i love httpfox and colorzilla for debugging and used it lot of time, all this are my favorite can't leave without it. I found this interesting post which will explain you the short cut and example with more details how to debug the web application with firefox.

Tuesday, May 12, 2009

MySQL PROCEDURE ANALYSE

It examines the result from a query and returns an analysis of the results that suggests optimal data types for each column. To obtain this analysis, append PROCEDURE ANALYSE to the end of a SELECT statement.

For Example:
SELECT col1, col2 FROM table1 PROCEDURE ANALYSE();
SELECT * FROM table1 PROCEDURE ANALYSE();

Quite common task during schema review is to find the optimal data type for the column value - for example column is defined as INT but is it really needed or may be SMALLINT or even TINYINT will do instead. Read the post in mysqlperformanceblog explained in more detail.

PROCEDURE ANALYSE() can be a double edged tool. In the first it will help you to find the optimal data type for column. In the second case it had no idea you needed bigger values and gave you a recommendation based on bad data. Like most tools, PROCEDURE ANALYSE() needs to be used properly to get the desired results.

Syntax and more info here.

Search & replace in files using php

Very nice post from Sameer, in his blog code-diesel explaining how to search and replace in file using PEAR library File_SearchReplace.
Below are link explaining how to install PEAR.

Install PEAR on Windows

Install PEAR on LINUX

Read more about File_SearchReplace with example here

Friday, May 08, 2009

PHP 5.3 upgrading notes

Hi all upgrading to php 5.3 just go through the php 5.3 upgrading notes on wiki.php.net, lots of changes done, lots of new classes,method also function added and extension added and removed. List of upgrading notes is huge go through it. Below are few quick notes:

Reserved word: GOTO,NAMESPACE,CLOSURE are now reserved word in PHP, Make correction to your programs or else php will trow Fatal Error while parsing the script.

Functions affecting backwards compatibility:
* var_dump() output with objects.
* session_start() now returns false if session startup fails for some reason.
* opendir(), scandir() and dir() now use the default context if no context passed.
* The new mysqlnd library necessitates using MySQL's newer 41 byte password format. Continued use of the old 16 byte passwords will cause mysql_connect() to produce the following error message: “mysqlnd cannot connect to MySQL 4.1+ using old authentication.” No need to worry if you are using MySQL version >= 4.1, Prior to MySQL 4.1, password hashes computed by the PASSWORD() function are 16 bytes long.

Bye Bye windows 98 or NT4: The minimum Windows versions are now windows 2000/XP.

Libraries added: mysqlnd (optional replacement for libmysql)

Extension Changes Causing Incompatibilities:
"Session" - Sessions would no longer succeed to store session-files in ”/tmp” path if open_basedir restriction is enabled, and ”/tmp” is not explicitly added to allowed paths list (special treatment of ”/tmp” was added in 5.2.2, but was not documented)

Deprecated:
* define_syslog_variables() now issues E_DEPRECATED
* all ereg functions now issues E_DEPRECATED (note that not all of them are prefixed with ereg)

New functions:

Array

* array_replace()
* array_replace_recursive()

Date

* date_add()
* date_sub()
* date_diff()
* date_parse_from_format()
* date_create_from_format()
* date_get_last_errors()
* timezone_version_get()

Filesystem

* parse_ini_string()

MySQLi

* mysqli_fetch_all()
* mysqli_get_connection_stats()
* mysqli_poll()
* mysqli_reap_async_query()

Tuesday, May 05, 2009

Submitting Sitemaps using robots.txt

What are Sitemaps?

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.

You can tell Google and other search engines about your Sitemap by using robot.txt

Read file in php

Really nice post with example explaining how we can read large size file using php.

Retrieving original url from short url using PHP and CURL

Good post explaining how to get back original url from short url.

expanding short url to original url using PHP and CURL

Monday, May 04, 2009

Luhn algorithm for validating credit cards

The Luhn algorithm also known as the “modulus 10″ or “mod 10″ algorithm, is a checksum formula which can be used to validate credit card numbers. Developed in the 1950’s by IBM scientist Hans Peter Luhn and described in U.S. Patent 2,950,048. A PHP implementation is shown below.


function LuhnCheck($strDigits)
{
$sum = 0;
$alt = false;
for($i = strlen($strDigits) - 1; $i >= 0; $i--)
{
if($alt)
{
$temp = $strDigits[$i];
$temp *= 2;
$strDigits[$i] = ($temp > 9) ? $temp = $temp - 9 : $temp;
}
$sum += $strDigits[$i];
$alt = !$alt;
}
return $sum % 10 == 0;
}

?>

Web scraping tutorial

Web scraping (or Web harvesting, Web data extraction) is a computer software technique of extracting information from websites.

I got a freelance work to extract all the hotel information in UK of some city from yellow pages, I wrote a simple php script which uses curl to get the data and parse it using regular expression and extract the require data and populate db, sorry was not aware probably ignored policies.

Now there is a PHP library that facilitates the process of creating web scrapers called Simplehtmldom. More information can be found here.

Checking coding standards with PHP_Codesniffer

PHP_CodeSniffer is a PHP5 script that tokenises and "sniffs" PHP and JavaScript code to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.

I was really behind this in my previous company, i want to make sure everyone follows the coding standard, with this tool i am sure that problem is solved.

You can also define your own coding standard, it can be integrated with Subversion.

More detailed information with installation and example can be found here.

Load testing

http_load is a simple load testing tool.

http_load is a useful HTTP benchmarking utility that lets you run multiple http fetches in parallel to test the throughput of your web server. It gives you a rough idea of how many bytes a server can serve in a predetermined time.

Detailed information with example and download link can be found here.

Note: I didn't test it.

Tuesday, April 28, 2009

PHP 5.3 features

I was just going through the interview of Lukas Kahwe Smith(Release manager php.net), He was explaining the key features we need to look out in PHP 5.3. In all the feature i am keen to use the feature lambda functions and closures and also PHAR.
About lambda function and closure i have already posted.
In this post lets look at PHAR, In the interview Lukas didn't explain in depth about it just mentioned PHAR is the result of a "proof of concept" PEAR package called "PHP_Archive".

I found an very good article on net about PHAR with an example.

Monday, April 27, 2009

Where is the include coming from?

This is the post if you are interested to know how the flow of code is which file is included from were, here is a post with example of few of framework. I like it interesting post :)

Here a link.

Thursday, April 23, 2009

How to improve PHP session security

Nice article about session security, I never tried any of session hacking technique but its true as session id is stored in cookies and cookies are stored in user side and also it is passed to server directly anyone in between can sniff or hijack it :)

Go through the article how to protect it.

Tuesday, April 21, 2009

Embedding PHP In CSS

Very interesting tutorial just check it out Embedding PHP In CSS

Useful PHP links

This list will be growing, please comment or send me a mail to add more link :)

i) Useful PHP Classes and Components

ii) Open-source PHP applications

iii) How to Install PHP on Windows

iv) A-Z PHP

v) Useful PHP + jQuery Components & Tuts for Everyday Project

vi) The ABC's of PHP

vii) Top 5 php template engines

viii) 10 Advanced PHP Tips To Improve Your Programming

ix) 6 books to master PHP

Auto Increment in Sybase

I had to use auto increment column in Sybase, I found we can use column as identity for it. Then I had a problem as I wanted to know last inserted value or id but I didn’t get answer today I was browsing to find how we can do so I found we can do it using Sybase global variable @@identity.

Example:

An easy way to insert a row into salesdetail after inserting a row into sales is to use the @@identity global variable to insert the IDENTITY column value into salesdetail. The @@identity global variable stores the most recently generated IDENTITY column value. For example:

begin tran
insert sales values ("6380", "04/25/97")
insert salesdetail values ("6380", @@identity, "TC3218", 50, 50)
commit tran
This example is in a transaction because both inserts depend on each other to succeed. For example, if the sales insert fails, the value of @@identity is different, resulting in an erroneous row being inserted into salesdetail. Because the two inserts are in a transaction, if one fails, the entire transaction is rejected

PHP 5.3, Lambda Functions, and Closures

PHP 5.3 will have a lot of exciting new features. One of the most important one for me is the introduction of lambda functions and closures support.

For more information click