开发者

Download script that downloads the page itself when no ID is specified, what's wrong?

I coded a script that when users want to download a file, it shows an advert first and then start the download passing the ID of the file via $_GET. Problem is that if I reach the page w开发者_如何学JAVAith no ID specified (download_file.php instead of download_file.php?id=1, for instance), the page starts the download of the page itself.

<?php
require("/membri/lostlife/mysql.php");
// Variables:
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM Setting WHERE ID = $id");
$row = mysql_fetch_array($result);
$downloads = $row["Downloads"] + 1;
//
switch ($_GET["action"])
{
    case "download":
    // Download the file:
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=\"$row[Filename]\"");
    readfile("/membri/lostlife/setting/$row[Filename]");
    // Update the database:
    mysql_query("UPDATE Setting SET Downloads = $downloads WHERE ID = $id");
    break;
    default:
    echo "";
    header("Refresh: 5; url=?id=$id&action=download");
}
?>

That's my code. What's wrong with it?


Also you got in your default from your switch a refresh header.. so when the action is NOT 'download' it is going to refresh to action=download.

ill would do it this way:

require("/membri/lostlife/mysql.php");

    $id = $_GET["id"];
    $action = $_GET["action"];

    // if its not empty and it is numeric(check if its a integer can be done in different ways)
    if(!empty($id) && is_numeric($id)) 
    {
        $query = mysql_query("SELECT Downloads, Filename FROM Setting WHERE ID = $id");
        $row   = mysql_fetch_assoc($query);
        $download = $row['Downloads'];
        $filename = $row[Filename];

        if($action == "downoad") {
            header("Content-Type: application/zip");
            header("Content-Disposition: attachment; filename=\"". $filename ."\"");
            readfile("/membri/lostlife/setting/". $filename);
        }
    }
    else
    {
        die("No ID found");
    };

You also updating something? what your doing know is update the download what you got from your select statement? so you don't need to update it? you do you want to count what you download?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜