Jump to content

serve a download THEN redirect to another page


urden

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! :)
Link to comment
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  :)
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.