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.
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.
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>";
}
?>
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
August 6th, 2008 | development, php, www | No Comments
It became clear early on in the planning stages of our current project that we were going to need a reliable backup solution. We discussed backing up to another server we already have in the datacenter and backing up to a server in another datacenter. The former was not redundant enough for our liking and the latter would introduce extra costs we could do without so early on.
Enter Amazon S3. You pay for as little as you need and it expands as you do. The S3 docs point to undesigned’s PHP5 library using cURL. Perfect. 15 minutes and about 10 lines of code later we were archiving and backing up user content to S3.