-5

I have a download script. It downloads from database after insert the email and click GO button, because i want to save the email of users who want to download from my website. it works properly. But every time of downloading it demands email from same user.

It should be like this, if any user insert his email and download file once at that time, then for new download at same time period not necessary to again submit his email.

My code is:

    <?php
require"connection.php";
extract($_REQUEST);
$result=mysql_query("select * from archives1 where volume='$volume'") or die (mysql_error());
while($row=mysql_fetch_array($result))
{
    extract($row);
    echo" <table width='456' height='151'  style='table-layout:fixed;'>
           <tr><td height='38'><b>Section:</b></td><td width='334'>$section</td></tr>
           <tr><td height='38'><b>Title:</b></td><td width='334'>$title</td></tr>
           <tr><td height='38'><b>Author:</b></td><td width='334'>$author</td></tr>
           <tr><td height='38'><b>Country:</b></td><td width='334'>$country</td></tr>
    <tr><td height='38'><b>Page Number:</b></td><td width='334'>$pgno</td></tr>";
    echo"</table>";
    echo"<span id='valid_email'>
                <form method='post' action='pdfdownload.php?filename=$name'>
                    <input type='text' name='valid_email' />
                    <input style='display:none;' type='text' name='valid_date' value='".date('d/m/Y')."' />
                    <input id='go' type='submit' name='valid_submit' value='Go' />
                </form>
              </span>
             <br><br>
              <span style='margin-left:430px;'>
                <a href='#?filename=$name' id='click_email'>Full Text(PDF)</a>
             <hr>";}
?>

It fetches all fields related from volume.

pdfdownload.php is:

<?php
    if(isset($_POST["valid_submit"]))
    {
        require"connection.php";
        extract($_POST);
        mysql_query("insert into archive_email values('','$valid_email','$valid_date')") or die(mysql_error());
        function output_file($file, $name, $mime_type='')
        {
            if(!is_readable($file)) die('File not found or inaccessible!');
            $size = filesize($file);
            $name = rawurldecode($name);
            $known_mime_types=array(
            "htm" => "text/html",
            "exe" => "application/octet-stream",
            "zip" => "application/zip",
            "doc" => "application/msword",
            "jpg" => "image/jpg",
            "php" => "text/plain",
            "xls" => "application/vnd.ms-excel",
            "ppt" => "application/vnd.ms-powerpoint",
            "gif" => "image/gif",
            "pdf" => "application/pdf",
            "txt" => "text/plain",
            "html"=> "text/html",
            "png" => "image/png",
            "jpeg"=> "image/jpg"
            );
            if($mime_type==''){
                $file_extension = strtolower(substr(strrchr($file,"."),1));
                if(array_key_exists($file_extension, $known_mime_types)){
                    $mime_type=$known_mime_types[$file_extension];
                } else {
                    $mime_type="application/force-download";
                };
            };
            //turn off output buffering to decrease cpu usage
            @ob_end_clean();
            // required for IE, otherwise Content-Disposition may be ignored
            if(ini_get('zlib.output_compression'))
            ini_set('zlib.output_compression', 'Off');
            header('Content-Type: ' . $mime_type);
            header('Content-Disposition: attachment; filename="'.$name.'"');
            header("Content-Transfer-Encoding: binary");
            header('Accept-Ranges: bytes');
            // multipart-download and download resuming support
            if(isset($_SERVER['HTTP_RANGE']))
            {
                list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2);
                list($range) = explode(",",$range,2);
                list($range, $range_end) = explode("-", $range);
                $range=intval($range);
                if(!$range_end) {
                    $range_end=$size-1;
                } else {
                    $range_end=intval($range_end);
                }
                $new_length = $range_end-$range+1;
                header("HTTP/1.1 206 Partial Content");
                header("Content-Length: $new_length");
                header("Content-Range: bytes $range-$range_end/$size");
            } else {
                $new_length=$size;
                header("Content-Length: ".$size);
            }
            /* Will output the file itself */
            $chunksize = 1*(1024*1024); //you may want to change this
            $bytes_send = 0;
            if ($file = fopen($file, 'r'))
            {

                if(isset($_SERVER['HTTP_RANGE']))
                fseek($file, $range);
                while(!feof($file) &&(!connection_aborted()) && ($bytes_send<$new_length))
                {

                    $buffer = fread($file, $chunksize);
                    echo($buffer);
                    flush();
                    $bytes_send += strlen($buffer);
                }
                fclose($file);
            } else
            //If no permissiion
            die('Error - can not open file.');
            //die
            die();
        }
        //Set the time out
        set_time_limit(0);
        //path to the file
        $file_path='admin_ijrt/gallery/'.$_REQUEST['filename'];
        //Call the download function with file path,file name and file type
        output_file($file_path, ''.$_REQUEST['filename'].'', 'application/pdf');
    }

?>

Please help. I am not getting any idea.

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
  • 1
    Please format your question. See link for forcing a download (http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php) – DarkBee Oct 09 '14 at 10:52
  • Now it is in valid format, please help – vinod kumar Oct 09 '14 at 10:56
  • possible duplicate of [How do I count unique visitors to my site?](http://stackoverflow.com/questions/18799808/how-do-i-count-unique-visitors-to-my-site) – Alexander Oct 09 '14 at 10:58

2 Answers2

0

Try using session and then checking for a session var.

Billy
  • 2,448
  • 1
  • 10
  • 13
  • I used session and after enter email, it downloads. But if we click again download button. It demands email. But after reload the page, download button do not demands email. So how can i reload page automatically after submit download button. – vinod kumar Oct 10 '14 at 12:56
0

SET Cookie after inserting email on first query (session will destroy after user close browser)

Check if Cookie not exists:

if(!isset($_COOKIE['email'])){
   //check if email already exists or insert email again if you want 
}

Continue your download code....

Sparsh
  • 301
  • 3
  • 9