CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Add to Download Count Click Counter (http://www.codingforums.com/showthread.php?t=283499)

rpcob 12-04-2012 01:46 AM

Add to Download Count Click Counter
 
Hello,
I have been using a script that uses a counter on the download page when it is visited. It will add 1 to the "dcount" table in my sql for the corresponding file id.
PHP Code:

addToDownloadCount($fid); 

I have removed the download page and added direct links to the display page of the file. Is there a way to simply have the code activate when the download link is clicked?

Thank you in advanced.

mOrloff 12-04-2012 04:08 AM

Are you locked into a PHP-only solution for some reason??
I only ask because this seems like a great opportunity for JavaScript to show off a little.
I'm sure someone more experienced than myself could come up with other (and possibly more elegant??) solutions, but my thought is to leave them as simple links on the page you serve.

Then, use JS to override their default behavior and make an AJAX call to a separate PHP script that serves up the file & does whatever other background stuff you might have in mind.

I have some code clips that can at least get you rolling ... but they're on my dev machine at the shop.
Let me know if there are any constraints you are working under, and I'll try to remember to check back in tomorrow morning (cross your fingers & hold your breath :D).

At a bare minimum, I hope this at least gives you some additional Google fodder.

rpcob 12-05-2012 12:55 AM

Im not stuck on php however my problem with js is sometimes it doesn't translate well on mobile devices.

Any code and directions you could give me would be great. As of right now on the display.php I just have the download counter being used as a page counter and below it a direct link to the file being called.

Thank you for the help.

Redcoder 12-05-2012 12:58 AM

You can pass the direct link to another PHP script via GET that will have the logging code.

E.g http://yourdomain.com/downloads_logg....com/ebook.pdf

The downloads logger can then send the download by headers -header() Check php.net manual after logging the download and counting.

rpcob 12-05-2012 01:50 AM

Quote:

Originally Posted by Redcoder (Post 1297305)
You can pass the direct link to another PHP script via GET that will have the logging code.

E.g http://yourdomain.com/downloads_logg....com/ebook.pdf

The downloads logger can then send the download by headers -header() Check php.net manual after logging the download and counting.

What would the link look like for that?

Im a little familiar with GET command. Ive used it once or twice before. Im already using one on the display page to get the file name and display it as the webpage title. I just don't know where to start for this

Redcoder 12-05-2012 09:49 AM

Quote:

Originally Posted by rpcob (Post 1297328)
What would the link look like for that?

Im a little familiar with GET command. Ive used it once or twice before. Im already using one on the display page to get the file name and display it as the webpage title. I just don't know where to start for this

Check out $_GET contents of url, like $_GET['url'] and you'll have the url of the file, then you parse the URL to get the name of the specific file. Icrement it and use headers to 'push' the download to the users browsers.

rpcob 12-06-2012 12:54 AM

Quote:

Originally Posted by Redcoder (Post 1297394)
Check out $_GET contents of url, like $_GET['url'] and you'll have the url of the file, then you parse the URL to get the name of the specific file. Icrement it and use headers to 'push' the download to the users browsers.

How do I do this without sending them to another page as well as get the headers and counter added included?

PHP Code:

<?php
$varFile 
$_GET["http://url.com/files/'.$filename.'"];
?>

My url already looks like: http://url.com/display.php?nav=display&file=4959

rpcob 12-08-2012 05:12 AM

I've tried three different attempts that both dont seem to work

First: Returns a page full of garbage like if you opened a zip file in notepad (.zip file)
PHP Code:

$fid $_GET['file'];
$results mysql_query("SELECT filename FROM files WHERE id=$fid");
        if (
mysql_numrows($results) == 0){
            echo 
"<b>File not found</b>";
            return;
        }
$file mysql_result($results,0,"filename");
$fileurl 'http://url.com/files/'.$file;

     
// Set headers
     
header("Cache-Control: public");
     
header("Content-Description: File Transfer");
     
header("Content-Disposition: attachment; filename=$fileurl");
     
header("Content-Type: application/zip");
     
header("Content-Transfer-Encoding: binary");
    
     
// Read the file from disk
     
readfile($fileurl);
exit(); 

Second: Returns same garbage (.zip file)
PHP Code:

<?PHP
$fid 
$_GET['file'];
$results mysql_query("SELECT filename FROM files WHERE id=$fid");
        if (
mysql_numrows($results) == 0){
            echo 
"<b>File not found</b>";
            return;
        }
$file mysql_result($results,0,"filename");
$fileurl 'http://url.com/files/'.$file;

header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.basename($fileurl).'"'); //<<< Note the " " surrounding the file name
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' filesize($fileurl));
readfile($fileurl);
?>

Third: Returns same garbage, but also alters page layout
PHP Code:

$fid $_GET["file"];
$results mysql_query("SELECT filename FROM files WHERE id=$fid");
        if (
mysql_numrows($results) == 0){
            echo 
'<b>File not found</b>';
            return;
        }
$file mysql_result($results,0,'filename');
$fileurl "http://url.com/files/".$file;
    
// Inform browser that this is a force-download
    
header('Content-Description: File Transfer');
    
header('Content-Type: application/octet-stream');
    
// Inform browser that data can be binary in addition to text
    
header('Content-Disposition: attachment; filename='.basename($fileurl));
    
header('Content-Transfer-Encoding: binary');
    
// Inform browser that this page expires immediately so that an update to the file will still work.
    
header('Expires: 0');
    
header('Cache-Control: must-revalidate');
    
header('Pragma: public');
    
header('Content-Length: ' filesize($fileurl));
    
// Push actual file.
    
ob_clean();
    
flush();
    
readfile($fileurl);
    exit(); 

On a side note there are both .zip, .rar, and .txt files. Do i just add content type lines?


All times are GMT +1. The time now is 09:46 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.