Php Session Variable Is Not Saving
Solution 1:
make a call to the session_start() function.
session_start()
This call should be in every script that needs to utilise the session data. For example, if you have a script that creates a CAPTCHA image and needs to store the secret word for the session, you will need to put session_start() at the beginning of the script. If you have another script that takes the user input for the form and checks the secret word entered by the user against what you stored earlier, you will also need to put session_start() in that script.
The function session_start() takes no parameters. It always returns TRUE, so you don't have to bother to check its return value. since session_start() sets a cookie via the HTTP cookie header, you must call it before you output anything from your script. It's best to simply call it at the beginning of your script.
Ending Sessions
session_destroy() is used to destroy the data associated with a session. However, this in itself does not clean up everything. For example, the session cookie is not unset. The $_SESSION array is also still available until your script ends.
To remove the cookie, manually delete it using the usual method one uses to delete a cookie in PHP. To get the name of the cookie to delete, call the session_name() function, which returns a string that is also the name of the cookie set by the PHP session handler.
*example code for how you can clean up after a session can be found in the official PHP manual.
Solution 2:
Not sure what your 3rd page looks like but mine would be:
session_start(); // Use session variable on this page. This function must put on the top of page.if( isset($_SESSION['i_am_in']) ) echo"logged in";
My Page 2:
if ($login_ok) {
session_start(); // Create session & settings
$_SESSION['abc123'] = 'whatever';
header('Location: page3.php');
}
Solution 3:
Page 1 is good, Page 2 and Page 3 should look something like this
PAGE 2
//initialize the session and set are $_SESSION variable from the form data
session_start();
$_SESSION['searchTerm'] = $_POST['find'];
//set where condition and check if $_POST['find'] has a value$whereclause = "";
if(isset($_POST['find'])) {
$whereclause = "WHERE mytable.lastName = '".$_POST['find']."'";
}
/*execute your statement note: data passed such as $_GET, $_POST, $_SESSION should never
be directly in your statement, use PDO to prepare and execute your statements to prevent
SQL injection*///then if you wanted to be redirected to page 3 use header after your statement has executed
header('Location:comfirmPage3.php');
PAGE 3
//initialize the session so we can access our $_SESSION variables
session_start();
//set where condition and check is $_SESSION['searchTerm'] has a value
$whereclause = "";
if(isset($_SESSION['searchTerm'])) {
$whereclause = "WHERE myTable.lastName = '".$_SESSION['searchTerm'] ."'";
}
//execute your statement
Solution 4:
replace
if (!isset($_SESSION)) {
session_start();
}
with
session_start();
because $_SESSION
is super global and its always available.
OR
Try checking for a specific $_SESSION
ex. $_SESSION['session']
rather than just a $_SESSION
.
Solution 5:
if(session_id() == ''){
session_start();
}
since $_SESSION is always available.
Post a Comment for "Php Session Variable Is Not Saving"