Archive for the ‘PHP’ Category
Wednesday, August 18th, 2010
In my last post I promised to quickly run down my reasons for using the simple, flat file Content Management system Stacey to manage my portfolio.
Why flat file?
As I am sure we are all aware, a traditional CMS is software that allows you to update your content through a user-friendly interface, and generally stores the content in some sort of database. Reasons to use this style of Content Management System includes being easy to use, they enable your site to be accessible from any browser, and you can leverage already existing code to extend your site’s functionality easily.
Sounds good, however there are as many downsides, which I have learned over many years of using a diverse range of software packages both commercial and open source. For one, being a technically savvy computer user, there is little advantage for myself to have a web-based interface as all of my development work is done from my home computer or my laptop. An interface can also be quite inflexible, getting a design pixel-perfect is often quite a chore, and while they are more convenient for an editors point of view from a development aspect they are more challenging due to the existence of a database. Finally, and perhaps most importantly, you cannot easily track and manage the information using a version control management tool such as Git.
A flat file system by contrast, stores all content within a simple file system rather than a database. This allows content as well as structure to be tracked and deployed easily. From a pure efficiency standpoint flat file systems are awesome. I can make a change to my content on my local machine using a text editor and Markdown, push up to my external Git repo, and deploy to my server in seconds without having to touch a database management interface or FTP.
The main thing that I have learned is that while CMS’s are crucial for some purposes, they are actually overcomplicating and problematic for others. Learning where this line is definitely important.
Why Stacey?
There are a few reasons, because its designed for designers, the developer is Australian (biased I know), is simple to use and extend, and also I just really like the bare-bones, minimalistic style.
Posted in PHP | No Comments »
Wednesday, March 11th, 2009
For the best part of 3 months I’ve been trying to find an elegant way to switch between http:// and https:// for certain pages. I crawled the forums and found a number of solutions. The first was to use the .htaccess file and mod_rewrite to rewrite the url. This solution was fine on one server but I had to switch recently and suddenly it was failing miserably for no reason that I could determine, so I was forced to find another way. As always, finding another way is a _good_ thing, and the way I found is nicer, easier and much more dynamic.
Create a file in application/helper called ssl_helper.php
if (!function_exists('force_ssl'))
{
function force_ssl()
{
$CI =& get_instance();
$CI->config->config['base_url'] =
str_replace('http://', 'https://',
$CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 443)
{
redirect($CI->uri->uri_string());
}
}
}
function remove_ssl()
{
$CI =& get_instance();
$CI->config->config['base_url'] =
str_replace('https://', 'http://',
$CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 80)
{
redirect($CI->uri->uri_string());
}
}
Load the helper, then in the constructor for any controller that requires ssl, simply insert:
force_ssl();
In every controller that you don’t want to have ssl put:
if (function_exists('force_ssl')) remove_ssl();
And there you go. I found the force_ssl function on the Codeigniter forum, so all credit goes to the original poster. All I did was provide a function to switch back.
Posted in PHP | 13 Comments »
Tuesday, March 10th, 2009
Below is a trick for quickly commenting and uncommenting optional sections of code, something I use often for switching between multiple database setups.
to
define('DB_NAME', 'blag_development');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
You will note that the only change here is the extra / at the start of the top line, which turns the block comment into a line comment and breaks the block comment, uncommenting the desired sections. The double slash before the closing */ ensures that the closing block comment does not break the code.
Posted in PHP | No Comments »
Friday, January 16th, 2009

I’m just putting some finishing touches on the content management system for the Growth Graph website which will go live in a couple of weeks. I wanted a system for generating labels, reciepts, and invoices incorperated into the content management system in a way that can be flexibly automated. Right now it’s only a fairly bare bones functionality that will require server access and some technical skills to operate well, with the interface coming in the next phase of development.
I needed the ability to generate two types of documents:
- Scalable Vector Graphic (SVG) files for the labels
- Office Documents for reciepts and invoices (eg .doc, .pdf)
The labels
SVG is a language for generating a vector image and because it is basic XML markup it is very easy to make any customized document once you have a template. Simply create a label template with keywords which will be scanned for and then replaced. The scan, replace and download I did in about 7 lines of codeigniter PHP. Have a look at the layout of my label template for an example, fitted exactly to the size of my label printer.
The office documents
I decided to try something I had been thinking about for a while and had some success implementing in Java; using an open XML document format so that I could work with the document in a similar way to that described above. The choice of document format was between the new Microsoft docx and the OpenOffice odt. Odt looked simpler so I went with it, though when it comes down to it both are just zip files containing a bunch of XML and styles. All you need to do to work with such files is to change the extension to ‘.zip’, open it with an archive tool and remove the ‘content.xml’ and paste that into a folder. A program can then copy the original office document and then replace the xml file with a new one with the keywords replaced.
The trick was then to then set this up with a large number of orders with a number of documents, then compile all these documents into a single zip file and push it down the pipes (SSL certified and encrypted for sensitive customer data) to a computer with a program that will pull it all back out again and print it. This simple system, kickstarted daily with a cronjob, will allow me to walk into the office to a pile of orders from the day before, ready to be shipped.
Posted in PHP | No Comments »
Wednesday, December 3rd, 2008
I realised the other day that my knowledge of the most basic unit of php, the humble string, was incomplete! Unbeknownst to me here exists an alternative syntax. Introduced in php 4 the Heredoc syntax is a much easier way of dealing with large strings.
$huh = 'theoretical physics';
echo <<<EOT
String theory is a still-developing approach to $huh, whose original building blocks are one-dimensional extended objects called strings.
EOT;Like with double quotes php will parse the given string looking for variables and other syntax, replacing them as it goes.
For the sake of completeness Nowdoc was included in php 5.3, being to single quotes what Heredoc is to double quotes. In other words, Nowdoc allows you to print a string without the fuss and overhead of parsing it first.
echo <<<'EOT'Throw some single quotes around the EOT and away you go! See the php manual for more.
Peace
Posted in PHP | 1 Comment »
Friday, November 14th, 2008

I just want to quickly describe a job I cracked out on the side the other week. A friend of mine had a client who was in need of a website in a hurry because an ad was booked to run on a local radio station within days and there was no site yet for customers to go to.
The website was fairly straightforward: a customer fills out the form to sign up for a series of weight loss events or request updates about upcoming events, and someone logs into the administration system to administer to the registrations, call clients if requested, and create/edit events.
I coded the system fairly quickly with the assistance of the Code Igniter framework. All in all, the whole project took me about 20 hours. You can observe the results here or the front end anyway. (edit: the customer has since changed some aspects of the site)
Posted in PHP | No Comments »