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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.