Slim-Jade, A Light-Weight PHP Framework Using Jade Posted on March 29, 2012 Recently, I put together a landing page for an idea I had to try and test whether or not it was viable. I drove traffic to the page with Microsoft’s AdCenter, tracked clicks on some categories and tried to get people to sign up for a mailing list to be informed about when the service goes live. Nothing too mind blowing, but it was both fun and educational. At the time, I had just started working with Node.js but wasn’t really proficient with it. I needed to put my landing pages together quickly, so rather than spend time beocming proficient, I decided to create the landing pages in PHP, a language I’d been using for the last 5+ years. Having used Express and falling in love with the Sinatra style, I decided to check out Slim. Having also used Jade with Express, I didn’t want to write HTML anymore. So what to do? Simple, combine the two! After finding an existing Jade parser for PHP, I spent some time learning how views were rendered in Slim. Later that day, I had a working landing page running on Slim and using Jade to render views. And how’d it work out? Pretty well. Slim is pretty quick, and as I already said, I really like the Sinatra style design. And of course, writing all my markup in Jade (and using Stylus for my css, albeit not automated) was pretty much everything I had hoped for. If I do future PHP development, this will most likely be my setup of choice. Check out Slim-Jade; fork it, edit it, and most importantly, let me know what you think!
Serving Files in Internet Explorer over HTTPS Posted on May 11, 2010 I recently came across a problem where serving a file to Internet Explorer would result in the following error message. This affected IE6, IE7 and IE8 in my testing. The way I was serving the file was through PHP via readfile. Before sending the contents of the file, I was, of course, setting some header parameters so that the browser would handle the file and the user wouldn’t just see some binary garbage on their screen. In my case, I was serving a PDF file that was being generated server-side and sent to the client. The basic header parameters are as follows: // We'll be outputting a PDF header('Content-type: application/pdf'); // It will be called your_file.pdf header('Content-Disposition: attachment; filename="your_file.pdf"'); // declare Files Size here (for the sake of peoples sanity, please add this header('Content-Length: '.filesize($filename)); This works just fine in most situations, but once you introduce a secure connection, IE fails with the above error. Of course, like most IE problems, this doesn’t happen in any other browser. Ugh. Continue reading →
Asynchronous PHP Posted on October 16, 2009 I needed to “asynchronous PHP” recently and it took me a little while to find the solution. What my script basically does is generate a PDF file for the user to download based on some parameters they have chosen. The problem was, the PDF generate take a while and during that time I needed to show the user a screen explaining that the file was being generated, which would also show the download link once the generation was complete. My setup is as follows: generate.php This script is where the user is sent when they click “download” on the parameter choice page. This is where the asynchronous magic has to happen. From here, the user is immediately sent to download.php. download.php This script gives the user either a “please wait” message of a “click here to download” message, depending on where the target PDF has been generated yet. My problem was, even though was added the header redirect code (shown below) to generate.php, the browser would still wait for the PDF to be generated before sending the user to download.php. In other words, it was only synchronous PHP, which didn’t help me at all. header("Location: download.php"); Thanks to this comment on php.net, I was able to make it work! Basically, you need to tell the browser that you are done sending it data, even though the PHP script will keep executing. Code below: //redirects the browser to the new url, but continues processing in the background function redirect_and_continue($sURL) { header( "Location: ".$sURL ); ob_end_clean(); header("Connection: close"); ignore_user_abort(); ob_start(); header("Content-Length: 0"); ob_end_flush(); flush(); session_write_close(); } So, the user is sent to generate.php, which immediately calls redirect_and_continue(‘download.php’), redirecting the user to download.php while still continuing to execute generate.php (and make the PDF file). Once the user is at download.php, the script checks for the existence of the generated PDF file (as indicated by the filename sent through the session) and uses a meta refresh tag in the HTML to keep reloading download.php. Once the PDF file exists, it provides the user with a link to the PDF file for download.