I can set session variables manually in a php file.
I can't get session variables to set using input variables from an html form.
I'm confused because the same input variables work when I use them to update a mySQL database.
Here is the php file that successfully sets a session variable:
<?php
session_start();
$_SESSION['username'] = "brandonpetaccio";
echo $_SESSION['username'];
?>
I test the session variables by echoing them in a separate php file:
<?php
session_start();
echo $_SESSION['username']."<br>";
echo $_SESSION['name1']."<br>";
echo $_SESSION['name2'];
?>
Username was already set in the first file. I am trying to set name1 and name2 using an html form.
Here is the file with the form. It contains php and html:
<?php
// STARTS SESSION FOR THIS PAGE
session_start();
// CONNECTS TO DATABASE
include 'dragon-conn.php';
// // NAME INPUTS
$iName1 = mysqli_real_escape_string($dragonden, $_REQUEST['iName1']);
$iName2 = mysqli_real_escape_string($dragonden, $_REQUEST['iName2']);
// SET SESSION VARIABLES
$_SESSION['name1'] = $iName1;
$_SESSION['name2'] = $iName2;
?>
<html lang="en">
<form method = "post" class="mt-5">
<!--NAME-->
<h2>People</h2>
<label for="iName1" class="" style="font-weight:bold;">First Name</label>
<input id="iName1" name="iName1" type="text" class="spaceless form-control username m-1">
<label for="iName2" class="mt-1" style="font-weight:bold;">Last Name</label>
<input id="iName2" name="iName2" type="text" class="spaceless form-control username m-1">
<div class="form-check form-check-inline m-2">
<input class="form-check-input" type="checkbox" value="" id="customer">
<label class="form-check-label" for="customer">
Customer
</label>
</div>
<div class="form-check form-check-inline m-2">
<input class="form-check-input" type="checkbox" value="" id="employee">
<label class="form-check-label" for="employee">
Employee
</label>
</div>
<!--SUBMIT-->
<input type="submit" id="addme" class="btn btn-primary m-1" value="Add Me">
</form>
</html>
After submitting the form with input, I refresh the php file that echos the three session variables. The first session variable is still set, and echos. The last two session variables do not echo.
I have session_start();
at the top of each file. The same input variables work when updating the database. I don't understand why they don't work when trying to set session variables via the html form.
What am I missing?
My Answer:
't be lost when the user navigates away from the page or closes the browser. They will remain active until the session is destroyed or expires.
Rate this post
4 of 5 based on 1947 votesComments