第一个例子是使用:
HTTP GET
请求加载页面。HTTP POST
请求提交表格。- 表单的数据存储在会话中。
HTTP GET
请求加载页面。
第二个示例是使用goto
“重排”缓冲区,避免额外的 HTTP 请求。
HTTP GET
请求加载页面。HTTPPOST
请求提交表格。- 缓冲区被刷新并显示内容。
此外,最后一个示例不使用会话。
301
<!DOCTYPE html>
<html>
<head>
<!-- ow noez! -->
</head>
<body>
<?php
// A very common scenario in user-flow handling is redirecting user to the page itself after submitting form, e.g.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['a'])) {
// Suppose there was an error & I've populated the error in $_SESSION.
// Now I would redirect user back to the form. This is because there
// is markup in the upper template hierarchy layer, e.g. "<!-- ow noez! -->"
header('Location: ' . $_SERVER['REQUEST_URI']);
exit;
}
}
?>
<form action="" method="post">
<?php if (isset($time)):?>
<pre>This value is from the past: <?=$time?></pre>
<?php endif;?>
<pre>Next time: <?php $time = time(); echo $time;?></pre>
<input type="submit" name="a" value="back in time!">
</form>
</body>
</html>
去
<?php
goback:
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<!-- ow noez! -->
</head>
<body>
<form action="" method="post">
<?php if (isset($time)):?>
<pre>This value is from the past: <?=$time?></pre>
<?php endif;?>
<pre>Next time: <?php $time = time(); echo $time;?></pre>
<input type="submit" name="a" value="back in time!">
</form>
<?php
// A very common scenario in user-flow handling is redirecting user to the page itself after submitting form, e.g.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['a'])) {
ob_clean();
$_POST = [];
$_SERVER['REQUEST_METHOD'] = 'GET';
goto goback;
}
}
?>
</body>
</html>
goto
场景不是优于301吗?