Jump to content

Using $session to save UI changes on site


iipa
 Share

Recommended Posts

Hello forum!

I have a site, where I want to remember two settings defined by user:

1) Theme (light or dark)
2) Language (Finnish or English)

Because user makes changes to these settings on client side, I am a bit lost with how can I save them in ProcessWire $session variable? I would like to use $session for more reliable saving, and since it is only two variables I will use, I doubt it will become too resource-needy.

I have tried using jQuery's post() -method referring to a file in Templates folder (theme.php), but I get 403 Forbidden Error. I don't like the idea of trying to post to same file user currently is in, which is why I thought having a separate file would be good in this.

Contents of theme.php:

<?php
	namespace ProcessWire;
	header('Access-Control-Allow-Origin: https://domain.com');
	$theme = $input->post['theme'];
	if(!empty($theme)) $session->theme = $theme;
?>

Long story short: Does anybody have any pro tips I could use with setting and getting $session variables?

Link to comment
Share on other sites

I build and use one template that is bound to a single page in PW where both are simply named "ajax". I can access it per URL "/ajax/" and use it for everything that needs to send or retrieve data in the background. Here is an example from a project where different forms send data that need to be validated first and then send to final (external) destinations. The client browser optionally gets back HTML like "success messages" or error descriptions.

<?php namespace ProcessWire;
/*==============================================================================
    XXXXXXXXXX :: site/templates/ajax.php
    2019-07-04
==============================================================================*/


/*******************************************************************************
*
*   This template file handles form submissions that are raised via AJAX!
*
*   Sanitize, validate, log and submit to final destinations.
*
*
    // how the used JS looks like
        $.ajax({
            url      : gAjaxUrl,                // /ajax/
            type     : 'POST',
            async    : true,
            dataType : 'html',                  // returned data has to be HTML
            data     : submitData,              // submitted data object
            success  : function(markup) {
                if('' != selector) {            // a selector string to match the
                    $(selector).html(markup);   // markup container
                }
            }
        });
        
    ...
*
*******************************************************************************/

// only process AJAX calls
if(!$config->ajax) return 'WRONG ACCESS METHOD';

// only process POST requests with valid required id
if(!$input->post->hidden_id) return 'WRONG ACCESS METHOD';

// ... followed by all project specific validations etc.

 

So, with your example I would check if there is a post value named theme, and if the value is one out of a whitelist of valid theme values:

// does this belong to theme selection
if($input->post->theme) {
	$validThemeValues = ['dark', 'light'];
	if(!in_array($input->post->text('theme'), $validThemeValues)) {
		header('HTTP/1.1 403 Forbidden');
		return;
	}
	$session->set('selectedTheme', $input->post->text('theme'));
	return;
}

// does this belong to another thing?
if($input->post->anotherThing) {

	// first validate, then process data
	// ...
	return;
}

 

  • Like 10
Link to comment
Share on other sites

2 hours ago, iipa said:

two settings defined by user:

1) Theme (light or dark)
2) Language (Finnish or English)

I would prefer cookies instead of sessions for such user-choices. Sessions expire when you close your browser, but cookies will still be remembered after x days (= the lifespan you define). You can set cookies with JS alone, so you don't even need a server / PHP for that. 

  • Like 8
  • Thanks 1
Link to comment
Share on other sites

17 hours ago, dragan said:

I would prefer cookies instead of sessions for such user-choices. Sessions expire when you close your browser, but cookies will still be remembered after x days (= the lifespan you define). You can set cookies with JS alone, so you don't even need a server / PHP for that. 

Somehow I didn't consider this option at all! Thanks @dragan, I decided to implement this with cookies ?

18 hours ago, horst said:

I build and use one template that is bound to a single page in PW where both are simply named "ajax". I can access it per URL "/ajax/" and use it for everything that needs to send or retrieve data in the background. Here is an example from a project where different forms send data that need to be validated first and then send to final (external) destinations. The client browser optionally gets back HTML like "success messages" or error descriptions.

And thanks to you too @horst, I'm sure this will come handy at some point!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...