Press "Enter" to skip to content

Category: Net: Techy: PHP

Posts about the PHP programming language

PHP: Getting individual packages on Zend Framework

@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.

Paypal Express Checkout and Recurring Payments

Are you, like me, using Paypal Express Checkout for integration into your shopping cart/ecommerce site?
Are you, like me, utilising the Paypal Subscriptions (Recurring Payments) options to set up future payments?
Are you, like me, getting a blank page when you are trying to setup a Recurring payment (maybe using the PHP NVP kit) after clicking “Agree and Pay” on the “Review your payment” page of the Paypal sandbox or live site)?

If so, the problem may be because you are sending an “AMT” (Amount) of 0 to Paypal: I did this because I didn’t want to actually take any money now…

It appears this is a long standing issue with the SetExpressCheckout section and RecurringPayments on Paypal and to avoid the blank white page, you’ll need to just send a nominal amount (such as 0.01) for Paypal to process “now”. However, Paypal does charge you a 20p transaction fee on the live system, so you may need to adjust your entire Paypal Express Checkout integration.

PHP: Zend Session: don’t set it up in the Initalizer

I’ve just wasted a few hours trying to get Zend_Session (part of the PHP Zend Framework) working correctly – previously, on this codebase, I had “rolled my own” cookie and session management system, but I thought I’d do it properly and utilise the Zend_Session system and store it all in a database…

It didn’t work.

Short answer as to why my sessions weren’t saving into the database (even after I switched to using the “default” Zend_Session_SaveHandler_DbTable ) was that I was setting up the session within the “routeStartup” section of Initalizer.php (which is loaded, in MVC fashion, via the bootstrap.php file). Changing bootstrap.php to:

// Prepare the front controller.
$frontController = Zend_Controller_Front::getInstance();
// Change to 'production' parameter under production environemtn
$frontController->registerPlugin(new Initializer('development'));
$maxSessionTime=60*60*24*30*6; // six months
Zend_Session::setSaveHandler(new My_Session_DbTable($maxSessionTime));
Zend_Session::rememberMe($maxSessionTime);

(i.e. take move the session savehandler out of the Initializer, but keep all the $thingy=new Zend_Session_Namespace whereever they are needed) means it started to work.

Most annoying thing: that there was no reason it didn’t work (no warnings, no errors, no documentation hints and no ‘out of place feeling’). Meh.

PHP: md5 hashes, base_convert and 32 bit limitations

I’ve had cause to convert an md5 hash from the standard hexadecimal base 16 to base 36 (mainly to make it shorter for presentation purposes). However, using base_convert in PHP proved unreliable as the string wouldn’t always convert back correctly. This is on 64 bit Linux, but I’m not certain PHP has been compiled with 64 bit support (so it is probably defaulting to 32bit).

However, thanks to this post on the PHP documentation, I’ve changed the code to use:

/*use gmp library to convert base. gmp will convert numbers > 32bit*/
if (!function_exists('gmp_strval')) {
    trigger_error('GMP does not appear to be compiled into PHP');
}
function gmp_convert($num, $base_a, $base_b)
{
        return gmp_strval ( gmp_init($num, $base_a), $base_b );
}

To test this, here’s the code I used:
function testconvert() {
    $a=Array(10,23,46,239423, PHP_INT_MAX, 323927832, 174623748237, PHP_INT_MAX.PHP_INT_MAX);
    foreach ($a as $key) {
        $converted=gmp_convert($key,10,36);
        $phpstandard=base_convert($key,10,36);
        print 'key='.$key.'. Convert: '.$phpstandard.','.$converted;
        if (!(base_convert($phpstandard,36,10)==$key)) { print '<strong>Standard PHP Failed</strong>'; }
        if (!(gmp_convert($converted,36,10)==$key)) { print '<strong>GM PHP Failed</strong>'; }
        print '<br>';
    }
    foreach ($a as $skey) {
        $key=md5($skey);
        $converted=gmp_convert($key,16,36);
        $phpstandard=base_convert($key,16,36);
        print 'key='.$key.'. Convert: '.$phpstandard.','.$converted;
        if (!(base_convert($phpstandard,36,16)==$key)) { print '  <strong>Standard PHP Failed</strong>'; }
        if (!(gmp_convert($converted,36,16)==$key)) { print '  <strong>GM PHP Failed</strong>'; }
        print '<br>';
    }
}