PHPSESSID – Changes between pages

If your PHPSESSID cookie inexplicably keeps changing between pages and you’ve already checked that session_start() is the very first thing in your scripts and you still can’t find it…Check that any CURL actions defines a cookie jar for storage otherwise it might overwrite any existing PHPSESSID cookie.

You do this by using:

curl_setopt($c, CURLOPT_COOKIEJAR, ‘/tmp/cookie-‘ . session_id());
curl_setopt($c, CURLOPT_COOKIEFILE, ‘/tmp/cookie-‘ . session_id());

It took me far too long to identify this issue!

Adding Attachments To Email Using the MailGun.Com PHP API

When sending messages via mailgun I really struggled to add an attachment using the obvious method:

$mg = new Mailgun('mailgun-api-key');
$msg = $mg->MessageBuilder();
$msg->setFromAddress($from);
$msg->setSubject($subject);
$msg->setTextBody($textBody);
$msg->addAttachment('/var/www/somefile.php');
$mg->post("{$domain}/messages", $msg->getMessage());

The solution was to do this….

$mg = new Mailgun('mailgun-api-key');
$msg = $mg->MessageBuilder();
$msg->setFromAddress($from);
$msg->setSubject($subject);
$msg->setTextBody($textBody);
$files['attachment'] = array();
$files['attachment'][] = '/var/www/somefile.php';
$mg->post("{$domain}/messages", $msg->getMessage(), $files);

Working Out Combinations of a Multidimensional Array in PHP

Occasionally you may wish to work out all the possible combinations of array elements and have probably trawled the internet looking for solutions and have come across many sites giving details of how to work out all the possible permutations… This is NOT permutations, but simple combinations:


function workOutCombinations ($data, &$all = array(), $group = array(), $val = null, $i = 0) {
    if (isset($val)) {
        array_push($group, $val);
    }


    if ($i >= count($data)) {
        array_push($all, $group);
    } else {
        foreach ($data[$i] as $v) {
            workOutCombinations($data, $all, $group, $v, $i + 1);
        }
    }
    return $all;
}


$ar[0] = array('a', 'b', 'c');
$ar[1] = array('1', '2', '3');
$ar[2] = array('red', 'green', 'blue');
$ar[2] = array('cat', 'dog', 'fish');


$combos = workOutCombinations($ar);
print_r($combos);

PLEASE NOTE: This is not my solution. The code above is a tweak of the original found on http://www.farinspace.com/php-array-combinations/ without PHP whining about depricated pass-by-reference usage.

724

Adding the HTTP Extension to PHP on Ubuntu 12.04 LTS

Quite often I forget the requirements of the php_http / pecl_http extension which is required for some network actions. Here is how to install most of what’s required.

apt-get install libcurl3 php5-dev libcurl4-gnutls-dev libmagic-dev
pecl install pecl_http

Then edit the two php.ini files

nano -w /etc/php5/apache2/php.ini
nano -w /etc/php5/cli/php.ini

to include the line

extension=http.so

Then restart Apache

/etc/init.d/apache2 restart