I'm making my own website and from the website you can download some of my c++ games. on the website i have this code that writes in to a .txt file some info from the visiter like: ip, os , browser and what site did he/she visit.
so ok i have the c++ game rars on my server and i have a simple html link to them which allow you to download them.
so my problem is, is there a way to check if the user downloads one of these rars? I mean is there a way to check if the user presses one of these download links?

here's the link that i currently use:

 <a href="Memory Game source code.rar" TITLE="Download Memory Game">Download</a>

so is there a way to check using php if that link was pressed?

Dani AI

Generated

has you on the right track: route downloads through a PHP endpoint and log there. Just be aware of the nuance: you can reliably record that the request reached your PHP script, but you cannot be 100% sure the user finished the transfer. At best, you can detect disconnects and continue/stop accordingly using PHP’s connection handling functions (e.g., ignore_user_abort() and connection_aborted()), but this is still only an approximation. (php.net)

A minimal, safer download.php that logs and streams efficiently (no output buffering; correct headers; chunk-safe) might look like:

// download.php
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT) ?: 0;
// TODO: look up $path and $name by $id; keep files outside the web root
$path = '/srv/downloads/memory-game.rar';
$name = 'Memory Game source code.rar';

if (!is_file($path)) { http_response_code(404); exit; }

// Example: log the attempt before streaming
// logDownload($id, $_SERVER['REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT']);

if (ob_get_level()) { ob_end_clean(); }
ignore_user_abort(true);
set_time_limit(0);

$size = filesize($path);
$fp = fopen($path, 'rb');

header('Content-Type: application/octet-stream');
header('Content-Length: ' . $size);
$cd = "attachment; filename*=UTF-8''" . rawurlencode($name);
header('Content-Disposition: ' . $cd); // RFC 6266 filename*
header('Accept-Ranges: bytes');

fpassthru($fp);
fclose($fp);
exit;

A few hardening/performance tips for : validate the id-to-file mapping and never trust user-supplied paths; set Content-Disposition per the RFC for robust filenames; and for larger files consider offloading delivery to the web server: Apache via X-Sendfile or nginx via X-Accel-Redirect (with an internal location). This preserves logging/authorization in PHP while letting the server handle the heavy lifting. (rfc-editor.org, tn123.org, nginx.org)

For background: header semantics and file streaming behavior are covered in the PHP manual (including fpassthru vs. readfile). (php.net)

Recommended Answers

All 3 Replies

Try putting a PHP link in the href that sends the request to a PHP file that will process the request and sends back the .RAR file.

eg. <a href="download.php?id=546" title="Download Memory Game">Download</a>

Then inside download.php you put code that sends back the .RAR file by specifying the appropriate headers.
e.g. by using a code along the lines below:

$id = $_GET['id'];
//Get the rar file with id equal to $id
//Then pass the rar name into the function below
function forceDownload($file_name){
    header('Content-Type: application/force-download');
    header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
    readfile($file_name);
}

thanks wilch, i will try that out!

thanks again wilch it worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.