我发现很多网站都描述了PRG,但没有简单的PHP代码示例.
这是我实施的内容:
form.php有一个动作:validate.php.validate.php永远不会被用户看到; 如果验证全部,$_GET并且如果有效将其写入数据库并生成确认页面的HTML /如果无效,则会生成错误页面的HTML,以解释错误.$_SESSION变量中然后validate.php调用header('Location: <as appropriate>);.submitted.php的invalid_input.php(如果用户读取URL)只包括echo $_SESSION['form_html'];.在我看来,这似乎可以防止页面重新加载和后退按钮问题.
我试图重新发明轮子了吗?
Han*_*nes 68
最简单的场景:
if ($_POST) {
// Execute code (such as database updates) here.
// Redirect to this page.
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
Run Code Online (Sandbox Code Playgroud)
使用REQUEST_URI.不要PHP_SELF像在大多数CMS系统和框架中PHP_SELF那样使用/index.php.
SW4*_*SW4 27
一段代码:
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();
}
Run Code Online (Sandbox Code Playgroud)
bco*_*sca 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
Run Code Online (Sandbox Code Playgroud)
常见用途是登录验证.这是用户提交登录表单时的流程.PHP应用程序通过$ _POST vars对用户进行身份验证.用户成功通过身份验证后,将303标头发送回浏览器.因此用户被重定向到新页面.
我想向您介绍一种经常在框架中更大规模、更详细地使用的方法。
我们有一个名为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; ?>
Run Code Online (Sandbox Code Playgroud)