A newsletter is a regularly distributed publication via email in register subscribe user. In this post, i have explained about How to create simple newsletter subscription script in PHP. Here I have created very simple code to Subscribe and Unsubscribe email user list.
Database
Create table columns id, email, active_code.
CREATE TABLE IF NOT EXISTS `email_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `email` varchar(200) NOT NULL, `active_code` varchar(250) NOT NULL, PRIMARY KEY (`id`) )
MySQL Database connection
Database configuration code.
<?php $db = new mysqli('localhost', 'root', '', 'db_name'); ?>
Html
Create simple form element.
<?php @$msg ?> = Error msg
<?php @$msg ?> = Success msg
<form method="post" id="login_form"> <p><label>Email *</label> <input type="email" name="email" required> <input type="submit" value="Submit" name="form_submit"> </p> <div id="logerror"><?php echo @$msg; ?><?php echo @$msg_sucess; ?></div> </form>
PHP
Contain the form email value to check email id exist or not, the New user only to allow to register the table.
$active_code=md5($email.time()); - Generate hash function based o time.
$link - Sending unsubscribe link along the active code.
<?php if(isset($_POST['form_submit'])) { extract($_POST); if($email!="") : $email=mysqli_real_escape_string($db,$email); $emailval = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/'; if(preg_match($emailval, $email)) : $db_check=$db->query("SELECT * FROM `email_user` WHERE email='$email'"); $count=mysqli_num_rows($db_check); if($count< 1) : $active_code=md5($email.time()); $link = 'http://hostname.com/folder/unsubscribe.php?key='.$active_code;
// Change your domain $fetch=$db->query("INSERT INTO email_user(email,active_code)
VALUES('$email','$active_code')"); $to="$email"; //change to ur mail address $strSubject="Mostlikers | Email Subscription"; $message = '<p>Thank you for subscribe with us.</p>' ; $message .= '<p>Click here to unsubscribe your email : <a href="'.$link.'">
unsubscribe</p>' ; $headers = 'MIME-Version: 1.0'."\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; $headers .= "From: info@mostlikers.com"; $mail_sent=mail($to, $strSubject, $message, $headers); $msg_sucess="Your request has been accepted!."; else : $msg="This $email email address is already subscribe with us."; endif; else : $msg="Please enter your valid email id"; endif; else : $msg="Please fill all mandatory fields"; endif; } ?>
unsubcribe.php
<?php if(@$_GET['key']!=""): $active_code=mysqli_real_escape_string($db,$_GET['key']); $fetch=$db->query("SELECT * FROM `email_user` WHERE `active_code` = '$active_code'"); $count=mysqli_num_rows($fetch); if($count==1) : $row=mysqli_fetch_array($fetch); $db->query("DELETE `email_user` WHERE id='$user_id'"); echo "Your email id unsubscribe with us"; else : echo "Please click valid link."; endif; else : header("Location:404.php"); endif; ?
Have a Question? Share your query by writing a comment below.
- A. P. J. Abdul Kalam
Related Topics
- Convert PHP data to JSON API URL
- Generate RSS Feed for website using PHP and JSON script
- Ajax add, view and delete using MySQL
- Active and inactive users concept using PHP and Ajax
- How to Make a Simple Search Engine using PHP and MySQLi
- PDO basic insert,view,update,delete with PHP function
- Send email with HTML template using PHP
- Login with facebook using CodeIgniter
This is a recipe for SQL injection attacks. mysqli_real_escape_string() will not protect you from injection. Use PDO and prepared statements instead. The regular expression is not comprehensive and will reject valid emails. For example there are TLDs that are longer than 4 characters. Additionally the mixing of presentation and business logic leads to spaghetti code that is not easily maintained.
ReplyDeleteGood Post @karthick Muthu keep posting.............
ReplyDeleteThank you bhanu prakash
Delete$uer_id unable to fetch ... it can not fire delete query
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeletePls add clausure "from" after DELETE
ReplyDelete