- The most recent blog articles - mostly about PHP, Golang, databases or general website dev ops.
Running Laravel Migrations on Heroku
Migrations are a really handy was of making database changes, be it schema or data, and keeping it in sync across multiple locations. They are however much more useful when automatic, so when they are needed to be run, they run. This helps prevent errors when you forget to run the migrations for instance. When using platforms like Heroku and tools like Composer, this becomes very easy. All that’s needed is to add the php artisan migrate --force command (–force is needed because they are run on the production) into the post-install-cmd section of your composer.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 messageA MySql Backup Script (with Download Speeds & Hipchat Integration)
It’s a common problem we developers come across, we need to keep a backup of our databases so that if anything happens to our live database we still have a copy of the data. This is a quick script which uses the mysqldump command to grab a copy of a database and save it. It saves the file as a gzip file (because it saves a mega amount of space over time).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: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.Enabling HTTP/2 Support on Apache2 and PHP
I’ve been a keen follower of the exciting changes to the HTTP specification over the past few years – with the release of the HTTP/2 standard in 2015 (after it evolved from the spdy project). If you’re unfamiliar with http/2 there are some great videos available which outline exactly what it is and how it is different from http 1.1 – which we’ve been so accustomed to over the years.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.Super-charge your WordPress Blog: Setup a CDN
Performance is important. We get this. As blog owners if our users get frustrated then we get frustrated. In this post we’ll look at how you can move a blog’s performance and loading times to the next level. A couple of years ago we looked at how to improve a website’s loading times – this post should be more specific than that, focusing primarily on WordPress and how content is delivered to your users.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.Validate a UK Grid Reference in Laravel 5
This will just be a small article and quite a specific one. We’ll be looking at how to verify an input to guarantee it is a valid 8 figure grid reference. This will be checking against the UK Ordnance Survey standard, so two letters followed by four easting numbers and four northing numbers. e.g. AA11112222 It is important to remove any spaces from the string before submitting. This can be achieved through a small JS function and a .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.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.Submit an Array with a HTML Form
Most of us will know how HTML forms work: inputs contained within the form send data to a url, by either a GET or POST request. This will result in the server being able to access this data on the other end. The way to do this usually goes something like this:Getting Started with Laravel: Part 2 – The Setup
In our first part to our Getting Started with Laravel series we looked at how to install Laravel using Composer and Git and then used the php artisan serve command to host a web server locally. In this part we’ll be moving on to look at getting Laravel setup with a database back end and creating our first query. This part might be quite a short one as there isn’t much setup needed with Laravel before diving straight into coding (which is for the best!).Getting Started with Laravel: Part 1 – The Install
This is the first article in a section of posts outlining how to get started using the PHP framework, Laravel. Why? There are many different PHP frameworks out there, some popular names include CodeIgniter and CakePHP, all having their merits. One of the most important aspects of choosing a framework comes down to how you feel when using it – does it make sense and feel good to use.HHVM, Laravel & Heroku – The Killer Combination
Laying the foundations to a new project recently, some new web technologies were considered for use, notable among these was the recent announcement of Facebook’s HHVM engine and HACK language, which is now gaining support and stability. The project itself was going to be run on Heroku, a cloud PaaS (Platform as a Service) which allows websites to scale dynamically as they grow. Aside from what the software was going to run on, the software itself was going to be written in PHP (or HACK maybe?) and built upon the Laravel Framework — choosen because of it’s online support, clean code base and large number of features (e.g. a full ORM, MVC structure, db migrations, etc.).Build your first Python API with Flask
While working with a Raspberry Pi recently I delved into the world of Python – and I come from PHP background, so this was quite an experience. Python, for those who don’t know, is a concise scripting language which works well across different platforms.Direct Upload to S3 (with a little help from jQuery) [Updated]
The contents of this article has been replaced by a PHP Composer package, hope you find it useful. View on Github Whist working on a new web app recently, running on Heroku, we wanted users to be able to upload large files (I’m talking 50mb-150mb) onto our site. We started by having the standard html form post the data to our back-end code, which processed it, etc, etc. This was fine for small files, but large files would hit the maximum request time of 30 seconds for any request on heroku, which then in turn messed up the dyno it was running on.How to: Speed up Your Website
Getting your website to load faster is not like running the 100m – or a marathon. It doesn’t need training or a team behind you to make noticeable improvements. All it takes is a little know-how (which I’ll help you with), an actual website and a few minutes of your time.Choose an Open Graph Image with WordPress
Facebook uses it’s Open Graph technology along with specific Open Graph meta tags to get information about pages with the iconic Like button on them. These are documented extensively on ‘Facebook Developers‘ and are introduced well by David Walsh on his blog.9+ htaccess Uses & Snippets
Htaccess files are a hidden type of file used on Apache (usually Linux based) for configuring certain aspects of the server. For more information on what they are read the Wikipedia article. Htaccess files can be (and are) used for a wide range of things, some of which will be covered in this article, as we will work though some of my favourite.