Announcement

Collapse
No announcement yet.

PHP script for coupon code downloading

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP script for coupon code downloading

    Hi everyone,

    I've been asked by a friend to help out with something he wants to try. Basically, he is a member of a band who has a new record out and he wants to try to give away the album in a downloadable form via some sort of coupon code that the user can submit to their band's website which would be a 'single use' code.

    I searched the web for something like this to try and help him out and I found the following PHP script to do so:

    http://www.allsyntax.com/tutorials/P...ownloads/1.php

    My problem is that I am not great with PHP, and another problem is that there appears to be errors in this PHP code and it looks sloppy.

    If there is someone out there that has experience in PHP that can take a look at this code and tell me where the problem is I would greatly appreciate it. From what I can tell, some of the tags to start and end the PHP code seem to be either missing or placed incorrectly. My web server that I'm using for testing does execute PHP correctly as I have used it for other things, but that code seems to result in code that gets passed through the PHP parser and is just displayed as text.

    Again, any help with this would be greatly appreciated. :yes

  • #2
    Try this, it's only simple but does the job.

    Create a table called coupons with 2 fields id and coupon


    PHP Code:


    <?php

        $couponCode 
    $_POST['couponCode'];

    if (empty(
    $couponCode)){
        echo 
    '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';
        exit();
    };

        include_once(
    "connection.php");

        
    $checkCoupon mysql_query("SELECT id, coupon FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error();

    if(
    mysql_affected_rows() == 0){
        echo 
    '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';
        exit();
    };

    while (
    $recivedCoupon mysql_fetch_array($checkCoupon)){ 
        
    extract($recivedCoupon);
    };

    echo 
    '<a href="yourfile.exe">Download the File Here</a>';
    ?>

    Comment


    • #3
      Hi and thanks for your response.

      Is this based on the example posted on the site or is it a separate script? If you can just elaborate a little more I might be able to figure this out. It seems simple but I am a beginner with PHP.

      Comment


      • #4
        Hi Tek,

        This is one I wrote yesterday when I saw your post.

        Here's how it works,

        Code:
        <form action="coupon.php" method="post" enctype="multipart/form-data" name="coupon">
        <input name="couponCode" type="text" />
        </form>
        The above is the form you should insert into your web page where you would like the coupon box to appear.

        Below is the php code, save this php code as coupon.php and place it into the same folder/directory as your webpage which contains the above form.

        PHP Code:

        <?php 
            
        //This is getting the code typed in from the page where the form is.
            
        $couponCode $_POST['couponCode']; 

           
        //If no code was entered the user will be redirected to a page called invalid_coupon.php (you will need to create this page and place a message on it, somthing like Sorry the code box cannot be empty etc.)
        if (empty($couponCode)){ 
            echo 
        '<meta **********="refresh" content="0;URL=invalid_coupon.php" />'
            exit(); 
        }; 
            
        //You will need to create a file called connection.php and place it into the same directory/folder as this file coupon.php, You must add your mysql connection information into connection.php
            
        include_once("connection.php"); 

            
        //This part is checking the database for the given coupon number
            
        $checkCoupon mysql_query("SELECT id, coupon FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error(); 

        //If the database did not contain the given coupon code the ithe user will be redirected to invalid_coupon.php
        if(mysql_affected_rows() == 0){ 
            echo 
        '<meta **********="refresh" content="0;URL=invalid_coupon.php" />'
            exit(); 
        }; 

        while (
        $recivedCoupon mysql_fetch_array($checkCoupon)){  
            
        extract($recivedCoupon); 
        }; 

        //If all has gone well upto now, i.e the coupon was valid then the coupon link will be shown.
        echo '<a href="yourfile.exe">Download the File Here</a>'
        ?>
        That's all there is to it really, it's simple but easy to modify.

        hope this helps.

        Comment


        • #5
          Hi ianhull,

          Thanks very much for your effort with this.

          I got it to work now but I had to make some changes:

          Code:
          <?php 
              //This is getting the code typed in from the page where the form is.
              $couponCode = $_POST['couponCode']; 
          
             //If no code was entered the user will be redirected to a page called invalid_coupon.php (you will need to create this page and place a message on it, somthing like Sorry the code box cannot be empty etc.)
          if (empty($couponCode)){ 
              echo '<meta **********="refresh" content="0;URL=invalid_coupon.php" />'; 
              exit(); 
          }; 
              //You will need to create a file called connection.php and place it into the same directory/folder as this file coupon.php, You must add your mysql connection information into connection.php
              include_once("connection.php"); 
          	[COLOR="Red"][B]db_login();[/B][/COLOR]
              //This part is checking the database for the given coupon number
              $checkCoupon = mysql_query("SELECT id, coupon FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error()[COLOR="Red"][B])[/B][/COLOR]; 
          
          //If the database did not contain the given coupon code the ithe user will be redirected to invalid_coupon.php
          if(mysql_affected_rows() == 0){ 
              echo '<meta **********="refresh" content="0;URL=invalid_coupon.php" />'; 
              exit(); 
          }; 
          
          while ($recivedCoupon = mysql_fetch_array($checkCoupon)){  
              extract($recivedCoupon); 
          }; 
          
          //If all has gone well upto now, i.e the coupon was valid then the coupon link will be shown.
          echo '<a href="yourfile.exe">Download the File Here</a>'; 
          ?>
          The parts in red had to be added. The 'db_login()' function needed to be called before the query was to be made. Here is my connections.php file:

          Code:
          <?php
          $confg['db_uname'] = "root";
          $confg['db_paswd'] = "";
          $confg['db_host'] = "localhost";
          $confg['db_dbase'] = "mosesdl";
          function db_login() {
          	global $confg;
          	$link = @mysql_connect($confg['db_host'], $confg['db_uname'], $confg['db_paswd']) or die("Error connecting: " . mysql_error());
          	@mysql_select_db($confg['db_dbase'], $link);
          }
          function db_logout() {
          	@mysql_close($link);
          }
          ?>
          The connection information is just for test purposes and will eventually change. Not a problem at all.

          Now, I only need to be able to do two more things with this script which I'm hoping you can help me with. First, once the coupon code has been verified in the database, I need to:

          1) Send the file directly to the user instead of a link. (Or as an alternative, protect the link from being used elsewhere unless the user has already been authenticated with their coupon code.)
          2) Remove the row from the database so that it can't be used again.

          If you can help with these few things, that would be awesome.

          Again, thanks very much for your help. :yes

          Comment


          • #6
            PHP Code:

            <?php  
                
            //This is getting the code typed in from the page where the form is. 
                
            $couponCode $_POST['couponCode'];  

               
            //If no code was entered the user will be redirected to a page called invalid_coupon.php (you will need to create this page and place a message on it, somthing like Sorry the code box cannot be empty etc.) 
            if (empty($couponCode)){  
                echo 
            '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';  
                exit();  
            };  
                
            //You will need to create a file called connection.php and place it into the same directory/folder as this file coupon.php, You must add your mysql connection information into connection.php 
                
            include_once("connection.php");  

                
            //This part is checking the database for the given coupon number 
                
            $checkCoupon mysql_query("SELECT id, coupon FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error();  

            //If the database did not contain the given coupon code the ithe user will be redirected to invalid_coupon.php 
            if(mysql_affected_rows() == 0){  
                echo 
            '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';  
                exit();  
            };  

            while (
            $recivedCoupon mysql_fetch_array($checkCoupon)){   
                
            extract($recivedCoupon);  
            };  

            //Notice the file is outside the root directory (this will prevent people downloading it directly or sending web grabbers to fine .exe on your server
                
            $file "../yourfile.exe"

                
            header("Content-type: application/force-download"); 
                
            header("Content-Transfer-Encoding: Binary"); 
                
            header("Content-length: ".filesize($file)); 
                
            header("Content-disposition: attachment; filename=\"".basename($file)."\""); 
                
            readfile("$file"); 
            } else { 
                echo 
            "Error Selecting File"
            }

            $deleteCouponCode mysql_query("DELETE FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error());
            ?>
            Hope this helps.

            Kind regards
            Ian

            Comment


            • #7
              Thanks very much for the new code.

              A few things with it though... there seems to be some syntax error around here regarding the curly braces:

              Code:
              } else { 
                  echo "Error Selecting File"; 
              }
              The second thing is that when I enter a valid coupon code the script tries to send me coupon.php instead of yourfile.exe. Again, any help is greatly appreciated. :yes

              Comment


              • #8
                PHP Code:


                <?php   
                    
                //This is getting the code typed in from the page where the form is.  
                    
                $couponCode $_POST['couponCode'];   

                   
                //If no code was entered the user will be redirected to a page called invalid_coupon.php (you will need to create this page and place a message on it, somthing like Sorry the code box cannot be empty etc.)  
                if (empty($couponCode)){   
                    echo 
                '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';   
                    exit();   
                };   
                    
                //You will need to create a file called connection.php and place it into the same directory/folder as this file coupon.php, You must add your mysql connection information into connection.php  
                    
                include_once("connection.php");   

                    
                //This part is checking the database for the given coupon number  
                    
                $checkCoupon mysql_query("SELECT id, coupon FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error();   

                //If the database did not contain the given coupon code the ithe user will be redirected to invalid_coupon.php  
                if(mysql_affected_rows() == 0){   
                    echo 
                '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';   
                    exit();   
                };   

                while (
                $recivedCoupon mysql_fetch_array($checkCoupon)){    
                    
                extract($recivedCoupon);   
                };   

                    
                header("Content-type: application/force-download");  
                    
                header("Content-Transfer-Encoding: Binary");  
                    
                header("Content-length: ".filesize("../yourfile.exe"));  
                    
                header("Content-disposition: attachment; filename="../yourfile.exe");  
                    readfile("
                ../yourfile.exe");  

                $deleteCouponCode = mysql_query("DELETE FROM coupons WHERE coupon '$couponCode'")or die(mysql_error()); 
                ?>
                I should of tried it, I got lazy when it all came to setting up the database

                Have a go at this

                If this does not work, try putting the file into the same directory/folder and make the changes at the bottom of the php with the path, remove the ../
                Last edited by ianhull; 02-21-2007, 02:10 PM.

                Comment


                • #9
                  This one works



                  PHP Code:


                  <?php    
                      
                  //This is getting the code typed in from the page where the form is.   
                      
                  $couponCode $_POST['couponCode'];    

                     
                  //If no code was entered the user will be redirected to a page called invalid_coupon.php (you will need to create this page and place a message on it, somthing like Sorry the code box cannot be empty etc.)   
                  if (empty($couponCode)){    
                      echo 
                  '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';    
                      exit();    
                  };    
                      
                  //You will need to create a file called connection.php and place it into the same directory/folder as this file coupon.php, You must add your mysql connection information into connection.php   
                      
                  include_once("connection.php");    

                      
                  //This part is checking the database for the given coupon number   
                      
                  $checkCoupon mysql_query("SELECT id, coupon FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error();    

                  //If the database did not contain the given coupon code the ithe user will be redirected to invalid_coupon.php   
                  if(mysql_affected_rows() == 0){    
                      echo 
                  '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';    
                      exit();    
                  };    

                  while (
                  $recivedCoupon mysql_fetch_array($checkCoupon)){     
                      
                  extract($recivedCoupon);    
                  };    

                      
                  $file "../yourfile.exe";  

                      
                  header("Content-type: application/force-download");  
                      
                  header("Content-Transfer-Encoding: Binary");  
                      
                  header("Content-length: ".filesize($file));  
                      
                  header("Content-disposition: attachment; filename=\"".basename($file)."\"");  
                      
                  readfile("$file");   

                  $deleteCouponCode mysql_query("DELETE FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error());  
                  ?>

                  Comment


                  • #10
                    Thanks very much ianhull. :yes

                    Here is the script I'm using now which appears to work correctly.

                    Code:
                    <?php    
                        //This is getting the code typed in from the page where the form is.   
                        $couponCode = $_POST['couponCode'];    
                    
                       //If no code was entered the user will be redirected to a page called invalid_coupon.php (you will need to create this page and place a message on it, somthing like Sorry the code box cannot be empty etc.)   
                    if (empty($couponCode)){    
                        echo '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';    
                        exit();    
                    };    
                        //You will need to create a file called connection.php and place it into the same directory/folder as this file coupon.php, You must add your mysql connection information into connection.php   
                        include_once("connection.php");    
                    db_login();
                        //This part is checking the database for the given coupon number   
                        $checkCoupon = mysql_query("SELECT id, coupon FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error());    
                    
                    //If the database did not contain the given coupon code the ithe user will be redirected to invalid_coupon.php   
                    if(mysql_affected_rows() == 0){    
                        echo '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';    
                        exit();    
                    };    
                    
                    while ($recivedCoupon = mysql_fetch_array($checkCoupon)){     
                        extract($recivedCoupon);    
                    };    
                    
                        $file = "music/Test.zip";  
                    
                        header("Content-type: application/force-download");  
                        header("Content-Transfer-Encoding: Binary");  
                        header("Content-length: ".filesize($file));  
                        header("Content-disposition: attachment; filename=\"".basename($file)."\"");  
                        readfile("$file");   
                    
                    $deleteCouponCode = mysql_query("DELETE FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error());
                    db_logout();
                    ?>
                    I really appreciate your help with this.

                    Comment


                    • #11
                      Great, glad I could help

                      Comment


                      • #12
                        Just noticed that you have placed your test.zip inside the music directory.

                        If you create a folder outside the root directory called music you can use

                        PHP Code:

                        $file 
                        "../music/Test.zip"
                        Which will prevent people from navigatng to yourdomain.com/music/test.zip


                        [music]
                        [root]
                        [images]
                        [admin]

                        Comment


                        • #13
                          Thanks for the tip ianhull.

                          Actually, the web host has a feature where I can password protect folders so that should do the trick for this example. I will certainly keep your suggestion in mind as well. :yes

                          Comment


                          • #14
                            Hey guys...I know it has been a while since you twon have worked on this issue, but I was wondering if I it is possible to this code to incorporate something like a count down feature. So that the coupon code can only be used one or three times or something like that.

                            Do either one of you two have any suggestions?

                            Thanks a lot guys

                            Comment


                            • #15
                              Sorry, for the late reply, ive been away,

                              Yes this is possible.


                              PHP Code:

                              <?php    
                                  
                              //This is getting the code typed in from the page where the form is.   
                                  
                              $couponCode $_POST['couponCode'];    

                                 
                              //If no code was entered the user will be redirected to a page called invalid_coupon.php (you will need to create this page and place a message on it, somthing like Sorry the code box cannot be empty etc.)   
                              if (empty($couponCode)){    
                                  echo 
                              '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';    
                                  exit();    
                              };    
                                  
                              //You will need to create a file called connection.php and place it into the same directory/folder as this file coupon.php, You must add your mysql connection information into connection.php   
                                  
                              include_once("connection.php");    
                              db_login();
                                  
                              //This part is checking the database for the given coupon number   
                                  
                              $checkCoupon mysql_query("SELECT id, coupon couponLimit FROM coupons WHERE coupon = '$couponCode'")or die(mysql_error());    

                              //If the database did not contain the given coupon code the ithe user will be redirected to invalid_coupon.php   
                              if(mysql_affected_rows() == 0){    
                                  echo 
                              '<meta **********="refresh" content="0;URL=invalid_coupon.php" />';    
                                  exit();    
                              };    

                              while (
                              $recivedCoupon mysql_fetch_array($checkCoupon)){     
                                  
                              extract($recivedCoupon);    
                              };    

                                  if(
                              $couponLimit == 0){
                                  echo 
                              '<meta **********="refresh"
                                  content=";URL=expired_coupon.php" />'
                              ;    
                                  exit();
                                  };
                              //end if

                                  
                              $file "music/Test.zip";  

                                  
                              header("Content-type: application/force-download");  
                                  
                              header("Content-Transfer-Encoding: Binary");  
                                  
                              header("Content-length: ".filesize($file));  
                                  
                              header("Content-disposition: attachment; filename=\"".basename($file)."\"");  
                                  
                              readfile("$file");   

                              $couponLimit--;

                              $updateCouponLimit mysql_query("UPDATE coupons SET couponLimit = '$couponLimit' WHERE coupon = '$couponCode'")or die(mysql_error());
                              db_logout();
                              ?>
                              Last edited by ianhull; 11-23-2007, 08:11 PM.

                              Comment

                              Working...
                              X