我找到了许多描述PRG的网站,但没有简单的PHP代码示例。
这是我实施的内容:
form.php
有一项操作:validate.php
。validate.php
;如果验证所有$_GET
,并且如果有效将其写入数据库并生成确认页面的HTML /如果无效,则会生成错误页面的HTML,以解释错误。 $_SESSION
变量中,然后validate.php
调用header('Location: <as appropriate>);
。submitted.php
invalid_input.php
(如果用户阅读网址)仅包含echo $_SESSION['form_html'];
。在我看来,这样可以防止页面重新加载和后退按钮问题。
我试图重新发明轮子了吗?
答案 0 :(得分:67)
最简单的方案:
if ($_POST) {
// Execute code (such as database updates) here.
// Redirect to this page.
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
使用REQUEST_URI
。不要像大多数CMS系统那样使用PHP_SELF
,而PHP_SELF
框架会引用/index.php
。
答案 1 :(得分:25)
一段代码:
if (count($_POST)) {
// process the POST data
// your code here- so for example to log a user in, register a new account..
// ...make a payment...etc
// redirect to the same page without the POST data, including any GET info you
// want, you could add a clause to detect whether processing the post data has
// been successful or not, depending on your needs
$get_info = "?status=success";
// if not using rewrite
// header("Location: ".$_SERVER['PHP_SELF'].$get_info);
// if using apache rewrite
header("Location: ".$_SERVER['REQUEST_URI'].$get_info);
exit();
}
答案 2 :(得分:11)
Browser
HTML form
method=POST
|
v
PHP app
reads $_POST
sends 303 header
|
v
Browser
receives header
redirected to
new page
|
v
PHP app
reads $_GET
does whatever
常见用途是登录验证。这是用户提交登录表单时的流程。 PHP应用程序通过$ _POST vars对用户进行身份验证。用户成功通过身份验证后,将303标头发送回浏览器。因此用户被重定向到新页面。
答案 3 :(得分:3)
<强> Caller.htm 强>
<form method="post" action="Callee.php?Query1">
<input type="text" name="PostData" />
<input type="submit" value="Go" />
</form>
Callee.php (被叫两次。)
if ($_POST) {
header("Location: ". $_SERVER['REQUEST_URI']. 'Query2');
// PART1: Use $_POST and $_GET to execute database updates here...
// Now any display (i.e. echo or print) will come after the header.
// ...
die; // When done, GET 'myself' to execute PART2 below.
}
// PART2: Results page goes here...
echo 'PART 2 display output: '; var_dump($_GET);
请注意,涉及两个查询字符串
看看var_dump关于$ _GET的说法:
PART 2 display output: array(1) { ["Query1Query2"]=> string(0) "" }
将POST 的标题放在POST部分的问题如下:
header("Location: ". $_SERVER['REQUEST_URI']. 'Query2'); die;
php手册说:“请记住,在发送任何实际输出之前,必须通过普通的HTML标记,文件中的空行或PHP来调用header()。读取代码是一个非常常见的错误。包含或要求函数或其他文件访问函数,并在调用header()之前输出空格或空行。使用单个PHP / HTML文件时存在同样的问题。“
但是,如果您需要根据POST部分中的内容构建“Query2”,则可能需要位于POST部分的底部。这没关系,只要你不尝试在它上面插入任何回声,甚至不用于测试。
答案 4 :(得分:3)
我想向您介绍一种常用于更大规模且在框架中更详细的方法。
我们有一个名为index.php
的文件。
index.php
并显示通知。<?php
if (!isset($_SESSION)) session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
switch ($_POST['submit']) {
case 'add':
// This is where our first POST will end up
// We can perform actions such as checking the data here
// After that we will add the POST data to a session
$_SESSION['postdata'] = $_POST;
// and unset the $_POST afterwards, to prevent refreshes from resubmitting.
unset($_POST);
// Now we will redirect...
header("Location: ".$_SERVER['PHP_SELF']);
break;
case 'confirm':
// We can now insert the data into the database or email it
// Then we will unset the session and redirect back
unset($_SESSION['postdata']);
// This is to display our notification
$_SESSION['success'] = true;
// And there we go again...
header("Location: ".$_SERVER['PHP_SELF']);
break;
}
// We will exit here because we don't want the script to execute any further.
exit;
}
?>
<?php if (isset($_SESSION['success']) && $_SESSION['success'] == true): ?>
<p>Our data has been processed succesfully</p>
<?php unset($_SESSION['success']); ?>
<?php endif; ?>
<?php if (isset($_SESSION['postdata'])): ?>
<p>
You want to add the following data:<br />
<pre><?php print_r($_SESSION['postdata']); ?></pre>
Is this correct?<br />
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
<button type="submit" name="submit" value="confirm">Yes</button>
</form>
</p>
<?php else: ?>
<p>
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
<input type="text" name="..."><br />
<input type="text" name="..."><br />
<input type="text" name="..."><br />
<input type="text" name="..."><br />
<button type="submit" name="submit" value="add">Add something</button>
</form>
</p>
<?php endif; ?>
答案 5 :(得分:1)
以下是form.php
<?php
session_start();
// 1) _____________________________________________ POST _____________________________
if ( count($_POST) ) {
$ermsg ='';
…
check data, write some data to database(s), set error message(s) if any
…
$userdata1 = $_POST['htUserdata1'];
$userdata2 = $_POST['htUserdata2'];
…
$_SESSION['PRG'] = array('field1'=>$userdata1,'field2'=>$userdata1,…,'ermsg'=>$ermsg);
session_write_close();
header('Location: ' . $_SERVER['REQUEST_URI'].'?z',true,303);
exit;
// 2) _____________________________________________ REDIRECT ________________________
} else if ( array_key_exists('PRG',$_SESSION) ) {
$userdata1 = $_SESSION['PRG']['field1'];
$userdata2 = $_SESSION['PRG']['field2'];
…
$ermsg = $_SESSION['PRG']['ermsg'];
unset($_SESSION['PRG']);
// 3) _____________________________________________ GET ______________________________
} else {
…
retrieve data from database(s)
…
$userdata1 = dbGet1();
$userdata2 = dbGet2();
…
$ermsg = '';
}
// 4) _____________________________________________ DISPLAY _________________________
?>
<!DOCTYPE html>
<html lang="fr">
…
<form method="post" action="form.php" accept-charset="utf-8">
<input id="htUserdata1" name="htUserdata1" type="text"/>
<input id="htUserdata2" name="htUserdata2" type="text"/>
…
</form>
<script language="javascript">
"use strict";
<?php
$G['htUserdata1'] = $userdata1;
$G['htUserdata2'] = $userdata2;
…
$G['ermsg'] = $ermsg;
$myJSON = json_encode($G);
echo "var G=$myJSON;";
?>
document.getElementById('htUserdata1').value = G.htUserdata1;
document.getElementById('htUserdata2').value = G.htUserdata2;
…
if ( G.ermsg !=='') alert(G.ermsg);
</script></body></html>