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

A Better PHP Redirect Method

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

[cc lang=”php”]header(“Location:”);[/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“;

then, you can redirect using this new function like this

[cc lang=”php”]redirect(“”);[/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”]/* *************************/
/* *************************/
function debugErrorHandler($errno, $errstr, $errfile, $errline)
if (error_reporting()!==0) {
switch ($errno) {
error_log(“PHP Warning Debug: Server Request URI: ” . print_r($_SERVER[“REQUEST_URI”], true));

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


/* *************************/
/* *************************/
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));


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;