user1995781
user1995781

Reputation: 19453

Laravel 4 store temporary data

I want to temporary store a series of array which will be used by next request. The stored information contains some sensitive data which will be used for navigating around that page with ajax call. The data were different from pages to pages. So, I just need to temporary store it for use when user is on that page.

First, I try to do it with cache: Cache::put($dynamickey, $multiArray, 20); But this will result in huge amount of "junk" cache store inside the folder even after it is expired.

So, I tried with session flush: Session::flash($dynamickey, $multiArray);. This works when user is open only 1 tab of webpage. But if user is open multiple tab of this website, it breaks.

For example: 1. User browse this website on tab1. 2. Then, user browse this website on tab2. As soon as after user browse website on tab2, the session data for tab1 is removed. 3. User come back and navigate tab1 content. The system break, and not working.

How can I store temporary data which will be deleted once it is no longer required, but also works well with multiple tab?

Thank you.

Upvotes: 1

Views: 2419

Answers (1)

SamV
SamV

Reputation: 7586

So, on the page that actually sets the session data you will need to generate a dynamic key which you can also generate when the ajax call is made. So:

Session:put($dynamicKey, $data);

Since the server doesn't know if you have multiple tabs open it just processes more requests, we need to distinguish AJAX requests from standard ones. This can be achieved via:

if (Request::ajax())
{
    if (Session::has($dynamicKey)) {
        Session::forget($dynamicKey);
        // Do your application logic
    }
}

So the session will not be removed until an ajax request is made where you can regenerate that key, now if you cannot regenerate that key from the data provided then you cannot tell apart two different requests. So you will need to get this key to the client side some how such as echoing it into a bit of javascript.

Now the AJAX call can utilise this key and send it in the request, where your server can pick it up and find the correct session of that tab.

Hope you understand this.

Upvotes: 3

Related Questions