Jump to content

Unsolvable problem. Need real expert!


doa24uk

Recommended Posts

I cannot get my head around this. A user wishes to download a file (file.txt) from a directory.

 

http://www.mysite.com/files/file.txt

 

However an HTACCESS file in the /files/ directory contains the following info

 

RewriteEngine on
RewriteRule (.*)(txt|txt2)$ /files/dl.php?file=$1$2 [QSA]

 

So it redirects the user to the dl.php script.

 

The script (below) works perfectly HOWEVER, the resulting download is of whatever text is on the page at the time of downloading (ie. whatever echo's are spat out by dl.php)

 

I can only think that when the download is forced via the script, it goes into a loop because the HTACCESS is telling it to go back to the dl.php page.

 

Is there a way to fix it or use sessions or something so that it only goes to the dl.php page once?

 

<?php
$lol = $_GET[file];
$max_downloads = '1000';
// $max_downloads = '10000000';
$ip = addslashes($_SERVER['REMOTE_ADDR']);
$todaysdate = date("Y-m-d");
$filex = filesize($lol);
$path = addslashes($_SERVER['REQUEST_URI']);

// DB connect
mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error());

mysql_select_db("DB") or die(mysql_error());

// Retrieve all data
$query1 = mysql_query("SELECT * FROM table WHERE IP='$ip' AND DATE='$todaysdate'")
or die(mysql_error());  

$data=mysql_fetch_array($query1);

if($data[iP]==''){
// store the record into $row

// If no results, input new IP & DATE
mysql_query("INSERT INTO table (IP, DATE, DOWNLOADED, EXEMPT) VALUES('$ip','$todaysdate','','0') ") 
or die(mysql_error());
echo 'no previous data - data now inputted';
}elseif ( $data[EXEMPT]=='1' ) {
header('Content-type: application/octet-stream');
header('Content-Disposition: filename="'.$lol.'"');
header('Content-length: '.$lol);
header('Cache-control: private');
exit();
}else{
// Do Nothing & carry on
}

// Calculate total downloads
$total = $data[DOWNLOADED] + $filex;

if ( $total >= $max_downloads ) {

// This file will take them over their limit
echo 'Run out of downloads & not EXEMPT - no download for you!';
// REDIRECT TO INFO PAGE

} else {
header('Content-type: application/octet-stream');
header('Content-Disposition: filename="'.$lol.'"');
header('Content-length: '.$lol);
header('Cache-control: private');
// REDIRECT TO DOWNLOAD
}
?>

 

I'm so close to getting this working!!!

 

Many thanks for all the help in advance!

 

DoA

Link to comment
Share on other sites

  • 1 year later...

Give this a try:

$lol = $_GET[file];
$max_downloads = '1000';
$ip = addslashes($_SERVER['REMOTE_ADDR']);
$todaysdate = date("Y-m-d");
$filex = filesize($lol);
$path = addslashes($_SERVER['REQUEST_URI']);

// DB connect
mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error());

mysql_select_db("DB") or die(mysql_error());

// Retrieve all data
$query1 = mysql_query("SELECT * FROM table WHERE `IP`='$ip' AND `DATE`='$todaysdate'")
or die(mysql_error());  

$data=mysql_fetch_array($query1);

if(mysql_num_rows($query1) < $max_downloads){
mysql_query("INSERT INTO table (`IP`, `DATE`, `DOWNLOADED`, `EXEMPT`) VALUES('$ip','$todaysdate','','0') ") or die(mysql_error());
echo 'no previous data - data now inputted';
    	header("Pragma: public");
    	header("Expires: 0");
    	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    	header("Content-Type: application/force-download");
    	header("Content-Type: application/octet-stream");
    	header("Content-Type: application/download");
    	header("Content-Disposition: attachment; filename=".basename($lol).";");
    	header("Content-Transfer-Encoding: binary");
    	header("Content-Length: ".filesize($lol));
    	@readfile($row['FileName']);
exit(0); 
}

Link to comment
Share on other sites

Hmmm this is my current database...

 

CREATE TABLE `downloaded` (

  `filepath` varchar(255) NOT NULL,

  `IP` varchar(15) NOT NULL,

  `DATE` datetime NOT NULL,

  `DOWNLOADED` varchar(120) NOT NULL,

  `EXEMPT` varchar(120) NOT NULL,

  UNIQUE KEY `filepath` (`filepath`,`IP`),

  KEY `ipadres` (`IP`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

 

i have it under unique yes.... but i thought that is correct....

Link to comment
Share on other sites

Currently what i have in dl.php

 

<?php

// change this value below
$cs_conn = mysql_connect('localhost', '****', '****');
mysql_select_db('*****', $cs_conn);


$lol = $_GET[file];
$max_downloads = '10';
$ip = addslashes($_SERVER['REMOTE_ADDR']);
$todaysdate = date("d-m-Y");
$filex = filesize($lol);
$path = addslashes($_SERVER['REQUEST_URI']);


// Retrieve all data
$query1 = mysql_query("SELECT * FROM downloaded WHERE `IP`='$ip' AND `DATE`='$todaysdate' AND `EXEMPT`='$exempt AND `downloaded`='$downloaded''")
or die(mysql_error());  

$data=mysql_fetch_array($query1);

if(mysql_num_rows($query1) < $max_downloads){



mysql_query("INSERT INTO downloaded (`IP`, `DATE`, `DOWNLOADED`, `EXEMPT`) VALUES('$ip','$todaysdate','','0') ") or die(mysql_error());



echo 'no previous data - data now inputted';
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($lol).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($lol));
@readfile($row['FileName']);
exit(0); 
}else{
// Do Nothing & carry on
}

// Calculate total downloads
$total = $data[DOWNLOADED] + $filex;

if ( $total >= $max_downloads ) {

// This file will take them over their limit
echo 'Run out of downloads & not EXEMPT - no download for you!';
// REDIRECT TO INFO PAGE

} else {
header('Content-type: application/octet-stream');
header('Content-Disposition: filename="'.$lol.'"');
header('Content-length: '.$lol);
header('Cache-control: private');
// REDIRECT TO DOWNLOAD
}
?>

 

and htacces

RewriteEngine on
RewriteRule (.*)(ace|avi|bin|bmp|doc|exe|gif|iso|jpg|mid|mp3|mpg|pdf|png|ppt|rar|txt|ttf|wav|xls|zip)$ /downloads/dl.php [QSA]

 

and sqlbase code

CREATE TABLE `downloaded` (
  `DATE` datetime NOT NULL,
  `DOWNLOADED` varchar(120) NOT NULL,
  `EXEMPT` varchar(120) NOT NULL,
  `ip` varchar(15) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

 

script is working in half way

 

every time i download file it write this in database...

sqlbaseas5.jpg

 

I would like that every time that something is downloaded that update field downloaded or exempt... and that date would be correct...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.