Skip to content

Instantly share code, notes, and snippets.

@shamess
Created January 6, 2012 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamess/1568621 to your computer and use it in GitHub Desktop.
Save shamess/1568621 to your computer and use it in GitHub Desktop.
An interpretation of the PRG pattern, using PHP.
<?php
session_start ();
if ($_POST['some_text']) {
$_SESSION['some_text'] = $_POST['some_text'];
header ('HTTP/1.1 303 See Other');
header ('Location: ./');
} else {
$some_text = "<p>No text set yet.</p>\n";
}
if ($_SESSION['some_text']) $some_text = "<p>'some_text' has been set to '".$_SESSION['some_text']."'.</p>\n";
?>
<!DOCTYPE html>
<html>
<head>
<title>PRG in PHP</title>
</head>
<body>
<p>See <a href="http://blog.shamess.info/2012/01/06/postrequestget-pattern-in-php/">my blog</a> for more information.</p>
<?php echo $some_text; ?>
<form method="POST" action="">
<p>This is a form, that will submit data back to this page using POST.</p>
<p>Enter a value for $_POST['some_text']: <input type="text" name="some_text" /></p>
<p><input type="submit" /></p>
</form>
</body>
</html>
@sannidhyashukla
Copy link

What does this line

header ('Location: ./');

do? And how do we fetch the actual posted data?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment