Hello everybody,,i am completly new in php(i'm a java;) programmer)and i need a help for creating a counter, i have a pdf file on the server and if someone has a counter that does the following:
-when someone clicks on the pdf file ,the counter stores the clicks times value in a txt file,,
<a href=simple.pdf>click here</a>

so i can read that txt file with my java program to show the clicks times
-----------------
i found many hit counters but the problem is how can i make them just for the pdf file
---------
i will post that java program when i'm finished form it:) ..

thanx for your help..

Dani AI

Generated

: was right to suggest serving the PDF through a PHP wrapper. The reliable pattern is: (1) increment a protected counter with proper locking, then (2) stream the PDF back to the client. Redirects can be cached by proxies or browsers and may miss hits; direct streaming through the script guarantees the increment runs for each served request.

Here is a simple, safe PHP example (place the counter file outside the web root and ensure the web server user can write to it):

<?php
// download.php
$pdf = __DIR__ . '/files/simple.pdf';
$counter = __DIR__ . '/../counters/simple.count';

if (!file_exists($pdf)) {
    header('HTTP/1.1 404 Not Found');
    exit;
}

$fp = fopen($counter, 'c+');
if ($fp) {
    if (flock($fp, LOCK_EX)) {
        $count = intval(stream_get_contents($fp));
        $count++;
        rewind($fp);
        ftruncate($fp, 0);
        fwrite($fp, (string)$count);
        fflush($fp);
        flock($fp, LOCK_UN);
    }
    fclose($fp);
}

// send PDF
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($pdf));
header('Content-Disposition: inline; filename="' . basename($pdf) . '"');
readfile($pdf);
exit;
?>

Notes and troubleshooting:

  • Put the counter file outside the web root (or deny direct access) so it cannot be altered by users. Create the counters directory and give the web server user write rights.
  • Use the flock pattern above to avoid race conditions; for high traffic use a database or an atomic store instead of a text file.
  • For very large PDFs, consider using your server's native sendfile mechanism (X-Sendfile / X-Accel-Redirect) so PHP only increments the counter and hands the file transfer to the web server.
  • If your Java app reads the count, prefer an HTTP endpoint that returns plain text (so the server handles locking) rather than reading the file directly while PHP may be writing. If the counter does not update, check file permissions and the web server/PHP error logs.

Recommended Answers

All 2 Replies

The only way that I know of to fire a php file is to open it, generally with a browser. If you go directly to a .pdf file, I have no idea how a php script will be able to detect this action. You could create a php file and serve the .pdf file from that php file. You will find some examples on doing that here:
http://us2.php.net/header

as far as writing to a text file:
http://us2.php.net/manual/en/function.fwrite.php

thnx R0bb0b for your help as u said: to open the pdf within the browser,,that's a good idea i'm trying to do it now,,and i found a good hit counter


thank you again..
nemom

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.