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.
<?php
require_once('db.php');
require('functions.php');
if(!empty($_POST['email']) && valid_email($_POST['email']) && checkUnique('email', $_POST['email']))
{
$random_key = random_string('alnum', 32);
$insert = mysql_query('INSERT INTO `subscribers` (`email`, `rand_key`)
VALUES ("'.mysql_real_escape_string($_POST['email']).'",
"'.$random_key.'")') or die(mysql_error());
$to = $_POST['email'];
$headers = 'From: me@localhost.com'. "\r\n" .
'Reply-To: me@localhost.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = "Please confirm your subscription";
$message = "Hello! You received thismessage because you subscribed to our newsletter. To confirm your action please click on the following link: http://www.mydomain.com/confirm.php?ID=".mysql_insert_id()."&key=".$random_key.".";
if(mail($to, $subject, $message, $headers))
{
echo 'Thank you for subscribing. To confirm your subscription please click on the link that we sent to your email address.';
}
else {
echo "Message not sent. Please make sure you're not
running this on localhost and also that you
are allowed to run mail() function from your webserver";
}
}
elseif (!checkUnique('email', $_POST['email']))
{
echo 'You are already subscribed.';
}
else {
echo 'Please make sure you entered a valid email address and try again';
}
?>
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
<?php
require_once('db.php');
include('functions.php');
if($_GET['ID']!='' && numeric($_GET['ID'])==TRUE && strlen($_GET['key'])==32 && alpha_numeric($_GET['key'])==TRUE)
{
$query = mysql_query("SELECT ID, rand_key, status FROM subscribers WHERE ID = '".mysql_real_escape_string($_GET['ID'])."'");
if(mysql_num_rows($query)==1)
{
$row = mysql_fetch_assoc($query);
if($row['status']==1)
{
echo 'Your subscription was already confirmed !';
}
elseif($row['rand_key']!=$_GET['key'])
{
echo 'The confirmation key that was generated for this subscription does not match with the one entered !';
}
else
{
$update = mysql_query("UPDATE subscribers SET status=1 WHERE ID='".mysql_real_escape_string($row['ID'])."'") or die(mysql_error());
echo 'Congratulations ! You just confirmed your subscription !';
}
}
else {
echo 'Subscriber not found !';
}
}
else {
echo 'Invalid data provided !';
}
?>
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 click here - download link lower on the page.
No. |
Attachments |
Downloads |
|
|
1 |
Ajax newsletter Archive containing Ajax newsletter form files. Uploaded on: 2007-09-21 | 12:59 am |
1316 |
|
Added by roScripts on May-29-2007, 2:45 am

2008-05-22 | 10:56 am
2008-03-21 | 11:17 pm
2008-02-21 | 11:34 pm
2007-06-22 | 12:50 am
2007-06-14 | 08:54 am