Page 1 of 1

Hash cookies that persist for the duration of a session

Posted: Wed Nov 25, 2020 9:38 pm
by nac
In the current version of nuBuilder, most hash cookies are transient as they are retrieved as form properties. I have often thought that it would be useful to have a hash cookie that is set once, e.g. on the landing page, and then is available in every form, report and PHP procedure.

There are some which are always available (see https://wiki.nubuilder.cloud/ ... sh_Cookies). These values (USER_ID, USER_GROUP_ID, HOME_ID, GLOBAL_ACCESS, ACCESS_LEVEL_CODE) are extracted from zzzzsys_session.sss_access. This arrangement provides a model for session-persistent values which are stored in and retrieved from the zzzzsys_session table.

One way of doing this

Add a new text column in zzzzsys_session (let's call it sss_hashcookies). This would be the same column type as sss_access and be used to store hash cookie key/value pairs in JSON format.

There are at least two ways that this value could be set on the client-side (JS).

A. Modify nuSetProperty(string1, string2) to nuSetProperty(string1, string2, boolean3) where boolean3 is an optional parameter with a default value of false. If it is set true then the property will be stored in zzzzsys_session.sss_hashcookies as well as setting the form property. If false, the behaviour remains the same as the existing function. It would be necessary to define precedence if the same property name is used in multiple calls to the function which switches the value of boolean3. For example, if a new temporary property is set and it has the same name as a key in zzzzsys_session.sss_hashcookies, then the key/value pair in sss_hashcookies is removed or is just overridden (see the modified nuReplaceHashVariables($s) below). In practice, this should not be a problem as it is up to the developer to define their own naming conventions to avoid potential clashes. The function nuGetProperty(string1) would also need to be modified to retrieve session-persistent hash cookies.

B. Define a new function (e.g. nuSetSessionProperty(string1, string2) ) to set the values in zzzzsys_session.sss_hashcookies. This would also need an equivalent nuGetSessionProperty(string1).

Both of these JS functions would send/receive data to PHP functions that would access zzzzsys_session.sss_hashcookies . The existing PHP functions nuSetJSONData($i, $nj) and nuGetJSONData($i) provide templates for storing and retrieving the session hash cookies.
In nucommon.php , the function nuReplaceHashVariables($s) could be modified so that the $a array includes the contents of zzzzsys_session.sss_hashcookies in addition to the contents of $_POST['nuHash']. Perhaps something like this, below - this code has not been tested, BTW.

Code: Select all

function nuReplaceHashVariables($s){

	$s	= trim($s);
	if($s == ''){
	  return '';
	}

	$q	= "SELECT * FROM zzzzsys_session WHERE zzzzsys_session_id = ? ";
	$t	=  nuRunQuery($q, array($_SESSION['nubuilder_session_data']['SESSION_ID']));			 
	$r	= db_fetch_object($t);
	$j	= json_decode($r->sss_hashcookies, true);

	$a 	= array_merge($j, $_POST['nuHash']);

	if (!is_array($a)) {
		return $s;
	}
	foreach ($a as $k => $v) {
		if(!is_object ($a[$k])) {
			$s	= str_replace ('#' . $k . '#', $v, $s);
		}
	}
	return $s;
}
I would welcome any feedback on the usefulness, practicalities and wisdom of this as well as better ways to implement such a feature.

Thanks

Neil

Re: Hash cookies that persist for the duration of a session

Posted: Sat Nov 28, 2020 2:35 pm
by kev1n
Steven,

What are your thoughts on this? Do you think this is feasible?

Re: Hash cookies that persist for the duration of a session

Posted: Fri Dec 11, 2020 9:14 am
by kev1n
FYI, the global hash cookies will soon be incorporated into nuBuilder.

Re: Hash cookies that persist for the duration of a session

Posted: Fri Dec 11, 2020 12:40 pm
by nac
Thanks kev1n.

That is good news. I look forward to it.

Neil