Subscribe via RSS

It's almost crucial for a website to build it's audience using every method possible. A good way of keeping your visitors informed and, most of all, active on your site (don't let them forget you) is by using a newsletter software. Contacting your subscribers from time to time by sending them good newsletters can have a massive impact on your business and earnings as well. We are not going to talk in this tutorial how to keep your subscribers informed and with what but, we are going to learn something. How to collect email addresses to feed our lists. For those of you that are not interested in a tutorial but the application please scroll down to the end of this writing and locate the download link. In my opinion, a "subscribe" form should be visible on all pages that your site is made of and should not be accessible by clicking a link just to get there (this is tested). It should also not reload the page after subscribing or showing any success pages and be time consuming. Very few of them visit your site just to subscribe but if they're there, and they like what you're writing/offering, why not subscribe to your newsletter. A good example would be an ajax form to collect the email addresses and respond with a message without the need to reload or redirect. We will use mootools as a Javascript library and for the AJAX effect, to communicate with a php file which will collect the input of the form and, based on our validation rules, decide what should be done. Our application will be formed from 5 pages: 1 used to connect to the database (db.php), 1 will keep the functions (validation, insertion, etc.) (functions.php), 1 will be used to confirm a subscription after the email is received and the link clicked (confirm.php), 1 will add the subscriber to the database (add.php) and the last one will be the form itself which can be easily implemented into any html or php file. A good thing would be to verify that the subscriber is indeed the owner of the email address provided so I decided to add a "status" field to the MySql table in order to be able to extract only confirmed email addresses when I decide to use them. This will ensure that our list is healthy and that we're not spamming anyone.

CREATE TABLE `subscribers` (   `ID` int(11) NOT NULL auto_increment,   `email` varchar(255) NOT NULL,   `rand_key` varchar(32) NOT NULL,   `status` tinyint(1) NOT NULL default '0',   PRIMARY KEY  (`ID`) ) ENGINE=MyISAM;
The basic validation to allow subscriptions will check to see if the email ($_POST['email']) entered is a valid email, that is not empty and it's not already on our list.
add.php
As you can see from the above code, we first require the functions.php file which holds our validation functions and after that we call them using the only post parameter (the email address) to run the validations (for more snippets and functions please visit the snippets section). Once/if it will pass the validation, the email will be added to our database and an email will be sent out to the subscriber containing a link that needs to be clicked in order to verify their subscription. The link points to confirm.php and will use the ID of the newly inserted email (mysql_insert_id()) and a random key of exact 32 characters to verify the status of the subscriber and it's validity. If the ID+the random key will match and the status of the subscriber is equal to 0, we update the record and change the status to 1 which means a confirmed email address. confirm.php



As you can see, our page will first check if the id provided in our link is numeric (numeric($_GET['ID'])), if the random key is alpha-numeric (alpha_numeric($_GET['key'])==TRUE) and if it has exactly 32 characters (strlen($_GET['key'])==32). Using if/else statements we always communicate with the subscriber and report if the update was successful or not. The application itself is very simple, straightforward and will not interrupt our visitor's activity on the page. That's perfect and as expected. For a demo (will not send out emails!) please clickhere - download link lower on the page.

Download : Visit Here


Posted by ABDUL SABOOR Monday, October 12, 2009

0 comments

Post a Comment