Thursday, November 6, 2008

Using session in PHP

Keeping in mind the fact that Internet is a stateless platform and every request for a web page is treated as unique, there is a serious need of a tool to maintain the state. Otherwise, it will be a messy situation to keep track of requests made by a particular user. The good news is that use of PHP session serves the solution to this problem. This session variable is of great importance for web applications like shopping carts and is considered as equivalent to cookies, other significant way of maintaining the state.

What Is PHP Session Capable Of

PHP session is capable of storing the information in the form of session variables, in order to handle the ever-increasing traffic on a website. For instance, consider a shopping website, with products scattered in different categories and different pages. In such a situation, a user may switch from one page t another and keep on adding products from different pages to his or her shopping cart.

Memcache

Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

The Memcache module also provides a session handler (memcache).

More information about memcached can be found at » http://www.danga.com/memcached/.

Below is one example


$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."
\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)
\n";

$get_result = $memcache->get('key');
echo "Data from the cache:
\n";

var_dump($get_result);

?>

Friday, May 30, 2008

PHP Design Patterns

Patterns are ways to describe best practices and good designs. They show a flexible solution to common programming problems.

* Factory Patterns: The Factory pattern allows for the instantiation of objects at runtime. It is called a Factory Pattern since it is responsible for "manufacturing" an object. A Parameterized Factory receives the name of the class to instantiate as argument.

* Singleton Patterns: The Singleton pattern applies to situations in which there needs to be a single instance of a class. The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects.
i got this good stuff from a newly coming website on PHP, http://www.e-php.info

Saturday, May 24, 2008

metaphone — Calculate the metaphone key of a string

Description
string metaphone ( string $str [, int $phones ] )

Calculates the metaphone key of str .

Similar to soundex() metaphone creates the same key for similar sounding words. It's more accurate than soundex() as it knows the basic rules of English pronunciation. The metaphone generated keys are of variable length.

Metaphone was developed by Lawrence Philips . It is described in ["Practical Algorithms for Programmers", Binstock & Rex, Addison Wesley, 1995].

quotemeta — Quote meta characters

Description

string quotemeta ( string $str )

Returns a version of str with a backslash character (\) before every character that is among these:

. \ + * ? [ ^ ] ( $ )


Returns the string with meta characters quoted.
This function is
binary-safe.

parse_str — Parses the string into variables

str

The input string.

arr

If the second parameter arr is present, variables are stored in this variable as array elements instead.



void
parse_str ( string $str [, array &$arr ] )

No value is returned.
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo
$first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

parse_str($str, $output);
echo
$output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

?>

sha1 — Calculate the sha1 hash of a string

string sha1 ( string $str [, bool $raw_output ] )
Returns the sha1 hash as a string.
$str = 'apple';

if (
sha1($str) === 'd0be2dc421be4fcd0172e5afceea3970e2f3d940') {
echo
"Would you like a green or red apple?";
exit;
}
?>