Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The main idea behind POST/REDIRECT/GET, as the article you linked to points out, is to avoid users resubmitting data (most of the time). Generally, you don't want the same POST (with the exact same data) to happen twice -- indeed, in some situations, it could end up performing some action (like charging a credit card) a second time, which would be bad.</p> <p>Most of what you ask about in your question are implementation details (like sending the ?success request parameter in the redirect).</p> <p>In practice, what usually happens is that your redirect <strong>on success</strong>. If, for example, the user's input fails validation, you <em>don't redirect</em>, and instead, redisplay the form, along with relevant error messages.</p> <p>Here's a basic example, all in one script. I've tried to include only what's important, with as little extraneous stuff as possible.</p> <p><strong>login.php</strong></p> <pre><code>&lt;?php /** * ensure user supplied both username &amp; password * @return mixed true or an array of error messages */ function validate_login_values($vars){ $errors = array(); if (empty($vars['username'])) $errors[] = 'You must supply a username, genius.'; if (empty($vars['password'])) $errors[] = 'You must supply a password, dummy.'; if (empty($errors)) return true; return $errors; // $errors must be an array. } if (! empty($_POST)){ $validationResults = validate_login_values($_POST); if ($validationResults === true){ // assume here that authenticate properly escapes it's arguments before sending them // to the database. if (authenticate($_POST['username'],$_POST['password'])){ //GREAT SUCCESS! The user is now logged in. Redirect to home page header("Location: /"); die(); } $errors[] = 'Invalid username/password. Try again, slim"; }else{ $errors = $validationResults; // validate_login_values created errors. } } ?&gt; &lt;h1&gt;Log In, Friend!&lt;/h1&gt;] &lt;?php //display errors, if there were any if (! empty($errors)): ?&gt; &lt;div class="errors"&gt;Something went horribly wrong: &lt;ul&gt;&lt;?php foreach($errors as $e) echo "&lt;li&gt;$e&lt;/li&gt;"; ?&gt;&lt;/ul&gt; &lt;div&gt; &lt;?php endif; ?&gt; &lt;form method="POST"&gt; &lt;!-- //username, password, and submit --&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload