Last active
September 25, 2016 00:01
-
-
Save abeyer/40b68d2263fec7871cb53ca4fa6c8258 to your computer and use it in GitHub Desktop.
php $_POST and $_SESSION
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
Fourth revision fixes $_POST typo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can see the changes from your original code (I just pasted that in verbatim in the first revision) in the revision history.
Second revision cleans up some html problems in the form: removed the extra form, and an unmatched , indented, etc...
Third revision changes the post/session related stuff on the form: We make sure the action of the form points to the formHandler.php, and don't bother with dealing with the session at all in the form, as it isn't needed there.