Jump to content


serve a download THEN redirect to another page

  • Please log in to reply
3 replies to this topic

#1 urden

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 August 2006 - 02:17 PM


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:
case 2: //serve file and then redirect (fileurl and offurl)
   header("Location: $fileurl");
   header("Location: $offurl");	 

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

Thanks! :)

#2 Chetan

  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 09 August 2006 - 02:33 PM

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
I am a PHP Guru, ask me questions if you want to

#3 urden

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 August 2006 - 02:46 PM

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  :)

#4 urden

  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 09 August 2006 - 03:30 PM

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)

function popsubmit(formelement,locelement){

//first read vars and pop window
locid = document.getElementById(locelement).value;



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users