doukuangxun5382 2013-04-07 16:22
浏览 57
已采纳

如何让我的PHP控制下载程序停止重复下载IP地址?

I have a simple download page that receives a file URL, download counter .TXT file url, and a redirect link for after the download. This is all sent through PHP arguments. Problem is, someone has been spamming the downloaders and messing up the count. I want to put a cap on the download counter so it will only count 3 hits from each address, per downloadable item.

I need to be able to do this with only PHP, and without a MySQL database. I do have reasoning for this, my database server is trash and unreliable, I can't have the counter going down.

This is my current downloader PHP file. Further information, this PHP file is in its own folder at /counter with a /counter/data directory for each download counting TXT file. These files store just a single integer value.

<?
    //Retrieve downloadable file URL
    $c_url = urldecode($_GET['url']);

    //retrieve redirect page URL
    $c_page = urldecode($_GET['page']);

    //Retrieve download counter TXT URL
    $c_file = "../" . $_GET['file'];
?>

<!-- Set up simple styles -->
<head>
    <style type="text/css">
        body{
            background: url("../images/background.png");
            color: #ffffff;
        }
    </style>
</head>

<!-- Creates download file URL in hidden iFrame -->
<iframe src="<?echo $c_url;?>" id="hiddenFrm" style="display:none;" frameborder="0"></iframe>

<body>
<center>
    <h1>Your download is starting</h1><br>
    <p>You should be redirected momentarily...<br>

    <?
    <br><br>

    if (file_exists($c_file))
    {
        //Add one value to the counter file
        file_put_contents($c_file, ((int) file_get_contents($c_file)) + 1);

        //Set an automatic redirect for the page (Return home)
        ?>
        <meta http-equiv="refresh" content="0;url=<?echo $c_page;?>" />
        <?

        //Display information about the download, and alternate exits to the page
        if ($c_url != "") 
        {?>
            <b><a href="<?echo $c_url?>">Click here</a> if the download did not start.<br><br>
            <a href="<?echo $c_page?>">Click here</a> if this page does not close after your download.</b></p>
        <?} 
        else 
        {
        ?>
            <b><a href="<?echo $c_page?>">Click here</a> if you are not redirected automatically</b></p>
        <?
        }
    }
    else
    {
        //In case our counter file is improperly specified
        echo "<br><br><br>Error, the counter file was not found!";
    }
    ?>
    </center>
</body>

My question simplified: How can I store IP addresses of people who download each product and prevent the download counter from counting more than 3 downloads from that address using only PHP and no databases?

Help is much appreciated.

EDIT: As I said above, I can't use databases. Also, I am interested in two solutions but not sure how to make them work (I am looking for code examples), one being cookies, and the other being text-files acting as databases.

  • 写回答

3条回答 默认 最新

  • duanke2503 2013-04-07 16:31
    关注

    Without using any database at all it would be pretty tough. I'd be surprised if you can't use MySQL at all, but maybe something even simpler like SQLite might work. Other than that there are two solutions I can think of: the file system and cookies.

    The problem with cookies are that they are transient, and if you have bots spamming you they will probably reject the cookie setting anyway.

    What you can do is maintain a file of

    IP    download-count    last-download-time
    

    If the file is small enough you can load it into memory with file and search for the IP address with preg_grep and update the count that way.

    Note that IP Address may not be the best bet either because some valid and invalid users may share mobile routers and IP Addresses can potentially be spoofed by use of a proxy, etc. Your best bet would be some sort of user authentication system, but that would be prohibitively difficult without a DB

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀