PHP Session Handling on Heroku

Most web applications use sessions of some kind for things like login systems, authentication, flash messages, etc. But using them on cloud services like Heroku can sometimes be a hastle. This post will hopefully outline some of the different methods you can use to get a fast, adaptable and easy session handler with PHP. Option 1) Single Dyno, Default Session (File) By simply running session_start(); with PHP, by default a file based session will start. This is great because it’s … Continued


Prevent your Site from being used in an iframe

Often as a security measure, it can be a good idea to prevent your site from being put within an iframe. This is a measure to prevent against click-jacking. It’s well supported in most of today’s web browsers – with support for: Chrome 4+Firefox 1.9+IE8+ This can be done by setting the HTTP header X-Frame-Options. Htaccess Header set X-Frame-Options DENY or in PHP <?php header(‘X-Frame-Options: Deny’); If you try and load the site with this header present, within Firefox you … Continued


How to Force the Download of a File with HTTP Headers and PHP

It’s quite a common scenario with the web to want to force a file to download, instead of allowing the browser to open it. This can apply to images, pdfs, html, anything a web browser can open (which is more and more these days). To accomplish this, we need to set some http response headers: Content-Type: application/octet-stream Content-Disposition: attachment; filename=”test.txt” Within PHP was can do this with a function like: function forceDownload($filename, $type = “application/octet-stream”) { header(‘Content-Type: ‘.$type.’; charset=utf-8′); header(‘Content-Disposition: … Continued


Direct Upload to S3 (with PHP & Composer)

This is a continuation in the ‘Direct Upload‘ series: First we began with a look at how you can directly upload a file to s3, talking it through in detail (13/10/2013). We later made another post, explaining how to handle multiple files and updating the code to use AWS’s signature V4 (7/3/2015). Now we’re back with another improvement! Instead of the copy and paste from a blog post solution we were advocating in past blog posts, we’ve now built a … Continued


SQL Searching with Relevance and Natural Language Processing

Working at a start-up in the early stages things are often built to solve a problem at the time, more often than not with a specific client in mind and time-frame in mind. As the company grows and becomes more established sometimes this very same code needs to scale or change based on features or performance original anticipated. One example of this is if your building your own search queries in SQL. We’ve all done it. They start out all … Continued


How to: Install PHP 7 on Ubuntu 14.04 / 16.04

It’s release day! Or it was on the 3rd of December 2015 – the release date for the final version of PHP 7.0. There have been a whole host of improvements made to PHP but we won’t delve too deep into changes. This is a very quick guide on how to update to the latest release for those of you running a Ubuntu Linux system. It is very simple, thanks to ondrej managing a ppa repo for this release, like … Continued


Keep Your PHP Code Clean with Traits

In PHP object-oriented programming (OOP), inheritance means a class can only ever have a single parent, thus PHP is called a single inheritance language. Traits however give the ability for a class to inherit one or more sets of functions from different places and are a way around the single inheritance problem. Traits are sometimes referred to as mixins because they aren’t a class, more just a set of functions which get added to whatever uses that trait – and … Continued


Converting a Decimal to a Fraction in PHP

Below is a helper function to convert a decimal, like for instance 6.5, into a fraction, 13/3 in this example. In fact the function will return an array with the number on the top of the fraction (the numerator) being the first item and the number at the bottom (the denominator) being the second item. Edit 25th Nov 2016: Changed the function to be more accurate and added some phpunit tests for it. The Function: /** * Convert a decimal … Continued


Dedicated vs. Shared Databases: A ‘MySQL has gone way’ fix

While working on a data heavy web application recently we noticed some strange unstable performance with our SQL database – this is a post about how we investigated it and what we did about it. The application was written in PHP and hosted on Heroku in the EU region. We were using a ClearDB database, also hosted in the EU. TL;DR Our ‘MySQL server has gone away‘ message was fixed by moving from a shared database to a dedicated database … Continued


9 Things I Wish I’d Known about PHP Years Ago

I have been working with PHP for a good number of years now – (not including WordPress development, does that count? 😉 ). Here are some of the things I wish I’d known at the very beginning. Most of them are small little code snippets but there are also a few points about diving into the code too deep. Towards the end are a few helpful resources which have helped me out along the way. Coding: 1. Array Manipulation Since … Continued


Create your own Exception Handler in PHP

If you’re used to writing your own PHP applications, you will no doubt be used to having to deal with exceptions being thrown at awkward moments. Resulting in a ugly 500 error being shown to the user and no extra direct from them to take. Luckily we can choose our own way of handling these exceptions, showing a better message or logging the error more efficiently. In an ideal world, we wouldn’t need these as everything would work perfectly, all … Continued


Direct Upload to S3 (using AWS Signature v4 & PHP)

The contents of this article has been replaced by a PHP Composer package, hope you find it useful.   View on Github This article is specifically about directly uploading files to S3 using the AWS Signature Version 4, which is mandatory for new S3 regions, like Frankfurt (EU). It will also become required on other regions at some point as Amazon migrate over, so it’s recommended to use this method where ever possible. For more info, you can read about … Continued