简单的重定向后 - 获取代码示例

Maw*_*awg 37 php

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


这是我实施的内容:

  1. form.php有一个动作:validate.php.
  2. validate.php永远不会被用户看到; 如果验证全部,$_GET并且如果有效将其写入数据库并生成确认页面的HTML /如果无效,则会生成错误页面的HTML,以解释错误.
  3. 无论生成哪个HTML都存储在$_SESSION变量中然后validate.php调用header('Location: <as appropriate>);.
  4. submitted.phpinvalid_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.

  • 经过测试,效果很好,也很简单.我将所有会话处理代码放在其中,并将所有页面输出放在它下面. (7认同)
  • 一行, `header( "Location: {$_SERVER['REQUEST_URI']}", true, 303 );` (3认同)
  • 要确保浏览器正确处理此问题,您应该包含标题("HTTP/1.1 303 See Other"); (2认同)
  • 正如 @xtempore 提到的,303 标头可以帮助浏览器更好地处理“后退”按钮。三行:`http_response_code( 303 ); header(“位置:{$_SERVER['REQUEST_URI']}”); 退出;` (2认同)

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)

  • 修改 - 我认为这个问题有点苛刻 (16认同)

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标头发送回浏览器.因此用户被重定向到新页面.


Pet*_*ter 7

我想向您介绍一种经常在框架中更大规模、更详细地使用的方法。

我们要做什么

我们有一个名为index.php.

  • 我们要提交一个表格
  • 我们将检查此提交
  • 我们将 POST 数据添加到会话中
  • 我们会将用户重定向到确认页面
  • 我们将显示数据并让用户确认。
  • 我们将提交,并最终处理数据。
  • 我们将重定向回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)