Hash cookies & sessionStorage

Questions related to using nuBuilder Forte.

Hash cookies & sessionStorage

Unread postby klasyc » Sun Nov 21, 2021 11:46 pm

Hello,

have a general question about hash cookies and session storage. I was able to find some information on this forum and in wiki, but I still cannot fully understand how they exactly work. Please could you check/correct my conclusions below?

1. For each item in a form, a hash cookie is automatically created after I POST the form. I.e. the hash cookie is created on PHP side and it also means I cannot reference the hash cookie from Javascript (I have to use jQuery to get field values). Then the hash cookie can be used in PHP custom code and in the javascript (nuGetProperty() function) code after the form is returned. Then the hashcookie disappears.

2. I can create a custom hash cookie by calling javascript function nuSetProperty(). It behaves the same way as the "automatic" hash cookie (it is valid for PHP code and returned form only) except I set the third parametr true. In this case the hash cookie has a global validity, but it's still limited to the scope of current browser window/iframe because the hash cookies are preserved as COOKIES in the browser window, right?

3. The session storage uses PHP $_SESSION array as a variable storage, therefore it is shared among all browser windows and has global validity.

4. When I call sessionStorage.setItem() from Javascript, the session variable is preserved in the browser until the form is sent and then it is stored in $_SESSION. In the other words, nuBuilder does not make asynchronous request to set the sessionStorage variable immediately when I call sessionStorage.setItem(). This implies when I use sessionStorage in onClick() event of Run object which opens a new tab, the new tab will not get fresh sessionStorage content yet because the original form has not been sent yet.

5. Session storage data cannot be used in SQL statements like #hash_cookies#.

Currently I am trying to make a form which contains a Run object. This run object opens another form which is restricted to a particular primary key. The primary key is provided by the form where the Run object resides. I want to filter the form according to the provided primary key and also when I create a new item in the form, I want to prefill my primary key.

My problem is that I need a hash cookie for the browse SQL statement, but also the session storage variable for the Edit form. I would prefer using one or another concept, but I don't like mixing of them :(

Thank you for your feedback!
Klasyc
klasyc
 
Posts: 5
Joined: Fri Nov 12, 2021 12:55 am
nuBuilder Version: 4.5

Re: Hash cookies & sessionStorage

Unread postby kev1n » Mon Nov 22, 2021 12:51 am

HI,

In the form's BE event, use nuAddJavascript() to sets the current Form's property/to make it available for the form.

Code: Select all
nuAddJavascript('nuSetProperty("#YOUR_GLOBAL_HASH_COOKIE#")');
kev1n
nuBuilder Team
 
Posts: 2117
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5

Re: Hash cookies & sessionStorage

Unread postby klasyc » Tue Nov 23, 2021 2:27 am

Hi Kevin,

thank you for your suggestion, this is very useful tip! Anyway, what about my questions about hash cookies and session storage? I really appreciate you post quick solutions here, but as a developer I would prefer to understand how the system exactly works because then I would be able to find the solutions myself. Also it would be very useful to have such information on wiki, but I don't know how to participate there.
klasyc
 
Posts: 5
Joined: Fri Nov 12, 2021 12:55 am
nuBuilder Version: 4.5

Re: Hash cookies & sessionStorage

Unread postby kev1n » Tue Nov 23, 2021 4:18 pm

Some additions and corrections:

I have to use jQuery to get field values


Use nuGetValue(), object_id.value or $('#object_id').val() to get a field's value.

can be used in PHP custom code


and in SQL (Display, Select objects)

and in the javascript (nuGetProperty() function) code after the form is returned.


Only hash cookies that are set with nuSetProperty can be retrieved with nuGetProperty(). But not the once that are automatically created.


but it's still limited to the scope of current browser window/iframe


No, global hash cookies exist in all browser windows/tabs/iframes.

because the hash cookies are preserved as COOKIES in the browser window, right?


They are not stored in cookies but in the session database table.


then it is stored in $_SESSION.


Session storage items are not stored in $_session. The information is stored in the user's browsing session, which is discarded by the browser when it is closed,
kev1n
nuBuilder Team
 
Posts: 2117
Joined: Mon Oct 15, 2018 2:13 am
nuBuilder Version: 4.5


Return to General

Who is online

Users browsing this forum: No registered users and 24 guests