Welcome Guest, Not a member yet? Register   Sign In
Storing data in session - is this always the solution?
#1

[eluser]SPeed_FANat1c[/eluser]
Hello,

last few days was debugging one, seemed like simple thing, but could not find a bug.

What was happening - I stored a variable in session to keep count of how many times alert was thrown. This was with ajax requests.

In our project there was a function that processes xmls and writes/updates the data from them to database.

And then I test, it works fine when I do not execute the function which proceses xmls, but I manually update the data in database, the counter in session is working well.

But when I run the procesisng xmls function, and use the same counter to count how many alerts there were - it somewhere used to set to zero. Ok, I then made logging, in those places where it sets to zero, it was to of them.
Then run xml processing againg and test the alerts. And see that counter was 0, but loging function didn't log any of those two lines where it should be set to 0.

Then later (it was end of the day, so not made too much testing yet) I made storing counter in another database table. And then it was not set to zero, counter started to work. This was the same logic, but just changed the way of storing counter data.

The session is stored also in database, so whats the difference I thought.

So how could that happen? Can't we trust sessions and should avoid them? I am not talking about security there, but just simple data saving. Or it still was some bug in my code and it accidentally started to work when I changed the place to save the variable?
#2

[eluser]InsiteFX[/eluser]
Sounds like it might be a coding problem in your code some place.

Could try defining the counter as a static variable and see if that helps.
#3

[eluser]SPeed_FANat1c[/eluser]
but static it cannot be set, because I need to store it, I mean function runs, ends and thats it, no information stored. Once function runs again, it needs to get current value from somewhere.

My collegue also said that he has had problem with sessions.

But I will test more how I have made (storing in database table) and we'll see. If that will work then it will be really strange.

#4

[eluser]SPeed_FANat1c[/eluser]
ok, I still think that this is session problem, I don't know at least what could I change, despite storing in another table.

I was debugging another thing with session, but similar to last one.

There is a function called response_center(). It runs constantly, javascript executes it automatically when it finishes running. Here is what it looks like:



Code:
function response_center() {
   //some code

   if (something) {
      $this->session->set_userdata('all_matches_hash', $hash_to_return);
   }

   //some other code
}

BTW this function also was running periodically with previous example.

Now there is another couple of functions:

function delete() {}
function submit() {}

User panel is something like this:

Buttons

I have MY_Session - extended session and this function modified so I could log data:

Code:
function set_userdata($newdata = array(), $newval = '')
    {
        if (is_string($newdata))
        {
            $newdata = array($newdata => $newval);
        }

        if (count($newdata) > 0)
        {
            
            foreach ($newdata as $key => $val)
            {
                $this->userdata[$key] = $val;
            }
        }

        $this->CI->utils->logging('DEBUG', 'set_userdata - userdata_array is now: ' . serialize($this->userdata));
        //$this->CI->utils->logging('DEBUG', 'set_userdata debug_backtrace: ' . serialize(debug_backtrace()));  //not enough space in field probably
        
        $this->sess_write();
    }


The rules are such:

as you can see in the image there are two delete buttons and one submit (confirm) button. Delete buttons delete object from session. Submit button lets say prints ticket.
There can be only one object in the ticket when submiting, otherwise the error has to be thrown. (I could throw error and not allow to add another object but thats not the case, they said I have to allow to add).



And here are the results of logging:

Press submit: there are two events, so error.
Press delete: in session there is only one object. (Ok)
Then I see in the log another record from response center function - that there are TWO OBJECTS in session. (Not OK)
Press submit and error - two objects, cannot submit. (I assume response center when writing another variable had to write all record and it had for some reason taken old record and just updated it).


So now the question - why then response_center function when setting userdata took old userdata? (As we can see in set_userdata function it takes current state and adds index)




Theme © iAndrew 2016 - Forum software by © MyBB