Document Expired

I have problem in my page, everytime I click the back button in browser I get “Document Expired” Then I need to refresh first before the page form will display or some content.

Thank you in advance.

Your are probably using POST in your form to add/update data on the server. You should redirect user after the data has been processed.

<?php
// Process/validate form data, update db or whatever you are doing with the data

// Redirect user
header('Location: some_page.php');
?>

[code]<?php

// …

// Redirect user
header(‘Location: some_page.php’);
// !!! and also add
exit; // do not miss this line!
[/code]

1 Like

exiting doesn’t matter if you do no further output/processing; if your page ends immediately after the header row, there is no benefit to exit/die.

2 Likes

Both true, forgot that. Also can use die() which is equivalent of exit().

True, and I think you are referring to the last ?> but, that does not mean the file ended.
I presented the exit; as a good practice, not only for this case.

function redirect($url) {
    header('Location: ' . $url);
    exit;
}
1 Like

I don’t know if it matters, but it really sounds like when your script is done, you aren’t outputting any HTML stuff. I would agree with @StarLion on this.

If you have a form being submitted between an else if statement, upon submission succession, you also would need to display the same content or different content in order for you to not get that notice.

Let’s say I have

if($_SERVER['REQUEST_METHOD'] == "POST") {
      if($_POST['input_field'] == "") {
             print("Input field has no value");
      }
} else {
      print("HTML FORM");
}

Well, were the curly brace ends before print(“Input field has no value”);, you need to have an else condition and have your original or new HTML content there. So complete example would be.

if($_SERVER['REQUEST_METHOD'] == "POST") {
      if($_POST['input_field'] == "") {
             print("Input field has no value");
      } else {
             print("HTML FORM");
      }
} else {
      print("HTML FORM");
}

This is if you want to stay on the same page, but display different types of errors and what not. It also isn’t really wise to redirect the user to a different page if the form fails to submit. You should set a session cookie and redirect the user back to the original page. Then check to see if that session cookie exists, if it does, create your own custom error handling or if the submission was successful, do the same except tell the user they have successfully submitted the form.

Well my first reply was just pseudo code. And I would not be printing HTML forms in the if-elses anyway. Here is a more complete example of what I meant (not complete and it could be improved in a many ways).

<?php
$errors = array();
if($_SERVER['REQUEST_METHOD'] == "POST") {
	if($_POST['input_field1'] == "") {
		$errors[] = "Field1 has no value";
	}
	if($_POST['input_field2'] == "") {
		$errors[] = "Field2 has no value";
	}	
	// Validate other possible values
	
	if (!count($errors)) {
		// If all good process data and redirect
		header('Location: some_other_page.php');
		exit();
	}
}
?>
<html>
<head><title></title></head>
<body>

<!-- Show errors -->
<?php
if (count($errors))
	foreach ($errors as $error) { echo $error .'<br/>'; }
?>

<form method="POST" action="some_page.php">
Field1: <input type="text" name="input_field1"><br/>
Field2: <input type="text" name="input_field2"><br/>
<input type="submit" value="Send">
</form>

</body>
</html>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.