Problem with my implementation of the Post/Redirect/Get pattern

亡梦爱人 提交于 2019-12-23 04:52:41

问题


I always use the Post/Redirect/Get method on my forms. My forms generally always submit to themselves. However, when I have an error in the form, I don't send any new headers. This is so I can easily do stuff like this

<input type="text" name="email" value="<?php echo $this->input->post('email', '') ?>" />

The PHP is just a library function that handles 2 arguments, the $_POST key and a default value if it isn't present.

This means if someone makes an error in the form, they don't have to refill out the form a second time. The disadvantage is that reloading the page gives them the POST warning in their browser.

Is there anyway to avoid this, without using something for state (i.e. cookies, session, database etc)


回答1:


I find the best way to do this is use the header function. You can post to what ever file you need even itself do the validation then use the header redirect to go back to the form if it failed. Store the post'd values in the session or another accessible variable, therefore you can access the previously entered data.

When using header("location: myscript.php"); make sure to include an exit(); afterwards otherwise you will still get the POST warning on a refresh.

myscript.php

if($_POST['submit'])
{
    //check for errrors

    if ($error)
    {
        $_SESSION['myPostVars'] = $_POST;
        header("location: myscript.php");
        exit();
    }
}

<form>
    // your form code
</form>

Edit: I just noticed that you edited your question to avoid using sessions.

You could serialize the post vars you want to return and put them in the query string (sent via the header()




回答2:


There is no way to avoid this without saving state in the session. When you encounter an error you could probably do something like the following:

  1. generate a unique id
  2. copy the $_POST variable into the session keyed by the unique id above
  3. redirect as you do now, but pass the unique id as part of the query string
  4. update your library to look for this unique id in the session (error situations) instead of accessing the $_POST variable directly, if the unique id wasn't passed on the request or the name you are looking for doesn't exist use the default value
  5. at the end of the request remove the unique id entry from the session. this saves polluting the session with too much garbage. You could also do this at the beginning of the request depending on your library



回答3:


I think you mean something like this:

function Value($array, $key, $default = false)
{
    if (is_array($array) === true)
    {
        settype($key, 'array');

        foreach ($key as $value)
        {
            if (array_key_exists($value, $array) === false)
            {
                return $default;
            }

            $array = $array[$value];
        }

        return htmlspecialchars($array);
    }

    return $default;
}

<input type="text" name="email" value="<?= Value($_POST, 'email', ''); ?>" />

You may also want to read this tutorial.




回答4:


You're basically doing it right. There's no particularly good way avoid the "repost" warnings.



来源:https://stackoverflow.com/questions/1678763/problem-with-my-implementation-of-the-post-redirect-get-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!