Posts Tagged ‘web server’

404 Errors: Report, monetize and analyse : 404 Error page, track and analyse your errors
Monetize Lost Visitors: Make Money With Your Error Pages

When you visit a site, & you have typed in the wrong folder of a domain, on most occasions 404 pages will show up. If that net site haven’t personalized it yet, the browser will automatically generate a 404 page like above.

Why do browsers still call this as a 404 page?! Only geeks know what 404 page!

Lots of sites are speaking about how to monetize your traffic, they mumble about advertisement placement, blah blah blah, but I’m sure some forgot about the error pages! On my everyday surfing activity, I always land to one or two 404 page everyday, that sadly has nothing in it.

Although it’s handy to see some warnings like these, it doesn’t make you any funds at all! All you have is a lost/unknown visitor that most likely won’t come back.

Here’s a useful plugin that notifies you through e mail whenever a person visits a 404 page. Try it for yourself & you’ll see the potential, on the other hand you can check your awstats in your cpanel & check how lots of people came by on your 404 page.

How Do You Set It Up?

If you’re on WordPress, professionally designed themes include a 404.php file. Just log-in in your admin panel, add some custom text in the 404 page template and you’re done.

If the theme you are currently on doesn’t have a 404.php file. You can create one and upload it where your current theme is located.

Add these common codes and save it as 404.php:

<?php header(“HTTP/1.1 404 Not Found”); ?>
<?php get_header(); ?>

<div class=”post”>
Custom text goes here

<?php get_sidebar(); ?>
<?php get_footer(); ?>

For some websites, you can add this one liner code to your .htaccess file:

ErrorDocument 404

If you like you can also add 401 (which will show up if a person visits an authorized page) and 403 (if a person visits a forbidden page) Just change the domain to where you want the visitor to be redirected if they have typed the wrong extension.

Monetization Ideas

After you have set up the error page, let’s now edit the files where your visitor will go. Here are some ideas to start off:

Affiliate Links

The easiest perhaps is this, just add the banners, links, etc… of the product you’re promoting and you’re all done.

Special Offer

If you have a product that you are promoting right now, you can sell it to them in a much lower price andmake them think that it’s a big steal. You can also sell PLR (Private Label Prodcuts) products in alternative if you haven’t created your own product yet.

More References.

Optimizing Error Pages: Creating Opportunities Out Of Mistakes



PHP SMTP e-mail script comparison : PHP Mail Scripts using SMTP transport, a guide for beginners
How to Send Email from a PHP Script Using SMTP Authentication

Part of what makes the PHP mail() function is so simple is its lack of flexibility. Most importantly and frustratingly, the stock mail() does not usually allow you to use the SMTP server of your choice, and it does not support SMTP authentication, required by many a mail server today, at all.

Fortunately, overcoming PHP’s built-in shortcomings need not be difficult, complicated or painful either. For most email uses, the free PEAR Mail package offers all the power and flexibility needed, and it authenticates with your desired outgoing mail server, too. For enhanced security, secure SSL connections are supported.

Send Email from a PHP Script Using SMTP Authentication

To connect to an outgoing SMTP server from a PHP script using SMTP authentication and send an email:

  • Make sure the PEAR Mail package is installed.
    • Typically, in particular with PHP 4 or later, this will have already been done for you. Just give it a try.
  • Adapt the example below for your needs. Make sure you change the following variables at least:
    • from: the email address from which you want the message to be sent.
    • to: the recipient’s email address and name.
    • host: your outgoing SMTP server name.
    • username: the SMTP user name (typically the same as the user name used to retrieve mail).
    • password: the password for SMTP authentication.
Sending Mail from PHP Using SMTP Authentication – Example
<?php require_once "Mail.php"; $from = "Sandra Sender <>"; $to = "Ramona Recipient <>"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = ""; $username = "smtp_username"; $password = "smtp_password"; $headers = array ('From' => $from,   'To' => $to,   'Subject' => $subject); $smtp = Mail::factory('smtp',   array ('host' => $host,     'auth' => true,     'username' => $username,     'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) {   echo("<p>" . $mail->getMessage() . "</p>");  } else {   echo("<p>Message successfully sent!</p>");  } ?>
Sending Mail from PHP Using SMTP Authentication and SSL Encryption – Example
<?php require_once "Mail.php"; $from = "Sandra Sender <>"; $to = "Ramona Recipient <>"; $subject = "Hi!"; $body = "Hi,\n\nHow are you?"; $host = "ssl://"; $port = "465"; $username = "smtp_username"; $password = "smtp_password"; $headers = array ('From' => $from,   'To' => $to,   'Subject' => $subject); $smtp = Mail::factory('smtp',   array ('host' => $host,     'port' => $port,     'auth' => true,     'username' => $username,     'password' => $password)); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) {   echo("<p>" . $mail->getMessage() . "</p>");  } else {   echo("<p>Message successfully sent!</p>");  } ?>

Tutorial: FTP Upload with PHP and cURL : Uploading To FTP Using PHP and CURL

An interesting project that I was working on today required grabbing remote files to a server and then pushing them to a remote FTP server.

PHP has built in FTP functions, but there are many reasons that you might want to use CURL instead.

Here is the code that I used to upload to FTP, slightly modified for portability:

$ch = curl_init();
$localfile = “test.tar”;
$fp = fopen($localfile, ‘r’);

curl_setopt($ch, CURLOPT_URL, ‘’.$localfile);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));

curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);

if ($error_no == 0) {
$message = ‘File uploaded successfully.’;
} else {
$message = “File upload error: $error_no”;
echo $message;

How-to use PHPMailer for SMTP and GMail : PHP mail using GMail SMTP and PHPMailer
Sending e-mails via SMTP with PHPmailer and Gmail

I have been a fan of PHPMailer for quite some time. It is a very easy to use class that offers much more power than the regular PHP mail function. Although one problem I have had PHPMailer have been its rather poor documentation. Methods and attributes are split up with examples and tutorials seemingly overlapping but disconnected as well. However this is a small fault for such a great contribution to the PHP development community.

A thing I found hard to find was a specific example of how to set up PHPMailer to work with a GMail SMTP server. The results are actually quite simple and I managed to get it working with only a few tries. However if you are reading this you probably do not want to waste that 10 minutes, so I’ll just show you the sample code below.

$pm = new PHPMailer();
$pm->Host = 'ssl://';
$pm->SMTPAuth = true;
$pm->Username = '';
$pm->Password = 'yourpassword';

The above code shows all the configuration settings that need to be set. I will not reproduce the normal message attribute settings needed for sending mail in PHP with PHPMailer as you can find them at the PHPMailer website.

Faster PHP Websites using Cache

In this part, I will show you how to ‘retrofit’ caching in to your scripts using the above script as an example. The objective is to speed things up by not having to regenerate the information every time somebody requests a file. Generating the content every request is a waste, on static information such as our CSS.
To add caching they must add things to our script. First, they must collect the information input to the script & generate a filename distinctive to that set of inputs. Secondly, they must look for a cache file & see if it is sufficiently recent. Finally, they must either use the cached copy or generate new content & cache it for next time.

Breaking the Flow

This part of the process really depends on the individual script, however I will show where I am going to break the flow of this script for the caching.

  1. <?php
  2. $fileDirectory = ”;
  3. $file = $_GET[‘q’];
  4. $nameExplode = explode(‘.’, $file);
  5. $ext = $nameExplode[1];
  6. $fileName = $fileDirectory . $file;
  8. if ($ext != ‘css’ AND $ext != ‘htm’ AND $ext != ‘html’) {
  9.     //Check for evil people…
  10.     die(‘Hackers…!’);
  11. } else {
  13.     //Lets get down to business
  14.     $handle = fopen($fileName, ‘r’);
  15.     $fileData = fread($handle, filesize($fileName));
  16.     //Now for some regex wizardry!
  17.     $newData = preg_replace(‘/\s+/’, ‘ ‘, $fileData);
  18.     fclose($handle);
  19.     //Time to output the data.
  21.     if ($ext == ‘css’) {
  22.         header(“Content-type: text/css”);
  23.     }
  24.     echo $newData;
  25. }


Putting it into Action

We will now actually write the code for caching into this script. I will first show the script completed and then go through each piece.

  1. <?php
  2. $fileDirectory = ”;
  3. $file = $_GET[‘q’];
  4. $nameExplode = explode(‘.’, $file);
  5. $ext = $nameExplode[1];
  6. $fileName = $fileDirectory . $file;
  7. $cacheName = ‘./cache/’ . $nameExplode[0] . $nameExplode[1] . ‘.tmp’;
  8. if ($ext != ‘css’ AND $ext != ‘htm’ AND $ext != ‘html’) {
  9.     //Check for evil people…
  10.     print_r($ext);
  11.     die(‘Hackers…!’);
  12. } else {
  13.     if (file_exists($cacheName) AND filemtime($cacheName) > (time() – 86400)) {
  14.         $cacheHandle = fopen($cacheName, ‘r’);
  15.         $newData = fread($cacheHandle, filesize($cacheName));
  16.         fclose($cacheHandle);
  17.         $isCached = TRUE;
  18.     } else {
  19.         //Lets get down to business
  20.         $handle = fopen($fileName, ‘r’);
  21.         $fileData = fread($handle, filesize($fileName));
  22.         //Now for some regex wizardry!
  23.         $newData = preg_replace(‘/\s+/’, ‘ ‘, $fileData);
  24.         fclose($handle);
  25.         //Lets cache!
  26.         $cacheHandle = fopen($cacheName, ‘w+’);
  27.         fwrite($cacheHandle, $newData);
  28.         fclose($cacheHandle);
  29.         $isCached = FALSE;
  30.     }
  31.     //Time to output the data.
  32.     if ($ext == ‘css’) {
  33.         header(“Content-type: text/css”);
  34.         if ($isCached) {
  35.             echo “// Retrieved from cache file. \n”;
  36.         }
  37.     } else {
  38.         if ($isCached) {
  39.             echo ‘<!– Retrieved from cache file. –>’;
  40.         }
  41.     }
  42.     echo $newData;
  43. }


The Explanation

This one’s a bit trickier and a little more likely to leave you scratching you head. But don’t worry, not much has changed and we will go through each section. An extra feature we have included is the refreshing of the cache every 24 hours. This is handy so if you change anything, you can either wait 24 hours or simply empty the cache directory. If you want a different refresh interval just calculate it in seconds.

$cacheName = ‘./cache/’ . $nameExplode[0] . $nameExplode[1] . ‘.tmp’;

This bit of code just gets the file’s name and extension, glues them together and adds the cache directory and the appropriate ‘.tmp’ extension.

  1. if (file_exists($cacheName) AND filemtime($cacheName) > (time() – 86400)) {
  2.     $cacheHandle = fopen($cacheName, ‘r’);
  3.     $newData = fread($cacheHandle, filesize($cacheName));
  4.     fclose($cacheHandle);
  5.     $isCached = TRUE;
  6. } else {


Here we’re checking if we have a cache file saved and if the cache file was created within 24 hours. If both these conditions are met then we open the file and extract its contents to substitute for the scripts output. We also set $isCached to true so we can output some messages at the end.

  1. //Lets cache!
  2. $cacheHandle = fopen($cacheName, ‘w+’);
  3. fwrite($cacheHandle, $newData);
  4. fclose($cacheHandle);
  5. $isCache = FALSE;

Now we are caching the output of the script for us to use in later requests. We simply open a file in write mode, dump our data into it and then close it. Strictly you don’t have to close files in PHP but it’s considered a good practise so I have done it here.

  1. //Time to output the data.
  2. if ($ext == ‘css’) {
  3.     header(“Content-type: text/css”);
  4.     if ($isCached) {
  5.         echo “// Retrieved from cache file. \n”;
  6.     }
  7. } else {
  8.     if ($isCached) {
  9.         echo ‘<!– Retrieved from cache file. –>’;
  10.     }
  11. }

This is another part of the script that was modified a little so that we can offer some feedback through the browser. If the file was retrieved from the cache we can add a message to the script’s output. Notice that the message for CSS scripts has ‘\n’ at the end. This is because the characters ‘//’ comment our entire line and ‘\n’ pushes everything else onto another line. If you want to disable the messages all you have to do is comment out the line ‘$isCached = TRUE;’.

Giving it a Whirl

If we use our script again, we will notice no change until we refresh a second time when we will see a message saying that the file was retrieved from cache. Sweet success! This caching setup can also be applied to the first script with little modification, however, that is left as an exercise for the reader.


Being able to quickly add simple but effective caching to any script that you are working on is an extremely useful skill. It just adds that extra bit to the script, reducing the load on your server and speeding up the site for users. Now that’s win-win!