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!

Setting A Static IP Address On An Ubuntu Virtual Box Machine

Here is are some quick and dirty instructions on setting (fixing) the IP address of Ubuntu within a Virtual Box VM:

  • In the Virtual Box Manager right-click your VM then choose “Settings”
  • On the left click “Network”
  • Change the adapter to “Bridged Adapter”
  • Log into your VM (probably using the VM console)
  • Edit the file
    sudo nano -w /etc/network/interfaces
  • Comment out iface eth0 inet dhcp by putting a # in front of the line
  • Add the following lines replacing the X’s for your network values:
    iface eth0 inet static
    address x.x.x.x
    netmask x.x.x.x
    network x.x.x.x
    broadcast x.x.x.x
    gateway x.x.x.x
    dns-nameservers x.x.x.x
  • Exit nano and save the file ([Control] + [X] then [Y])
  • Restart the interface
    sudo /etc/init.d/networking restart

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.

373

Installing PHP 5.4 on Ubuntu 12.04 (Precise Pangolin)

Installing PHP on Ubuntu 12.04 is very straight forward – however (at the time of writing) the latest version available for Ubuntu 12.04 LTS is 5.3.10, so what if you want to install PHP 5.4.x on your server? Well…

If you’ve never used add-apt-repository you may need to execute the following command

apt-get install python-software-properties

Install a signing key for the PPA (and add a new repo)

add-apt-repository ppa:ondrej/php5

Update the entire apt base

apt-get update -y && apt-get upgrade -y && apt-get dist-upgrade -y

Ta-daaaaa!

 

Source: http://www.barryodonovan.com/index.php/2012/05/22/ubuntu-12-04-precise-pangolin-and-php-5-4-again

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

Setting Up FTPS on Ubuntu 12.04 LTS (Precise Pangolin) With VSFTPD

This is a quick ‘n’ dirty version of an excellent post on http://ubuntuforums.org/showthread.php?t=518293 by epimeteo which describes the in-depth configuration options of VSFTPD, so if you’re an advanced user you should probably go and read it.

However, if all you want is a quick FTPS server and don’t particularly care about user restrictions, IP or connection restrictions this is the post for you…

Install VSFTPD

apt-get install vsftpd

Edit the config file

nano -w /etc/vsftpd.conf

Within the config file make the settings read as follows (uncomment as appropriate)

anonymous_enable=NO
local_enable=YES
write_enable=YES

Add some new settings at the bottom to enforce the SFTP part

ssl_enable=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
ssl_tlsv1=YES
ssl_sslv2=YES
ssl_sslv3=YES

Save it and then start the VSFTPD server

service vsftpd start

That’s all there is to it – you should now be able to login to your server using SFTP!

Installing Webmin on Ubuntu 12.04 LTS (Precise Pangolin)

Webmin is a fantastic tool for administering many aspects of your server such as users, groups, cron job etc. The greatest part of webmin is that it abstracts much tedious command-line work from Linux server administration.

The first step is to find out what the latest version of webmin is numbered. You can do that by pointing your browser at: http://sourceforge.net/projects/webadmin/files/webmin/

At the time of writing the most recent version of webmin is 1.620 – if there is a more recent version available the adjust the following commands.

Install the prerequisites/dependencies:

apt-get install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl apt-show-versions python

Get the install file:

wget http://prdownloads.sourceforge.net/webadmin/webmin_1.620_all.deb

Install the package:

dpkg -i webmin_1.620_all.deb

That’s it! You can not login to webmin on https://YOUR-SERVER-NAME-OR-IP:10000

Installing GearmanD on Ubuntu 12.04 LTS (Precise Pangolin) With PHP Support

I’m sure you’ve already visited many, many sites which explain how to install GearmanD on Ubuntu – but you’ve continued searching because something broke or didn’t quite work out. I too was in the same situation and spent 3 days battling through bits and pieces of information scattered across the web.

Here is the process I went through to install Gearman 1.1.7 on a Rackspace/OpenStack Ubuntu 12.04 instance which (assuming decent connection etc) should take less than 30 minutes!

Make a server instance

Obviously skip this part if you are working in Rackspace cloud or have already provisioned an Ubuntu instance.

  1. Log into your control panel at https://lon.manage.rackspacecloud.com for UK Rackspace customers or  https://manage.rackspacecloud.com for US customers
  2. Click “Hosting”
  3. Click “Cloud Servers”
  4. Scroll down and click “Select” next to Ubuntu 12.04 LTS (Precise Pangolin)
  5. Give your server a name
  6. Select your server size – the size you choose will be dependent on the workload/throughput you expect your server to do, if you need lots of things done quickly with no regard to expense choose a larger server, however if you just want this gearmand server to plod through jobs then a smaller server will probably do.
  7. Click “Launch”
  8. Make a note of the password which is shown during set up – you will need it to log in to the server.

Just sometimes…

All you need to do is:

apt-get install gearmand-server

Install Ubuntu Update & Gearman Prerequisites/Dependancies

  1. Log into your server using your SSH client (I use puTTY)
  2. If you’ve just created the instance, change your password by entering:
    passwd
  3. Update & Upgrade your Ubuntu to have the latest patches etc by running:
    apt-get update
    apt-get upgrade
  4. Because my Ubuntu instance was a clean install I needed to install the LAMP stack – there will be dependencies in here which Gearman requires – such as MySQL – if you’re not installing the LAMP stack you will need to do some futher reading! I use tasksel to do my LAMP install (because I’m lazy)
    tasksel
  5. Install Gearman’s dependencies:
    apt-get install build-essential libboost-thread-dev libboost-thread1.46-dev libcloog-ppl0 libboost-program-options-dev libevent-dev php5-dev libcloog-ppl0

Installing GearmanD

  1. Figure out which version of Gearman you need by visiting https://launchpad.net/gearmand – in my case I’m using 1.1.7 so in the subsequent instructions change 1.1.7 to your version number
  2. Get the version of Gearman you require and unpack it:
    cd /usr/src
    wget http://launchpad.net/gearmand/1.2/1.1.7/+download/gearmand-1.1.7.tar.gz
    tar -xvzf gearmand-1.1.7.tar.gz
    cd gearmand-1.1.7
  3. Make & Install GearmanD
    ./configure
    make && make install
    cd ..

All being well you should now have Gearman install and nothing broke/exploded or otherwise shit its pants during the install process.

Installing the PECL Extension for Gearman

To use Gearman with PHP we need to install the PECL extension – to do that we:

  1. Figure out which version we need by visiting http://pecl.php.net/package/gearman – in my case I’m using 1.1.1 so in the subsequent instructions change 1.1.1 to your version number
  2. Download & unpack the appropriate file
    wget http://pecl.php.net/get/gearman-1.1.1.tgz
    tar -xvzf gearman-1.1.1.tgz
  3. Install the PECL extension:
    cd gearman-1.1.1
    phpize
    ./configure
    make
    make install
    cd ..

Update PHP with Gearman Extension

You made it to the last step! Getting PHP to recognise Gearman…

  1. You need to update your php.ini file(s) with the Gearman extension. As I intend to use Gearman both from the command line and Apache I need to update both of my php.ini files. To locate them I used:
    locate php.ini
  2. Which told me I need to edit /etc/php5/apache2/php.ini and /etc/php5/cli/php.ini. I’m not a huge fan of vi so I use nano to edit instead:
    nano -w /etc/php5/apache2/php.ini
    nano -w /etc/php5/cli/php.ini

    I added the following to each php.ini file:

    extension=gearman.so

In Conclusion

You should now have a working Gearman server – hopefully it took you less than 30 minutes and can now go make a nice cup of tea!