Jump to content

$_SESSION variables losing their value


Epic720

Recommended Posts

I have been searching and searching online for a solution to my problem here. I have so bar been unsuccessful. I searched on here before deciding to post this. So I sincerely apologize if this problem has previously been addressed.

 

I have an array that I am storing into $_SESSION that I create from an include file that is included in each php file. This array is to store a number of the EducationInformation class.

 

I am using header calls to get from php file to php file. And for some reason, when returning from one page to another the class I am losing class data stored in the array.

 

Here are some code snippets:

 

//Save all frequently used session variables back before leaving page
        $_SESSION['educationCurrentInfo'] = $educationCurrentInfo;
        $_SESSION['educationInformation'][$educationCurrentInfo] = $educationInfo;
        $_SESSION['educationInformation'][$educationCurrentInfo]->address = $addressInfo;
        
        //Make sure that the session variables are getting saved before leaving page
        session_write_close();
        
        //Navigate back to Education.php
        header('location:Education.php', FALSE);
        exit(0);

 

And when creating a new:

 

//Add one to the education count
        ++$educationCount;
        $educationCurrentInfo = ($educationCount - 1);
        $_educationInformations[$educationCurrentInfo] = new EducationInformation();

 

I have done var_dump() s all over the place to find out exactly where the data is being lost at. And it seems to be happening after one record is set just fine and you try to add a second. When the second record is added, all of the simple variables in the class get wiped. Yet, the class Address within the record stays in tact. Very weird and hard for me to track down.

 

Does anyone have any suggestions as to how to resolve this? I really need to get over this hump!

Thanks in advance~!!!

Link to comment
Share on other sites

Epic720,

 

    I use sessions all of the time and have never used session_write_close();  I did not even know what the function was, so I looked it up on php.net...

 

" session_write_close — Write session data and end session " is the first line in the manual... It is probably the "end session" part of the function that is killing you.  Just a thought.

 

Scot L. Diddle, Richmond VA.

   

Link to comment
Share on other sites

In the include file that every php file includes I have a session_start(); The problem isn't losing the array altogether it is losing some of the data stored in it. More specifically all of the simple data types, not the class within the class.

 

I have been having this problem for some time. Then I discovered session_write_close(); this solved the problem at first, when I was only adding one record to the array. Because before I was losing the data from one php file to another. Now it happens when I add another record. The newly created record gets recorded properly, but the previous record then loses it's simple data. And if I add a third record, the same thing happens to the second. And so on.

 

This is the class I am referring to:

 

class EducationInformation
{
private $schoolname = "";
private $address;
private $graduated = "";
private $degree = "";
private $yearsAttended = 0;

 

$address is a class that stores the address strings. In the constructor of this class is where I instantiate it. Everything but $address gets wiped when the new record is added.

Link to comment
Share on other sites

Epic720,

 

    I was able to add three schools under Education... Then the Add new school button showed as disabled. ( By Design ? )

 

  I will be out for the next 3 days, but will look again this Friday ..

 

Scot .

Link to comment
Share on other sites

OK, I think this is dumb but at least I am no longer losing my variables. Maybe for some reason the session variables are not getting set before the end of each page.

 

I added the session_write_close() at the bottom of every php file and now I am retaining my variables.

 

I still don't know why I have to do this, and no one else does. Maybe someone can explain to me what i'm doing differently.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.