PHP: Minify Code to Improve Performance

Here’s a quick and simple way to minify all your website’s code on-demand to improve website performance. Pass all of your website’s code into a function that strips out unnecessary stuff as follows:

[cc lang=”php”]/* remove all single line javascript comments */
/* replace all white spaces with a single space */
/* remove all multiline javascript and css comments */
/* remove all HTML comments */
$pattern = array(‘/s+//[[:print:]]+/’, ‘/s+/’, ‘//*.*?*//’, ‘/<!–.*?–>/’);
$replacement = array(”, ‘ ‘, ”, ”);
$minifiedcode = preg_replace($pattern, $replacement, $unminifiedcode);
[/cc]

WARNING: If your javascript is not well-coded (e.g. missing semi-colons), then you may get an error until you fix them.

Styling Hyperlinks

When styling hyperlinks, there are some rules you need to follow:

  • a:hover MUST come after a:link and a:visited
  • a:active MUST come after a:hover
Here’s an example:

a:link {color:#FF0000;}      /* unvisited link */
a:visited {color:#00FF00;}  /* visited link */
a:hover {color:#FF00FF;}  /* mouse over link */
a:active {color:#0000FF;}  /* selected link */

Convert Photoshop Font Point (Pt) to Pixels (Px)

When creating a new website from a Photoshop design, you may find yourself needing to convert between pt to px. Here’s a formula for making the conversion:

font size in pixels / font size in points = browser dpi (96) / Photoshop image resolution (ppi or points per inch)

So, if you have a Photoshop image with 13pt font at 72 ppi, the corresponding font size in pixels for display in a web browser is  Continue reading Convert Photoshop Font Point (Pt) to Pixels (Px)

FIX: PHP Cannot modify header information – headers already sent (line:1)

Sometimes, you may start to see a PHP error complaining about headers already being sent so headers can’t be modified. This usually happens when you echo content and then called the header() function. While those are easy to fix, some others arent, like when PHP complains about output already being sent on LINE 1 when you know there’s nothing on line 1 except “<?php”. Following is an example error
[cc lang=”html”]====================================
PHP WARNING
====================================
PHP Warning: Cannot modify header information – headers already sent by (output started at /home/www/abcde.php:1) in /home/www/def.php on line 90[/cc]
Continue reading FIX: PHP Cannot modify header information – headers already sent (line:1)

Railo: Open-Source Coldfusion Application Server

Recently, I needed to develop a mass emailing application using Coldfusion. I decided to use MySQL this time because it had a web-based management system and I didn’t feel like downloading and installing SQL Server Management Studio. Normally, when developing Coldfusion Apps locally, I would installed the Coldfusion Developer Edition. But, it’s so big and the installation process is kinda long, I decided to Railo. Railo is an open-source CFML processor. It’s compatible with almost all of the original Adobe Coldfusion tags and functions. It’s also very small (8 MB) and the Express version is super simple to install. Just double-click on a batch file and your web server is running. That’s pretty much the installation process. You could even put the whole thing on a USB drive. Continue reading Railo: Open-Source Coldfusion Application Server

Simple, On-The-Fly Image Resizing in PHP

Recently, I installed WordPress and a theme that used an image resizer extension called “TimThumb“. It turns out it’s actually pretty fast and useful. For example, if you have an image on your server called “castle1.jpg”, you can display it in your website as usual using

[cc lang=”html”][/cc]

and, if you want to resize it without having to go into Photoshop, resizing it, uploading it to your server, using TimThumb, you can just do this: Continue reading Simple, On-The-Fly Image Resizing in PHP

Ways to Improve Website Performance

Here are a couple of different ways to improve website performance using 3rd party services:

  • Google PageSpeed Service
    Page Speed Service New! is an online service to automatically speed up loading of your web pages. Page Speed Service fetches content from your servers, rewrites by applying web performance best practices and serves it to end users via Google’s servers across the globe.
    This is a paid service
  •  Amazon CloudFront CDN
    Upload and deliver website components such as images, videos, audio, javascript, etc from Amazon’s cheap CDN
    This is a pay-per-use service
  •  Yotta Site Speed Optimizer
    Similar to Google PageSpeed Service
    This is a paid service
  • CloudFare
    Free to paid

Global Economic Crisis

By now, many of you have probably noticed that the economic crisis that started back in 2008 isn’t over and doesn’t seem to be ending any time soon. Furthermore, demonstrations are taking place more and  more all around the world. Corrupt governments all over the Middle East are being toppled. If you haven’t already done so, I highly suggest you watch some or all of the following videos to learn the real cause of the economic meltdown the world is going through. The more informed you are, the better we can be at preventing governments and Wall Street from letting it happen again.

A Better PHP Redirect Method

PHP provides the header( ) function for redirecting users to another page, e.g.

[cc lang=”php”]header(“Location: http://www.google.com”);[/cc]

but, in case that doesn’t work, here’s a custom method that will attempt to redirect your users in 3 different ways:

[cc lang=”html”]
function redirect($target) {
if (!headers_sent()) {
header(“Location: $target”);
}

echo “n”;
echo “document.location = ‘” . $target . “‘;n”;
echo “

If you are not automatically redirected, please click here“;
exit;
}
[/cc]

then, you can redirect using this new function like this

[cc lang=”php”]redirect(“http://www.google.com”);[/cc]

How to Include Server Request URI in PHP Error Log

Having an error log in PHP is great because it allows you to see errors without showing them to your users. But, it’s often very difficult to fix errors without knowing what page a user was viewing that generated the error. Here’s some code that you can add to your PHP website to include the server request URI in your error log.

[cc lang=”php”]/* *************************/
/* HANDLE NON-FATAL ERRORS */
/* *************************/
function debugErrorHandler($errno, $errstr, $errfile, $errline)
{
if (error_reporting()!==0) {
switch ($errno) {
default:
error_log(“PHP Warning Debug: Server Request URI: ” . print_r($_SERVER[“REQUEST_URI”], true));
break;
}

return false; // false -> Execute PHP internal error handler
}
}

set_error_handler(‘debugErrorHandler’);

/* *************************/
/* HANDLE FATAL ERRORS */
/* *************************/
function shutDownFunction() {
# Using error_get_last is the “right” way, but it requires PHP 5.2+. The back-up is a hack.
if (function_exists(‘error_get_last’)) {
$lastPHPError = error_get_last();
$phpFatalError = isset($lastPHPError) && $lastPHPError[‘type’] === E_ERROR;
} else {
$phpFatalError = strstr($output, ‘<b>Fatal error</b>:’) && ! strstr($output, ‘</html>’);
}

if ($phpFatalError)
error_log(“PHP FATAL Debug: Server Request URI: ” . print_r($_SERVER[“REQUEST_URI”], true));
}

register_shutdown_function(‘shutDownFunction’);[/cc]

Add this to the top of your PHP pages (by including it) and you’ll see the server request URI in your error logs.