Now Playing

Archive for the ‘www’ Category

PHP caching and APC

October 9th, 2009 | development, php, www | No Comments

A friend asked me recently about the caching options available to him with PHP on a linux stack. While he had looked into memcached and seen the difference it can make, he had overlooked APC.

APC, or Alternative PHP Cache, not only allows for faster pages through opcode caching, but also since v3.0.0, allows for users to store any kind of data in memory for access between and across all requests. See my example below for a better idea on how to use it.

/* The first argument is the unique key which
 you will use to store some data later.
For globally cached data, the URL could make a good key,
and you could append the user id for user specific data.
The second argument is set to true or false depending
on whether APC finds data at the location,
if we don't find it, we know we need
to (re)generate it. */

$html = apc_fetch("unique_key_url", $success);
if(!$success)
{
	$html = big_and_slow_function();

	/*here the first argument is the key again,
	followed by the data to store and
	finally the time to live period in seconds */

	apc_add("unique_key_url", $html, 180);
}
echo $html;

Using a mechanism like this, you can see massive decreases in page load times. The friend I mentioned earlier brought his front page load time down from a number of seconds to low milliseconds.

PHP and stdClass

June 28th, 2009 | php, www | 1 Comment

The built in class stdClass can be instantiated manually for creating generic objects which you can then set member variables for, this is useful for passing objects to other functions or methods which expect to take an object as an argument. An even more likely usage is casting an array to an object which takes each value in the array and adds it as a member variable with the name based on the key in the array.

<?php
$obj = (object) array("foo" => "bar")
echo $obj->foo; // outputs "bar"
?>

One place you might get caught out here is trying to directly access numerically indexed values on the object, this breaks PHP’s naming convention and does not work correctly even with escaping.

<?php
$obj = (object) array("foo"=>"bar","foobar");
echo $obj->foo; //outputs "bar"
echo $obj->{0}; //$x->0 would cause a syntax error, this simply causes a notice for being unable to find the variable.

HTTP Auth with PHP

June 27th, 2009 | php, www | No Comments

HTTP Auth has a number of practical uses even in complex PHP backed systems. A prime example, and the reason I had looked into it, is web service and API authentication. In this case it is much simpler for the client to provide the details in this manner rather than perform an extra request to grab an authorisation token. A basic example lifted from the PHP manual is below.

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>

AIB and Three Webtext PHP5 APIs

June 15th, 2009 | php, www | 1 Comment

Over the last day I’ve put together two PHP5 API classes (loosely termed, it’s screen scraping at heart) for online services I use, AIB online banking and Three’s newly launched webtexts. I’m releasing both under the LGPL license and you can find them in my open source releases directory.

Update: I’ve ported the Three API to Perl and added it to the three releases directory

PHP Tips #1: foreach

February 6th, 2009 | www | No Comments

I love foreach loops for their simplicity and readability, but they’re a bit quirky when it comes to PHP. Here’s a couple of tips I’ve picked up.

The following are functionally the same:

<?php
$arr = array("one", "two", "three");
reset($arr);
while (list($key, $value) = each($arr)) {
    echo "Key: $key; Value: $value
\n";
}
foreach ($arr as $key => $value) {
    echo "Key: $key; Value: $value
\n";
}
?>

Ever want to drive straight into a foreach without checking that the array has elements?

<?php
$non_array = null;
foreach ((array) $non_array as $key => $value) {
    echo "Key: $key; Value: $value
\n";
}
?>

About

A Web Developer by nature and by trade. Living in Dublin, Ireland and working at Web Reservations International. Enjoys working on Open Source, web server stacks, XHTML/CSS, JavaScript and playing with the latest trends in technology.