- The most recent blog articles - mostly about PHP, Golang, databases or general website dev ops.
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.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 will get this messageHow 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: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 Composer package instead.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.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 many of the previous releases.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 aren’t directly instanciable, so could be described as abstract.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.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 structure (among other things) – and not by any code changes.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.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 the time – but alas we need to prepare for the inevitable.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.A Week with Laravel 5: What’s New
With a PHP project we have just started, we decided to begin development with the Laravel 5 framework – even before it’s been released. The framework is due to be released this month (Jan 2015) and instead of starting with Laravel 4 and attempting to migrate upwards, it seems natural to begin with the newest version even though it’s features might change slightly pre-release. Being comfortable developing with both PHP and Laravel 4, some of the changes with version 5 seemed quite major.How to: Asynchronous PHP
Over the Christmas period I had a play around with getting PHP to run code asynchronously (not as easy as it sounds). When you have code, like API calls, which take anywhere near to a second to complete it drastically adds to the loading times of your pages. One easy solution for web apps is to only call the back-end PHP using AJAX. This means the page doesn’t hang and reload, things can happen concurrently and quickly.Modern PHP Password Hashing
In the early days of PHP, if you wanted to store passwords you had to do a lot of the work yourself – this left many systems vunerable, as it was only half implemented or done badly. Since then, however, the team behind developing PHP has been hard at work making the whole process much easier (thus increasing overall security).Create your own PHP Autoloader
If you’ve worked with any large file in PHP you will probably have come across a large block of include statements at the top of it. Includes allow one file to include many other files and are used a lot in OOP. A large amount of includes at the top your code poses two major annoyances: firstly they’re taking up valuable space which could otherwise be used for more important things; secondly because of their explicit nature, they get left in the code over time when they’re not needed or the file they are directing to gets move resulting in warnings and errors.