Capture / DB record of file download direct link

hey guys,

I have this statement in a PHP file of mine that records downloads of a file, but the file is accessed via an ‘‘a’’ tag on the index page:
<a href="dl.php?f=file.zip">file.zip</a>

and I have 2 other files that help with the recording: dl.php, and conn.php

but, I want to send someone a direct link to a file in one of my dirs. say the file URL is site.com/dir1/file.zip

I want to send the user this link without telling them to go to page first and click a link to get the file like I illustrated above, and what I currently doing in another one of my dirs. can I throw the download transaction into a MYSQL DB by sending just a link to a file like this? there would be no query string obviously. I tried the following but I get no errors. no file is downloaded either, just a blank page renders:

<?php header('dl.php?f=file.zip'); ?>

thanks guys.

If your user is going directly to the file URL then PHP won’t even see the request, so your current setup won’t catch it. You’ll have to configure the web server itself to log the request. There are two approaches you could take.

The first one is to have your web server direct all requests through a PHP endpoint, ensuring your PHP script runs for every request. That way you can do what you want with the request, including logging the access or asking the user to login. You’d do this by configuring the server to redirect http://site.com/dir1/file.zip to something like http://site.com/access_control.php; access_control.php can then check the user’s requested URL and do any required checks / logging before returning the file.

The second option is to configure your webserver to log requests to your database directly; apache has the module mod-log-sql-mysql which will do this and I’d imagine something similar is available if you use a different web server.

this is a very small issue. and it’s not worth going through all the web server trouble to get this one thing done. but i’m going to log the page anyway so I can refer to it later. but one more question…when most files are DL’ed from websites, doesn’t the logging, in terms of throwing the count of downloads into the DB, come from a PHP script anyway? I mean, if someone clicks a DL link on a website and that button points directly to the file that’s being downloaded, how does the web master log those downloads ? by doing one of the things you just pointed out?

That’s right. You could also do it by parsing the access logs already created by your webserver, but that relies on your web server logs lasting forever; they normally get rotated out regularly.

can you give me a little info on the concept of an endpoint? you didn’t say much about that. I looked it up, but this page is the only one that was returned that said anything about the word endpoint:

It’s kind of a vague word. In this instance, I just mean a single PHP file that all requests to your website go through. The original URL that your user typed is still available to the script as a server variable so you can use that to decide what file the user was after, then log the request and return the file.

let me test this out and get back to you. thanks for everything. I will post again when I get it done.

Sponsor our Newsletter | Privacy Policy | Terms of Service