Jump to content

[SOLVED] Browser back after Redirect


Plaid

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/100383-solved-browser-back-after-redirect/
Share on other sites

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

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.

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.