force page refresh?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

force page refresh?

Post 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!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

Readfile sends output, no headers beyond that point possible.
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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().
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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...
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
fgomez
Forum Commoner
Posts: 61
Joined: Mon Sep 26, 2005 11:23 pm
Location: Washington, DC

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply