Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output buffering and a header redirect 1

Status
Not open for further replies.

Foamcow

Programmer
Nov 14, 2002
6,092
GB
I'm working on a quick download script that gathers visitor info and logs it in a Mysql table when they try to download a file.

The download link on the page triggers a Greybox 'popup' window and runs a PHP script that checks for the presence of a cookie, shows a form if needed, stores info in the table and finally does the download itself. .

Currently the file download itself is handled by a header("Location:<path to file>") function. I'm aware this may not be the best way to do it but it does what I need for now.

However, this means that I can't output anything to the browser once the header call is made.

The problem is that I need to close the Greybox window with some Javascript but I can't output the necessary code because the header is already sent.

So I'm trying to use output buffering but not having much luck.

For the purposes of this question, let's assume I just want to output something to the browser AND download the file.
How might I do this?

Currently I have something like

Code:
echo("Download complete");
header("Location:/downloads/".$fileinfo['url']);
exit;

Which plainly won't work. I thought with output buffering I could do

Code:
ob_start();
print("Download complete");
header("Location:/downloads/".$fileinfo['url']);
exit;
ob_end_flush(); // moot since the script exits and the buffer is flushed

I've been trying to read up on Output Buffering but I'm not having much luck. I can't find an example that does what I'm trying to do - they all seem to revolve around replacing text within the output buffer.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
hmm, i don't think you're going to win with the direction you're looking at. Predominantly i think that your answer will come from javascript rather than php. (assuming you don't want to go down a flash or java route)

how about this?

1. spawn your greybox (not a dialog of course, but a proper browser window)
2. doesn't matter about the url of the greybox.
3. make the greybox say 'Download in Progress'
4. at the end of the greybox add a javascript redirect to your php script that does the actual download. use a js function to perform the redirect.
5. in the php script DO NOT output headers until you have determined whether or not you can download the file. If you can, output the application/octet-stream headers and then the file. The browser will continue to display the Download in Progress message. If the dl is not permitted, echo the error message and the greybox will change itself.

to have the greybox auto-close, i think this is more complex. the best i can come up with is to have the download script write a uniqueID to the disc or db once it has finished the download. and then have the greybox start an ajax lookup to the server every 5 seconds to check for the presence of the unique ID. if the ID is there the PHP script would delete the ID from the db and send back an OK signal. On receipt the greybox closes. You can also control this process from the window that originally spawned the greybox. You can also hybridise the behaviour by changing the text to 'Download complete' and then closing it 5 seconds later or something. You get the gist, I'm sure!

 
Hmm I think you may be right and I should use JS to check for the cookie and redirect to either the form or the actual download depending on the result.

This is such a pain as the thing works beautifully at the moment except it leaves the greybox on screen and the user needs to click "close".

For the record I think I can close the greybox with a call to a javascript function - at least I seem to remember doing that at some time with another job.

Thanks... I think :)


--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
for sure you can close the box with js (either from the greybox itself, or from the window that spawned it). you could use the suggestion in the last para to sort this out at the proper time.
--with apologies for javascript in the php forum (it's only a lil' bit)--
Code:
//from the greybox
self.close();

//from the spawning process
//assumed opened like this
var greybox = window.open(url);

//close like this
if (false == greybox.closed) {greybox.close();}
 
I was just scribbling out something along the lines of your suggestion and I had an idea.

Instead of header(Location:...) I tried header(Refresh: 2; url=...)

This seems to work.
I can execute some javascript AND download the file

This may save me a fair bit of work!

What I can't seem to remember is what function closes the greybox.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
personally i'd still have the validation done by php etc, just use js to spawn the interaction.

and of course you don't need to have the greybox spawned at all, i guess. you could just float a div on the main page and use that to hold your messages. then either remove it from the DOM or hide it when you're done with it.

for the window closing methods, see above.
 
When I say Greybox I refer to

I worked out that I can call the following from within the greybox to close it.

Code:
parent.parent.GB_hide();


Unfortunately this won't work because, of course, as soon as it closes the header(Refresh:..) can't get called.

So... I put a delay on the javascript call that closes the box.

It seems to work, but gives me the feeling that it's held together with sticky tape and spit. :)
It's bound to fail at some point but will get me through for the moment until I can tackle the problem with a fresh mind.



--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top