Page 1 of 1

force page refresh?

Posted: Thu Aug 17, 2006 12:26 pm
by fgomez
Hello,

I've just finished a script (with some input from this forum--thanks!) that forces a download on the user's computer.

After the download prompt is delivered, the mySQL database is updated.

The user hits OK to download the file. The dialogue box goes away, and they are back at (still at) the page where they clicked the form button. I should note that the form calls a different file, i.e., the user is on page1.php; the form action is page2.php.

The problem is that the information on page1.php is now out-of-date (because the db has been updated by page2.php).

I want page2.php to force page1.php to refresh.

I tried something like this:

Code: Select all

//this is page2.php
header('Content-type: text/htm') ;
header("Content-Disposition: attachment; filename=\"$filename\"") ;
readfile("./files/$filename") ;
header('Location: http://www.example.com/page1.php') ;
die() ;
... but I'm told by PHP that I already sent headers (lines 1 and 2 of this snippet). Any suggestions? Is this question clear enough?

Thanks!

Posted: Thu Aug 17, 2006 12:29 pm
by s.dot
I'd imagine you'd have to remove the linebreaks inbtween your header()'s

Also, by forcing the download.. the headers are already sent. Therefore you cannot send them again with the location request.

You could do a javascript redirect.

Posted: Thu Aug 17, 2006 12:33 pm
by MarK (CZ)
Readfile sends output, no headers beyond that point possible.

Posted: Thu Aug 17, 2006 12:34 pm
by fgomez
Not sure what you mean... Lines 1 and 2 work fine the way they are; it's when I add the third header() that I get errors. Plus, I need the readfile() to occur before the last header().

Posted: Thu Aug 17, 2006 12:36 pm
by fgomez
MarK (CZ) wrote:Readfile sends output, no headers beyond that point possible.
Thanks, Mark. I didn't think it would work when I tried it, but it never hurts to try. Any suggestions as to how to achieve the desired effect?

Posted: Thu Aug 17, 2006 12:37 pm
by s.dot
scottayy wrote:You could do a javascript redirect.
:-D This of course would assume that all your visitors have javascript enabled. Are you looking for a pure PHP solution?

Posted: Thu Aug 17, 2006 12:45 pm
by fgomez
scottayy wrote:
scottayy wrote:You could do a javascript redirect.
:-D This of course would assume that all your visitors have javascript enabled. Are you looking for a pure PHP solution?
Whoops, didn't see your entire post the first time. The project is an admin interface, so I guess I could rely on a javascript as the visitors would be few... though I'd prefer to stick to PHP...

Posted: Thu Aug 17, 2006 1:02 pm
by fgomez
OK... I may need help with this JavaScript suggestion. This didn't work...

Code: Select all

header('Content-type: text/htm') ;
		header("Content-Disposition: attachment; filename=\"$filename\"") ;
		readfile("./files/$filename") ;
?>		
<script type="text/javascript">
<!--
window.location = "http://www.example.com/"
//-->
</script>
<?php
		die() ;
Maybe I'll try an onsumbit event back on page1.php? I've never used an onsubmit... don't know if that will allow the form to submit or not...

Posted: Thu Aug 17, 2006 1:03 pm
by feyd
Do the header()/page-level redirection to the end goal page. On that page, meta-refresh to the file or a script that sends the file.

Posted: Thu Aug 17, 2006 1:06 pm
by fgomez
feyd wrote:Do the header()/page-level redirection to the end goal page. On that page, meta-refresh to the file or a script that sends the file.
I'm always glad when Feyd responds to my questions because I know I'm going to get the answer! Unfortunately, I don't understand what you're trying to say. Could you slow it down a notch or two?

Posted: Thu Aug 17, 2006 1:17 pm
by feyd
foo.php

Code: Select all

<?php

session_start();
$_SESSION['fileServe'] = $filename;
session_write_close();
header('Location: http://domain/bar.php');

?>
<!DOCTYPE ....>
<html>
  <head>
    <title>Redirecting to elsewhere</title>
    <meta http-equiv="refresh" content="2;url=/bar.php">
  </head>
  <body>
    You will be redirected shortly. Please <a href="/bar.php">click here</a> if you have no patience.
  </body>
</html>
bar.php

Code: Select all

<?php

session_start();
$filename = $_SESSION['fileServe'];

header('Content-type: ...');
// blah blah
readfile($filename);
exit;

?>

That's given the scenario so far. Myself, I would actually make foo.php a "your download will begin shortly" page. It wouldn't header() redirect, but meta-refresh to the file, either via a script or directly.

Posted: Thu Aug 17, 2006 4:06 pm
by s.dot
fgomez wrote:OK... I may need help with this JavaScript suggestion. This didn't work...

Code: Select all

header('Content-type: text/htm') ;
		header("Content-Disposition: attachment; filename="$filename"") ;
		readfile("./files/$filename") ;
?>		
<script type="text/javascript">
<!--
window.location = "http://www.example.com/"
//-->
</script>
<?php
		die() ;
Maybe I'll try an onsumbit event back on page1.php? I've never used an onsubmit... don't know if that will allow the form to submit or not...

Code: Select all

document.location.href = 'page1.php';
Feyd's solutions sounds more practical, though.

Posted: Thu Aug 17, 2006 4:10 pm
by feyd
Passing Javascript in the HTML stream will only get mixed into the file itself if the browser chooses not to display it. Remember: one request, one stream.