puddleJumper puddleJumper - 3 months ago 9
PHP Question

PHP Session variable is not saving

I've looked through all the problems about the session variable not saving and don't see my problem so I'm going to ask it.

I have a form that once submitted it searches my database for that name. The initial form is on page 1.
On page 2 I take the the variable from page 1 and save it like this

$searchTerm = $_POST['find'];


which is used as the search for the database and it works perfectly. Under that I have my sql statement then I have placed this

//initialize the session
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['searchTerm'] = $searchTerm;


I have then tested the $_SESSION['searchTerm'] on page 2 to make sure that it is saving properly, and it does. My issue comes in when I try to go to a 3rd page that is a confirm page for the form from page 2. I have the session start clause at the top of the 3rd page and I even inserted

ini_set('display_errors',1);
error_reporting(E_ALL);


to check for errors, no errors were displayed. So my next step was to test the session itself to see if it was refreshing the session id. I found a script on this site www.webassist.com/forums/posts.php?id=5735 to test whether the server could possibly be causing the issue. The script works fine no problems, showing that the server is not the problem. However when I upload my 2nd and 3rd page to the server and put in

<?php echo session_id(); ?>


the numbers from page 2 and page 3 are completely different therefore making my variable null. I did further research and thought it might be because there was a session_destroy or session_unset but I didn't put one of these on either page. When I tried this on my local machine I got the same session id but still the session variable I set was blank.

Does anyone have any other ideas on how or why this would happen?

**********edit *********************

page 1 has this form

<form name="search" method="post" action="page2.php">
<div>
<!-- Search-->
<label>Search by Last Name:</label>
<div>
<table width="100%">
<tr>
<td><input type="text" id="" name="find" placeholder=""></td>
<td><button id="submit" type="submit"><span>Search</span></button></td>
</tr>
</table>
</div>
</div>
</form>


page 2

$searchTerm = $_POST['find'];


if(!empty($searchTerm ) && ctype_alpha($searchTerm ))
{
$whereclause = "WHERE mytable.lastName = '".$searchTerm ."'";
}
else {
$whereclause = "";
}

// sql query is here this one searches it is long and just used the $whereclause above.

// second sql query that is only submitted if button from the form below is hit is here.

$insertGoTo = "confirmPage3.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'] ;

}
header(sprintf("Location: %s", $insertGoTo));

//initialize the session
if (session_id() == '') {
session_start();
}
$_SESSION['searchTerm'] = $searchTerm;


// then the form is listed below that with the rest of the html


page 3

<?php if(session_id() == '') session_start(); ?>

if(!empty($_SESSION['searchTerm']) && ctype_alpha($_SESSION['searchTerm']))
{
$whereclause = "WHERE myTable.lastName = '".$_SESSION['searchTerm'] ."'";
}
else {
$whereclause = "";
}

// More sql statement here for this one it is only selecting not updating or changing anything.

// Table below this that is used to hold the items retrieved from the database.


***********edit 2 and my fix**********

so after some back and for with a few of you, Aaron Gong suggested I start with a blank page and go from there. I did this and have finally diagnosed the issue. It was something that I hadn't thought of that I very well should have. I don't like using dreamweaver and now I remember why. When I originally created the page 2 I used dreamweavers insert code to insert my sql statement to do the updating. Well it placed in the code these lines of code.

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}


This will completely mess with you if you are trying to send the user to another page to confirm their entries. I commented it out and set the $editFormAction to my page and used that in the header string and viola that immediately fixed my issue. What I hadn't realized is when it was refreshing the page that it was emptying my $_POST variable and found that error finally through the empty pages suggest by Aaron.

Hope this helps someone else and I will never trust the code from dreamweaver again. I learned to code by hand and should know better but I was in a hurry and thought oh it won't harm it.

Thanks again to all your suggestions.

Answer

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');
}
Comments