Simple Subscribe and Unsubscribe Script in PHP - Mostlikers

25 November, 2015

Simple Subscribe and Unsubscribe Script in PHP

A newsletter is a regularly distributed publication via email in register subscribe user. In this post i have explain about How to create simple news letter subscription script in php. Here i have created very simple code to Subscribe and Unsubscribe email user list. 

Simple Subscribe and Unsubscribe Script in PHP



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`)
)


Mysqli 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, 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.

"Never stop fighting until you arrive at your destined place - that is, the unique you. Have an aim in life, continuously acquire knowledge, work hard, and have perseverance to realise the great life."
                                             
                                         - A. P. J. Abdul Kalam








Related Topics

4 comments:

  1. 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.

    ReplyDelete
  2. Good Post @karthick Muthu keep posting.............

    ReplyDelete
  3. $uer_id unable to fetch ... it can not fire delete query

    ReplyDelete