简单的PHP Post-Redirect-Get代码示例 [英] Simple PHP Post-Redirect-Get code example

查看:134
本文介绍了简单的PHP Post-Redirect-Get代码示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了许多描述PRG的网站,但没有简单的PHP代码示例.

I have found many sites that describes PRG, but no simple PHP code example.

这是我实现的:

  1. form.php具有操作:validate.php.
  2. validate.php永远不会被用户看到; if验证所有$_GET,如果有效,则将其写入数据库并生成确认页面的HTML;如果无效,则生成错误页面的HTML,以解释错误所在.
  3. 生成的所有HTML都存储在$_SESSION变量中,然后validate.php调用header('Location: <as appropriate>);.
  4. invalid_input.phpsubmitted.php(以防用户读取URL)仅由echo $_SESSION['form_html'];组成.
  1. The form.php has an action: validate.php.
  2. The validate.php is never seen by the user; if validates all $_GET and, if valid writes it to database and generates the HTML of a confirmation page / if not valid, it generates the HTML of an error page explaining what is wrong.
  3. Whichever HTML is generated get stored in a $_SESSION variable and then validate.php calls header('Location: <as appropriate>);.
  4. The submitted.php of invalid_input.php (in case the user reads the URL) consists only of echo $_SESSION['form_html'];.

在我看来,这就像针对页面重新加载和后退按钮问题的防护措施.

That seems to me like protection against both page reload and back button problems.

我是不是想通过重新发明轮子来愚蠢?

Did I goof by trying to reinvent the wheel?

推荐答案

最简单的情况:

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.

Use REQUEST_URI. Do not use PHP_SELF as in most CMS systems and frameworks PHP_SELF would refer to /index.php.

这篇关于简单的PHP Post-Redirect-Get代码示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆