【问题标题】:PHP download link restrictionsPHP下载链接限制
【发布时间】:2014-02-27 10:26:54
【问题描述】:

我正在尝试在下载链接/计数器上设置一些安全性,但作为一个完整的 PHP 新手,我不知道如何处理它。

目前,下载按钮链接到一个简单的 file.php,它在 count.txt 中增加了一个简单的下载计数器。

file.php 看起来像这样:

<?php

$hit_count = @file_get_contents('count.txt');
$hit_count++;
@file_put_contents('count.txt', $hit_count);

header('Location: examplename.zip'); // redirect to the real file to be downloaded

在此处添加一些保护的最简单方法是什么,基于每个 IP(或任何建议)?我没有用户变量,它是一个公共链接,不需要任何注册。

例如:

  • 限制为只能下载一次,然后你必须等待至少30分钟才能再次点击

  • 每个 IP 每天只能下载一次

  • 或其他一些好的解决方案

任何指向正确方向的指针将不胜感激。

【问题讨论】:

  • $_SERVER['REMOTE_ADDR'] 包含用户 IP。至于限制每个 IP 每天一次下载......你将不得不做一些工作。您需要存储所有 IP 地址以及时间戳,并在 6/12/24 小时后过期。某种 SQL 数据库可能是最好的
  • 存储在数据库中会更有帮助。

标签: php hyperlink download restriction


【解决方案1】:

我要做的是基于 IP 的安全性。

检查这个伪代码。

IF ip not stored
   store ip 
   store current_timestamp
   allow download
ELSE 
   Retreive stored timestamp for the ip
   IF currentstamp - retreived timestamp < 30min
      disallow
   ELSE
         update current_timestamp for ip
         allow download

您可以将 IP 存储在文件或数据库中(mysql、postgresql、sqlite 等)。

【讨论】:

    【解决方案2】:

    我从朋友那里得到了一些帮助,这是他如何使用 MD5/Hash + 使链接有效期为 5 分钟来解决的。

    在下载页面上:

    <? $time = time(); ?>
    <a href="yourlink-path/file.php?time=<?=$time?>&hash=<?=md5($time.'BADASS')?>">DOWNLOAD HERE</a>
    

    并且在file.php中:

    <?php
    
    $hash = $_GET['hash'];
    $linktime = $_GET['time'];
    
    if (md5($linktime.'BADASS') == $hash) {
    
      // check if time is not older than 5 mins
      $currenttime = time();
    
      if (($currenttime - intval($linktime)) < 300) {
    
            // DOWNLOAD HERE
            $hit_count = @file_get_contents('count.txt');
            $hit_count++;
            @file_put_contents('count.txt', $hit_count);
            header('Location: myfile.zip'); // redirect to the real file to be downloaded
    
            exit;
    
        } else {
    
            // LINK IS TO OLD, do something here, maybe just:
            exit;
    
        }
    } else {
    
        // Hash has been tampered with, do something, maybe just:
        exit;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 2012-05-24
      • 1970-01-01
      • 2015-09-06
      • 2016-08-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 2012-12-17
      相关资源
      最近更新 更多