Jump to content

Limit number of downloads per IP


sg552

Recommended Posts

Hi,

 

I want to limit of mp3 download on my website and I use this code: You see before user download, the script will check and insert their IP to the database.

 

<? 
// every page needs to start with these basic things

// I'm using a separate config file. so pull in those values
require("config.inc.php");

// pull in the file with the database class
require("database.class.php");

// create the $db ojbect
$db = new Database($config['server'], $config['user'], 
                   $config['pass'], $config['database'], $config['tablePrefix']);

// connect to the server
$db->connect();

// snippet of code to see if a user has downloaded too much

$config['download_limit'] = 3;

// check database for their IP
$sql = "SELECT count(*) AS number FROM ".$db->pre."downloads
          WHERE ip='$_SERVER[REMOTE_ADDR]'
          AND category='games'";

$iplocked = $db->query_first($sql);

// check to see if IP and/or cookie is over the limit
if($iplocked['number'] >= $config['download_limit'] || 
$_COOKIE['download_limit'] >= $config['download_limit']){
    echo "over user limit error";
    // YOU: exit or redirect them
}
// they're good to go. allow download


<!DOCTYPE html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head></head>
<body><td align="center"><input type="submit" value="Download File"></td></body>
<html>


else{
    // insert their ip into database
    $data = array('ip'=>$_SERVER['REMOTE_ADDR'], 'time'=>time(), 'category'=>"games");
    $db->query_insert("downloads", $data); 

    // set a cookie too
    setcookie("download_limit", ($_COOKIE['download_limit']+1), time() + 24 * 3600);

    // YOU: send the user to the file
}


// deletes "expired" entries each time script is run
$lockTime = time()-24*3600; // 24hrs
$sql = "DELETE FROM ".$db->pre."downloads
          WHERE category='games'
          AND time < $lockTime";
$db->query($sql); 

// and you're done, remember to close connection
$db->close(); 
?>

 

this is what I run in phpmyadmin:

 

CREATE TABLE `downloads` (
  `id` int(11) auto_increment,
  `ip` varchar(15) NOT NULL,
  `category` varchar(30) NOT NULL,
  `time` TIME NOT NULL,
  PRIMARY KEY  (`id`)
)

 

and what I get is this error:

 

87031616mx6.png

 

any idea ??? thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/123021-limit-number-of-downloads-per-ip/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.