Page 1 of 1

Storing URL parameters in a PHP session variable [done]

Posted: Thu Sep 21, 2023 11:24 am
by nac
I have recently wanted to use some custom parameters passed via the login URL. I propose storing these as a PHP session variable which is created when the $_GET array is parsed in index.php. My request then is to add these three lines near line 229 in index.php (after the built-in Auto Login parameters have been assigned to variables). In my example, the password parameter would be removed from the stored array.

Code: Select all

	
$nuHome = $_GET['h'] ?? '';

// store the parameters for the duration of the session
$URLParams = $_GET;
unset($URLParams['p']);  // remove the password from the array
$_SESSION['nubuilder_session_data']['URL_PARAMS'] =  $URLParams;
// end of parameter storage insert

I realise that there are methods in both JavaScript and PHP to achieve this, but a little experimentation has convinced me this would be the simplest and most reliable solution.

Thanks

Neil

Re: Storing URL parameters in a PHP session variable

Posted: Fri Sep 22, 2023 7:07 am
by kev1n
Hi Neil,

Thanks for your suggestion!

The code within index.php can also be executed at a later time, such as when an iframe is loaded. Therefore, it would be prudent to conditionally set the URL_PARAMS only if they have not been defined already. Failing to do so could lead to potential difficulties in retrieving them later.

Code: Select all

	$URLParams = $_GET;
	unset($URLParams['p']);
	if (!isset($_SESSION['nubuilder_session_data']['URL_PARAMS'])) {
		$_SESSION['nubuilder_session_data']['URL_PARAMS'] = $URLParams;
	}
The parameters can be retrieved with nuGetURLParams(). Update is on Github, please test.

Re: Storing URL parameters in a PHP session variable

Posted: Fri Sep 22, 2023 10:41 am
by nac
Thanks Kevin,
That all makes sense. I will test it and let you know ASAP.
Neil

Re: Storing URL parameters in a PHP session variable

Posted: Tue Sep 26, 2023 10:06 am
by nac
Hi Kevin,
On the basis of tests done so far, this code is fine and working exactly as expected.
Thanks
Neil