Allowing File to Download Only for Subscribed Members Using PHP - Mostlikers

08 June, 2016

Allowing File to Download Only for Subscribed Members Using PHP

Recently i have implemented subscribe to download for facebook login download file. After that I got many request tutorials related with topic script. In this article i have explain about how to allowing file to download only for subscribed members using php and ajax. Follow and implement this logic to your website you will get more subscribe users. If your using google feed burner subscription you have to upload manually website subscriber email list to the database.  

Allowing File to Download Only for Subscribed Members Using PHP


For more about the newsletter subscription follow the below topic

Database

Create a new 'users' table below structure insert your subscriber list.
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(100) NOT NULL,
  `status` enum('0','1') NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
)


Download File Path

Create a download file in any file format (zip,rar,jpeg,png,pdf,txt) save it the project path location.
Ex : http://yoursite.com/downloads/files.zip

Downloading the file script explanation.

  1. Register new subscriber email to the database.
  2. Verify the subscriber user email id for the verification process send email with the activation link.
  3. if user activate the email email change table column value 0  => 1.

HTML

Contains simple HTML code. <div id ="checking_result"> ajax error response display.
<html>
<body>
<div><h4>Download script  for Subscribed users only!</h4></div>
  <input type="text" name="email" id="email"/>
  <input type="submit" id="form_download" value="Download" ><br/>
<div id="checking_result"></div>
</div>
</body>
</html>


Jquery code

$('#form_download ').click - Input email value post to the ajax_download page. Here i have used json to post and get the response. 
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $('#form_download').click(function(){
    var user_email = $('#email').val();
    $.post('ajax_download.php', {email_id : user_email},
    function(data) {
      if(data.status=='1') location.href=data.url; 
      else $('#checking_result').html(data.msg);
    }, "json");
  });
});
</script>


Ajax code response (ajax_download.php)

Contains PHP code. This script helps you to check email id valid or not. $zipFile - Downlaod file location.  
<?php
$db = new mysqli('localhost', 'root', '', 'mostlikers'); 
//database connection
if(@$_POST['email_id']!=""):
    $email=mysqli_real_escape_string($db,$_POST['email_id']); 
    $fetch=$db->query("SELECT * FROM `users` WHERE `email` = '$email'");
    //check the email 
    $count=mysqli_num_rows($fetch); 
    if($count==1) : 
    $row=mysqli_fetch_array($fetch);
    //status '0' - not verify. 
        if($row['status']=='0'):
            $msg="Your email id not verified, subscribe again.";           
        else :
            $status = '1';
            $zipFile = "http://yourhost/download_demo/download_demo.zip";
            //Path location
            $msg="Download file";            
        endif; 
    else : 
        $msg="Sorry no email found subscribe below.";
    endif;
endif;
echo json_encode(array('url' => @$zipFile,'status' => @$status,'msg' => @$msg));
?>










Related Topics

5 comments:

  1. Thanks Karthick, for the tutorial, but on question arises here in mind, that,
    you created users table to store subscribers email, but we have subscribers in feedburner so how we can fetch active subscribers from feedburner so that we can give them downloadable files... i am asking about it...

    ReplyDelete
    Replies
    1. Export your feedburner email list insert to database. Update new user list currently I am updating manually. Wait 2 days I will find feedburenr old api code, because Google has removed that API documentation.

      Delete
    2. ok, thanks karthick you are the best blogger as well as programmer :)

      Delete
  2. Hello,
    Iam developing the ecommerce website for a monthly magazine.once the user has subscribed for annual subscription, he has to be given access to download 12 files.Once he downloads the file the download button should be disable.

    Iam facing issue after downloading one magazine and if the page is refreshed, the download option is getting enabled again for that user.But i need once the downloading is completed the download option should be disable for the entire downloading of full magazines.After downloading of full magazines the button should be enable.For subscribed user.
    I need a solution with database.Please send me source code.

    ReplyDelete
    Replies
    1. Create a subscribe_new new table (id,userid,download_status enum(0,1),date)

      The moment user click the download button by using Ajax insert userid and status =1 subscribe table. In download page check whether that userid already exists or not. If tha user downloaded file, make you button disable.

      Delete