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

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.

PHP New Line Breaks n

Recently, I needed to create a new line in a containing multiple lines of text using PHP. I thought I could just do

[cc lang=”php”]$message = ‘abcde n’;[/cc]

but, apparently PHP interprets that as a literal  slash followed by the letter n. The trick is you need to use double-quotes to get the new line to show up, as in

[cc lang=”php”]$message = “abcde n”;[/cc]

How To Add SEO Keywords to a Website Without Cluttering it up

Adding keywords optimized for search engines is extremely important for getting good rankings in Google. But, what do you do when your website’s home page or any other page looks exactly the way you want it without many keywords or you prefer it to mainly contain photos with little text. Well, here’s a “white-hat” way of adding a bunch of text with keywords to your site which will get noticed by Google but not so much by your visitors.  Continue reading How To Add SEO Keywords to a Website Without Cluttering it up

Different Ways to Use a 404 File-Not-Found Handler

1. Redirect to a File Not Found page
If a file doesn’t exist, you can redirect your users to a page saying so.

2. Redirect to the correct location of a file
If, for example, your server treats file names with case sensitivity turned on, then a request to abc.php and Abc.php will be different. If all your file names are in lowercase, then a request to Abc.php will return file not found. Fix this Continue reading Different Ways to Use a 404 File-Not-Found Handler

Reset Your CSS For Equal Display in All Browsers

As you probably know, browsers have their own default CSS styles which determine how a web page looks such as the spacing between elements and how form fields look. To save yourself from the headache of trying to fiddle with CSS settings to get your websites to look right in all browsers, add the following CSS reset code to the top of your CSS file.
[cc lang=”css”]/* reset / remove browser defaults */
body, div, h1, h2, h3, h4, h5, h6, p, ul, ol, li, dl, dt, dd, img, form, fieldset, input, blockquote {
margin: 0px;
padding: 0px;
border: 0px;
font-weight: normal;
}[/cc]

Improve Website Performance Using a CDN (Content Delivery Network)

If you’re a local business targeting a local geographic area, it may not matter much. But, if many of your customers come from different parts of the world, you can improve website performance by using a CDN (Content Delivery Network). Basically, website assets like images and videos can be referenced in your code to come from a CDN instead of the same server hosting your web pages. If a user is viewing your website from the US, then the website’s assets will be downloaded from a fast server near the US. Similarly, is a user is viewing your website from Europe, then the website’s assets will be download from a fast server near Europe. Though CDNs used to be expensive, they’re cheap now. Amazon offers a cheap and easy-to-use CDN called CloudFront where you pay per use.  Here’s one way to set it up: Continue reading Improve Website Performance Using a CDN (Content Delivery Network)