Jump to content

Download Tracking


DarkPrince2005

Recommended Posts

Greetings Boys and Girls

 

I'm trying to limit the amount of downloads on a specific file. I've found the below script online to check if a file has been successfully downloaded, and then delete it from the downlloads table. but it seems to decriment the download count even if the download was not successful? any ideas...Please it's urgent.

 

<?php
// Sample usage

	include "inc/config.php";
	mysql_connect("$host","$user","$pass");
	mysql_select_db("$dbase");
	$check = mysql_query("select a.session_id,a.reference,a.payment_status,
	b.Session,b.Product_Description as Product_Description,b.Product_Price as Product_Price,b.Product_Quantity as Product_Quantity,
	c.REF,c.FILE as file,
	d.reference,d.file,d.dl_count as dl_count,dl_date,dl_end
	from order_details a
	inner join cart b on b.Session = a.session_id 
	inner join products c on c.REF = b.Product_Description
	inner join downloads d on d.reference = a.reference
	where d.reference = '".$_GET["Reference"]."'
	and a.payment_status = '1'
	and not isnull(d.dl_count)
	and d.dl_count != '0' limit 1");

if(mysql_num_rows($check) > 0){

function sendTest()
{	
$resultssss = $_GET["file"];
if(file_exists($resultssss)){
$res = sendFile($_GET["file"], 'application/octet-stream');
if ($fh = @fopen(dirname(__FILE__).'/result.txt','w')) {
  @fwrite($fh, var_export($res, true));
  @fclose($fh);
  }
	/*if ($res['status']) {
	echo 'Download succeeded';
	} else {
	echo 'Download failed';
	}*/

	$sql = mysql_query("select a.session_id,a.reference,
	b.Session,b.Product_Description as Product_Description,b.Product_Price as Product_Price,b.Product_Quantity as Product_Quantity,
	c.REF,c.FILE as file,
	d.file,d.dl_count as dl_count,dl_date,dl_end
	from order_details a 
	inner join cart b on b.Session = a.session_id 
	inner join products c on c.REF = b.Product_Description
	inner join downloads d on d.reference = a.reference
	where a.reference = '".$_GET["Reference"]."'
	and a.payment_status = '1'
	and d.dl_count != 'NULL'
	and d.dl_count != '0' limit 1");

	if(mysql_num_rows($sql)<1){
	echo 'p';
	}
	else
	{
	saveDownloadStatus($res);
	var_dump(saveDownloadStatus($res))
		while($row = mysql_fetch_array($sql)){
		mysql_query("UPDATE downloads set dl_count = ".($row["dl_count"]-1)." where reference = '".$_GET["Reference"]."'");}
	}
}
else{
echo 'bp';
}
}

// The sendFile function streams the file and checks if the
// connection was aborted.

function sendFile($path, $contentType = 'application/octet-stream')
{


ignore_user_abort(true);

header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="' .
basename($path) . "\";");
header("Content-Type: $contentType");

$res = array('status'=>false,'errors'=>array(),'readfileStatus'=>null,'aborted'=>false);

$res['readfileStatus'] = readfile($path);
	if ($res['readfileStatus'] === false) {
	$res['errors'][] = 'readfile failed.';
	$res['status'] = false;
	}

if (connection_aborted()) {
$res['errors'][] = 'Connection aborted.';
$res['aborted'] = true;
$res['status'] = false;
}

return $res;
}

// Save the status of the download to some place

function saveDownloadStatus($res)
{
$ok = false;
$fh = fopen('download-status-' . $_SERVER['REMOTE_ADDR'] . '-' .
date('Ymd_His'), 'w');
	if ($fh) {
	$ok = true;
	if (!fwrite($fh, var_export($res, true))) {
	$ok = false;
	}
		if (!fclose($fh)) {
		$ok = false;
		}
	}
return $ok;
}


sendtest();
exit;
}
else
{
echo 'as';
}

?>

Link to comment
https://forums.phpfreaks.com/topic/201591-download-tracking/
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.