2011-09-22 52 views
5

我已經創建了一起以.php一個非常簡單的下載代碼救贖(感謝從這裏幫助)和我有一個很難試圖找出服務下載最好的方法是什麼如果驗證成功。基本上 -服務下載表格後提交W /驗證

用戶輸入無效的代碼 - >頁面刷新錯誤消息。 用戶輸入有效的代碼 - >下載'另存爲' - >刷新頁面。

在那一刻我使用http://www.zubrag.com/scripts/download.php來提供文件,但一旦它開始下載,我的表單刷新頁面,但只有一半加載內容?!

這是我用PHP腳本的形式。

<div class="dcrForm"> 
    <p>Have a physical copy of this release? Claim your digital download by entering your Download Code below.</p> 
    <form action="index.php" method="post"> 
     <input type="text" name="code" class="dcrInput" value=""> 
     <input type="submit" name="harrisSubmit" class="dcrSubmit" value="Submit"> 
    </form> 
<?php 
    include("scripts/dcr_config.php"); 
    $code=""; 
    $log=""; 

    if (isset($_POST['harrisSubmit'])) 
    { 
     $code=$_POST['code']; 

     $link = mysql_connect($hostname, $dbusername, $dbpassword); 
     mysql_select_db("$databasename"); 

     $query = "select count from $harris where code='$code'"; 
     if ($q=mysql_query($query)) 
      if ($r=mysql_fetch_array($q)){ 
       if ($r[0]<3) 
       { 
        $subquery="update $tbname set count='".($r[0]+1)."' where code='$code'"; 
        mysql_query($subquery); 
        ?><script>window.location.href="download.php?f=test.txt";</script><?php 
       } 
      } 
     $log="<p>Invalid code. Try Again.</p>"; 
    } 
    echo $log.""; 
?> 
</div> 

有沒有人有什麼最好的方式來提供下載的想法是什麼?我知道,現在的人誰的文件位置可以下載的文件,但我不知道我怎麼會去保護我

回答

4

我很高興到目前爲止,您取得今天!

如果您打算將用戶重定向到一個下載腳本,該腳本將需要有某種形式連接到它,以防止未經授權的下載令牌,基本上重新驗證給出的代碼或令牌。

在上面的腳本,而不是輸出的JavaScript重定向到下載腳本,你可以這樣做:

<?php 

include "scripts/dcr_config.php"; 
$code = ""; 
$log = ""; 

if (isset($_POST['harrisSubmit'])) { 
    $code = trim($_POST['code']); 

    $link = mysql_connect ($hostname, $dbusername, $dbpassword); 
    mysql_select_db ("$databasename"); 

    $code = mysql_real_escape_string($code); // very important! protects against exploits 

    $query = "select count from $harris where code='$code'"; 
    if ($q = mysql_query ($query)) { 
     if ($r = mysql_fetch_array ($q)) { 
      if ($r [0] < 3) { 
       $subquery = "update $tbname set count='" . ($r [0] + 1) . "' where code='$code'"; 
       mysql_query ($subquery); 

       $file = '/path/to/protecteddownload.txt'; 

       // send file to browser as a download dialog 
       // no content can be output prior to these header() calls 
       header('Content-type: application/octet-stream'); 
       header('Content-Disposition: attachment; filename="file.txt"'); 
       header('Content-Length: ' . filesize($file)); 
       header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 
       header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); 

       echo file_get_contents($file); 
       exit; // terminate script 
      } else { 
       $log = 'Sorry, this code has already been redeemed.'; 
      } 
     } else { 
      $log = 'Invalid download code. Try again.'; 
     } 
    } else { 
     // query failed 
     $log = 'An error occurred validating your code, please try again later.'; 
    } 

    $log = "<p>Invalid code. Try Again.</p>"; 
} 

?> 

<?php if (isset($log) && $log != ''): ?> 
<strong class="error"><?php echo $log ?></strong> 
<?php endif; ?> 

<div class="dcrForm"> 
<p>Have a physical copy of this release? Claim your digital download by 
entering your Download Code below.</p> 
<form action="index.php" method="post"><input type="text" name="code" 
    class="dcrInput" value=""> <input type="submit" name="harrisSubmit" 
    class="dcrSubmit" value="Submit"></form> 
</div> 

下載腳本可能是有些相似的東西我有以上。 這個例子的關鍵在於,您使用file_get_contents提供的文件無法從Web訪問。您只有在輸入有效代碼時才發送。

+0

嗨,再次畫了,謝謝,這是一個非常有用的項目。我已經嘗試了你的上述解決方案,但即使我將$文件設置爲可從Web訪問的位置,並將文件名更改爲我指向的文件名,也無法下載並且頁面刷新並僅載入一半! – Bantros

+0

你使用絕對路徑還是相對路徑? – dizas

+0

我改變了一下代碼的結構,因爲如果你想發送頭文件,那麼在這之前不能輸出html或空格,特別是如果我們要提供下載的話。該代碼塊上方是否有其他HTML代碼發送,但在示例中未顯示?如果是這樣,請確保提供下載的腳本在提供文件之前沒有其他輸出。對不起,以前沒有回覆,我一定錯過了收件箱信息。 – drew010

1

我只有一個簡單的問題,這個文件有多大?難道這是一個PHP超時正在閱讀文件瀏覽器時遇到的情況?

你可以玩的PHP設置,以確認這種(http://php.net/manual/en/function.set-time-limit.php)。

只是我2美分

+0

我從來沒有想到,它是一個大約250MB左右的.zip – Bantros