views:

82

answers:

4

Hi. I'm trying to explain as best as I can, sorry for my English. I have a list of links, each linked to a php file with an id by parameters (ex. download.php?id=1 or ?id=2 and so on). This file create a new instance of a class witch return the correct header of the files so it displays the save dialog box of the browser.

Now I need to check if the files is already downloaded in past (The first time you downloaded it I add a field on the mysql db). This checks go ahead if you haven't download the files, else return false.

Here is the problem, when it returns false or something else the browser redirect me to the download.php file, so I get a blank page or what I'm echoing. I need that if the file is already download it show me a js alert for advice ppl. Hope you can understand what i mean. Thanks for help

A: 

it's too late to show a js alert in that case, you'd need to do something like ajax to check whether the file has been downloaded, and then show the alert or start downloading the file then.

once your web browser has started loading a new url (eg download.php) then it is too late, you are already navigating away from the current page.

oedo
Yeah. Tried to do something in AJAX but there is another problem now, the ajax answer can be manipulated so there will be a security issue.Or I'm wrong?
Kreker
You'll need to send something that can be used to identify the session.
zaf
Besides, I can do with AJAX but when I call the php file via AJAX it can't return the modified header for the downlodable file because jquery (i'm using it) can't handle correctly the response...I'm sad >:-(
Kreker
@zaf like what?
Kreker
Sorry, you won't need to send anything. The ajax request will send the session identifier and your php script can check if the user is logged in/out.
zaf
@riccardo don't be sad. Get the ajax php script to return 0 or 1 (or whatever). If the result is 0 then show an alert. If 1 then change the window location to the download script url.
zaf
@zaf thanks, you got exactly the right idea :)
oedo
+2  A: 

Technically you can without ajax, the download.php can output the following if the user has already downloaded the file:

<script>
alert('It was false - you already have the file!');
window.back();
</script>

Just depends how well it integrates with your site. Not tested but thats the general idea.

zaf
mmmm this kind works, but a little unstylish because it show me the blank page, the alert then returns back and reload the page:(thanks for help instead
Kreker
Thats why I said "depends how well it integrates with your site". You can always have a pretty picture with music in the background while the user is deciding to click the button or not.
zaf
A: 

So you've got a page that looks like:

1. file1
2. file2
3. file3

and they've got links to the download script on each. If you want to prevent multiple downloads, you do it in two places. Here on the main file list page, and on the download.php page. When the user clicks on one of the files, you have an onclick handler remove the link from the clicked file. This can be done by refreshing this page (and simply not adding the download link when the page is generated), or using some DOM manipulation to remove the tag around the filename.

The download page will also do checks if the file's been sent previous and can handle that condition itself.

Doing so in both places will degrade nicely if for whatever reason the client doesn't have Javascript enabled.

Marc B
A: 

You need to handle it in two places for the best behavior.

  1. Determine if the user can download the file when you generate the download page, and don't create links for files that can't be downloaded.

  2. To handle the case of someone having multiple windows open or otherwise downloading a file without reloading the downloads page, check if the user can download the file in download.php before sending any headers. If he can't, send a redirect back to the download page:

    header("Location: downloads.php?error=repeat_download");
    exit;
    

    …and use the error parameter to include a message at the top of the file list explaining what happened.

Sidnicious