Skip to content

Instantly share code, notes, and snippets.

@abeyer
Last active September 25, 2016 00:01
Show Gist options
  • Save abeyer/40b68d2263fec7871cb53ca4fa6c8258 to your computer and use it in GitHub Desktop.
Save abeyer/40b68d2263fec7871cb53ca4fa6c8258 to your computer and use it in GitHub Desktop.
php $_POST and $_SESSION
<form class="form-horizontal" role="form" method="post" action="formHandler.php">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="Your Name" value="">
</div>
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="submit" class="btn btn-primary">
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- getCurrentURL is called from custom-inc.php -->
<input type="hidden" name="urlPointer" value="<?=getThisPage(); ?>">
</div>
</div>
</form>
<?php
#call session start...
if(!isset($_SESSION)) {
session_start();
$sessionActive = '<p>Session was STARTED</p>';
}else{
$sessionActive = '<p>Session was PRESENT</p>';
}
// If we have a session initialized and got new data posted, then save it to the session
if (isset($_SESSION) && isset($_POST["name"])) {
$_SESSION["name"] = $_POST["name"];
}
echo '<br /<br />var_dump session:';
var_dump($_SESSION);
$name = $_SESSION['name'];
echo "Name in session is: " . $name . "<br /><br />";
echo '<br /<br />var_dump post:';
var_dump($_POST);
$name2 = $_POST['name'];
echo "Name in post is: " . $name2 . "<br /><br />";
echo $sessionActive;
@abeyer
Copy link
Author

abeyer commented Sep 25, 2016

Fourth revision fixes $_POST typo

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