Archive for the ‘Net: Techy: PHP’ category

PHP Profilers – A Quick summary

December 29th, 2009

More an aid to memory then anything, here’s the PHP profilers I’ve recently heard about:

XDebug by Derick Rethans
The oldest and most well known profiler. Needs modification of the server’s PHP file to run. Compatible with KCachegrind/WinCacheGrind, MacCallGrind and Webgrind and remote debugging tools. Produces quite large debug files (typically 900Kb to 10Mb compare to XHProf’s 110Kb).
XHProf by Facebook Inc (available via PECL)
Large amount of information provided and allows you to “drill down” by various sections – allows comparison of “diffs” between runs to check performance enhancements. Needs modification of the server’s PHP file to run. Also compatible with the *Grind systems. Used by Facebook and Manga High’s online maths games website, but only currently runs on Linux/FreeBSD. Seems to be preferred by iBuildings (see Profiling with XHProf) which is how I originally heard about it.
PHP Quick Profiler (PQP) by Particle tree
Needs modifications to your PHP code, but does not need the server’s PHP modifying (so it can be used on shared hosting easily). Nice and bright. Used by ParticleTree on their Wufoo product.

PHP: self and clone

October 21st, 2009

I recently had to make a PHP class (let’s call it “class_A”) which returned an array of itself (to return multiple search results). All well and good and I used “$object=new self();” to do so like this:


class class_A {
public $setting;

public function doSomething() {
$toReturn=Array();
for ($i=1;$i<100;$i++) { $object=new self(); $object->setting=$i;
Array_push($toReturn,$object);
return $object;
}
}

Calling $class_A->doSomething() then returns 100 “class_A” objects.

However, I then needed to extend the PHP class (let’s call this class_B – declared with “class class_B extends class_A”) to take into account additional settings and I fully expected $object to be a new copy of class_B (as class B is technically the class being run).

Nope, it doesn’t work like that. I ended up with $object being class_A . I had to modify the code to change:
$object=new self();
to:
$object=clone $this;
which then allowed $class_B->doSomething(); to return 100 class_B objects.

Annoying, but something to remember!

PHP: Extract just the domain name from an URL

July 30th, 2009

Although I posted this on Stack Overflow, but I thought for reference purposes I’ll keep a copy here as well on how to extract the domain name from a URL in PHP:

If you just want to handle three character top level domains – then this code works:

<?php
// let's test the code works: these should all return
// example.com , example.net or example.org
$domains=Array('here.example.com',
'example.com',
'example.org',
'here.example.org',
'example.com/ignorethis',
'example.net/',
'http://here.example.org/longtest?string=here');
foreach ($domains as $domain) {
testdomain($domain);
}
function testdomain($url) {
if (preg_match('/^((.+)\.)?([A-Za-z][0-9A-Za-z\-]{1,63})\.([A-Za-z]{3})(\/.*)?$/',$url,$matches)) {
print 'Domain is: '.$matches[3].'.'.$matches[4].'
'."\n";
} else {
print 'Domain not found in '.$url.'
'."\n";
}
}
?>

$matches[1]/$matches[2] will contain any subdomain and/or protocol, $matches[3] contains the domain name, $matches[4] the top level domain and $matches[5] contains any other URL path information.

To match most common top level domains you could try changing it to:

if (preg_match('/^((.+)\.)?([A-Za-z][0-9A-Za-z\-]{1,63})\.([A-Za-z]{2,6})(\/.*)?$/',$url,$matches)) {

Or to get it coping with everything:

if (preg_match('/^((.+)\.)?([A-Za-z][0-9A-Za-z\-]{1,63})\.(co\.uk|me\.uk|org\.uk|com|org|net|int|eu)(\/.*)?$/',$url,$matches)) {

etc etc. If you want a list of top level domains, you may find Mozilla’s TLD List useful and DKIM Reputation Detected Registered Domains code handy.

PHP: Zend Framework and Browser Content-types

July 8th, 2009

I’ve just added a bit more security on to the system I’m working on and for that, I needed to check what the Content-type of the content the browser was sending (as I’m looking at checking that it’s an “application/x-amf” content type requested via Flash). Since I’m using Zend_Framework and wanted to make the check in the controller, it seemed sensible to use:
if (!($this->getRequest()->getHeader('Content-type')=='application/x-amf')) {
throw new Exception('Not a valid request');
}

which worked ok in Firefox. However, when testing in Internet Explorer, I always got “Not a valid request”… Why? Well, IE sends a “Content-Type” header instead of “Content-type”: note the different capitalisation!

It gets worse! Google Chrome (based on Apple WebKit), sends “content-type”.

Here’s the summary

Browser Content type field Content length field
Google Chrome 2.0.172.33 content-type Content-Length
Firefox 3.0.11 Content-type Content-Length
Microsoft Internet Explorer 8.0 Content-Type Content-Length
Safari 4.0 Content-Type Content-Length

Hope it helps somebody else!

PHP: Getting individual packages on Zend Framework

February 22nd, 2009

@binarykitten (on Twitter) had the question I officially give up.. how the hell do you download only 1 component of the Zend Framework? I’ve been looking all over the site and I told her that I wasn’t aware of a method, that there were potential dependencies and to maybe try asking @calevans. He responded with the link http://epic.codeutopia.net/pack/ which appears to be a packager for the PHP Zend Framework versions 1.6 and 1.7 developed by Jani Hartikainen (@jhartikainen).

So, yes, it is possible to (unofficially) download parts of the Zend Framework and not have to worry about dependences.

gamy-dance
%d bloggers like this: