Plaid Posted April 9, 2008 Share Posted April 9, 2008 Hi, I have got a problem with a self validating form that checks to see if a file exists. If so the user is redirected to view the file. The problem is that the browser back wont work and performs the redirect again( it will work if you press the back button twice, but most users don't get this). Any ideas how I can get this to work ? <?php $exists = true; if ( isset( $_GET['partNum'] ) ){ $partNum = $_GET['partNum']; unset($_GET['partNum']); $reportPath = '/home/httpd/htdocs/xray/reports/'; $fileExt = '_report.xls'; $file = $reportPath.$partNum.$fileExt; if (file_exists($file) == true ){ redirect('http://xray/reports/'.$partNum.$fileExt); $exists = true; } else { $exists = false; } } function redirect($url){ echo " <script language=\"javascript\"> <!-- location.replace(\"$url\"); --> </script>"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Plating</title> </head> <body> <form action="<?=$_SERVER['PHP_SELF']?>" method='GET'> <h2>View Reports</h2> Enter the Part Number: <input type='text' name='partNum' /> <br/><br/> <?php if ($exists == false){ echo('Report for this part does not exist - '.$partNum.'<br/>'); } ?> <br/> <input type='submit' value='Submit' /> </form> </body> </html> Thanks in advance P Quote Link to comment Share on other sites More sharing options...
p2grace Posted April 9, 2008 Share Posted April 9, 2008 Save the current page to a session var and then create a back button that redirects to the session var instead of making them hit the back button in the browser. Quote Link to comment Share on other sites More sharing options...
Plaid Posted April 10, 2008 Author Share Posted April 10, 2008 I can't stop the users using the back button on the browser and I can't add a custom back button to a static page e.g. .xls or .html ( or I dont know how too) Is there any other aproach? I would have thought this must be a common requirement, can anyone point me in the direction of some code? Thanks for the reply anyway Quote Link to comment Share on other sites More sharing options...
poleposters Posted April 10, 2008 Share Posted April 10, 2008 I think the problem could be that when you hit the back button the page recognises the that the file exists and reruns the script. I could be wrong but you could rewrite the script to check whether submit has been posted. Quote Link to comment Share on other sites More sharing options...
Plaid Posted April 10, 2008 Author Share Posted April 10, 2008 No joy, I still get the same thing when i use if (isset($_GET['Submit'])) { ??? Quote Link to comment Share on other sites More sharing options...
haku Posted April 10, 2008 Share Posted April 10, 2008 Try changing this: if (file_exists($file) == true ){ redirect('http://xray/reports/'.$partNum.$fileExt); $exists = true; } else { $exists = false; } } function redirect($url){ echo " <script language=\"javascript\"> <!-- location.replace(\"$url\"); --> </script>"; } to this: if (file_exists($file) == true ){ header ('Location: http://xray/reports/'); $exists = true; } else { $exists = false; } } No guarantees. But you shouldn't need a javascript redirect for this. Quote Link to comment Share on other sites More sharing options...
Plaid Posted April 10, 2008 Author Share Posted April 10, 2008 Many thanks it now works... I'm sure i had tried this before but must have done something wrong! Quote Link to comment Share on other sites More sharing options...
haku Posted April 10, 2008 Share Posted April 10, 2008 Thats good. I typed that up REALLY quick right before I left work, so I didn't have time to look too closely at it. The reason you had to do a double back click with the javascript solution is because the page has to load a little before the javascript re-direct kicks in. This means that you have to click back one time to get to the redirect, then again to get to the page before the redirect. But if you use a php redirect, the redirect happens without any of the page loading the second time, which means that the page you go back to is the one before the redirect. This method is also better because it doesn't require the user to have javascript enabled for it to work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.