开发者

How do you create a database log when a website user has downloaded a file through php?

I am required to create a small website that people access through a html login/password form where a session begins. I have completed this however I need to log when a user clicks a link and downloads a file.

I nee开发者_高级运维d to record which user has clicked to downloaded the file.

All I have at the moment is a link to a download.php as follows

link

<a href="download.php">download</a>

file

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="somefile.pdf"');
readfile('somefile.pdf');

how would I go about logging who has actually downloaded a file?


It would be very trivial to record the session login details at the top of the download.php file. It's not necessary to redirect via another intermediary file.

If you want to stop people directly linking to download.php you should put some security check at the start of download.php.


You could create a table in your database for the logging, with the user id, dates etc.

In download.php simply have an if statement to determine whether they're logged in (i.e. session is set) and then insert a new row into your logging table, complete with the user id, dates and other data.


Ok two solutions:

First the easy one. Don't do anything! Yes, apache already logs all access to every file in access.log file. I know this is the lazy way but would it satisfy?

Second, ok let's say you need a better way. Does it need to be in the database or can it just be a file? If so, then in your down file open a database connection write to a table and close and then read the pdf file. Or you can write to a flat file if it can be simple as that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜