How I put on my site a downloadable file with unique id for each person who download

-1

How do I put a file to download to my site where the button link always changes after the file is downloaded and never from a person downloading it through the same link that has already been downloaded once?

I use wordpress, I've seen this on websites, but I do not know how to do it, can you help me?

    
asked by anonymous 17.02.2017 / 22:13

1 answer

3

The idea is to create a table with a unicoid field.

A page that will insert $ key = uniqid (md5 (rand ())); in the unicoid field and generate a link download.php? id=". $ key

On the download page, obtain the database key equal to the value of the id parameter. If it exists, force the browser to download and immediately give a delete in the unicoid field.

downloadkey.php

$link = new mysqli ("localhost", "USUARIO", "SENHA", "DB");

if(empty($_SERVER['REQUEST_URI'])) {
    $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'];
}

$url = preg_replace('/\?.*$/', '', $_SERVER['REQUEST_URI']);
$folderpath = 'http://'.$_SERVER['HTTP_HOST'].'/'.ltrim(dirname($url), '/').'/';

$key = uniqid(md5(rand()));

$time = date('U');

echo "<p>Download link: <a href=\"" . $folderpath . "download.php?id=" . $key . "\">Baixar</a></p>";
echo "<p><span class=\"box\">" . $folderpath . "download.php?id=" . $key . "</span></p>";


$sqli = ("INSERT INTO downloadkey (uniqueid,timestamp) VALUES(\"$key\",\"$time\")");
$result= mysqli_query($link,$sqli);

mysqli_close($link);

Download.php

$link = new mysqli ("localhost", "USUARIO", "SENHA", "DB");

if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}

$query = ("SELECT * FROM downloadkey Where uniqueid='".$_GET['id']."'");
$result = mysqli_query($link,$query);

if (mysqli_num_rows($result) > 0) {
    $arquivo = 'arquivo.zip';
    header('Content-type: octet/stream');
    header('Content-disposition: attachment; filename="'.$arquivo.'";'); 
    readfile($arquivo);
    $sql = ("DELETE FROM downloadkey Where uniqueid='".$_GET['id']."'");
    $result = mysqli_query($link,$sql);
    exit;
}else{
    echo "<p>Link de download que você está usando é inválido.";
    echo "<br><a href=\"downloadkey.php\">Clique aqui para obter novo link de download</a></p>";
}

mysqli_close($link);
    
18.02.2017 / 15:26