Jump to content

Archived

This topic is now archived and is closed to further replies.

urden

serve a download THEN redirect to another page

Recommended Posts


Hi,

First time poster, long time lurker.

I have a failry simple problem I'd really like to get solved. I'm hoping some header magic will do the trick, but let's see shall we. what the problem is:

I'd like to serve a download from my own server with a PHP script, and then immediatly redirect to another website. The basic problem here is that I do not have any control of the final "landing" page that the user goes to, so I cannot serve the download from there (then it would all just be a simple matter of doing a header(Location:) and then have the landing page serve the download)

obviously multiple header redirects won't work, as PHP just takes the last redirect instruction and executes that (that's the behavior I've seen anyway) - But this would be the optimum behavior:
[code]
case 2: //serve file and then redirect (fileurl and offurl)
  header("Location: $fileurl");
  header("Location: $offurl");
  break;
[/code]

I hope this is clear enough and that any of you guys have an idea on how to accomplish this...


Thanks! :)

Share this post


Link to post
Share on other sites
You cant do it with PHP but you can
put javascript so that when one clicks to download it takes to the page and a popup downloads

Share this post


Link to post
Share on other sites

Ahaaa, so you would put a script in the anchor tag which simply pops up the download PHP script and then redirects the user.

That's an acceptable solution (maybe) ... I'll have to mull it over for a bit. The only "obstacle" I see is that it wouldn't be very pretty in conjunction with all the code I have so far (meaning, I'd have to update two pages instead of just one to fit in the javascript when adding a new download - The way I have constructed things anyway)

I was hoping there was some way you could use the headers to accomplish this, or some exotic PHP approach. Oh well... If anybody has some other ideas let me know  :)

Share this post


Link to post
Share on other sites

Alrighty, I got it solved with the javascript approach. I wont post all the code here, just the stuff that took me the longest to get right. There is of course some form code and the underlying php which deals with everything, but I think you would be able to reverse engineer that fairly easily.

this is the Javascript that pops the download script (and tells it that it is being popped so it knows just to serve the file) and then it submits the form to the download script as usual (which then makes the same download script redirect instead of serving the file)

[code]
function popsubmit(formelement,locelement){

//first read vars and pop window
locid = document.getElementById(locelement).value;
vindue=window.open('inc/downloadn.php?locid='+locid+'&popped=yes','newwindow','width=400,height=300,top=100,left=100');

document.forms[formelement].submit();

}
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.