1

I'm new in Expression Engine, but have a good knowledge of codeigniter framework.

Idea is to store custom value in user session: geolocation data (City name) and put this value to input.

I wrote some javascript code, but it fires every time, when user loading page. I want to do it once and then retrieve location from user session.

I'm not sure that I need modify EE core modules. Is there any standart EE module, which handle user session globally at whole site?

2
  • Hi namesmile, when you say 'store a custom value in user session' do you mean in the client side session cookies or the server site session object? Also, re your JS code, you say it runs every time the page loads, but this would be correct wouldn't it? JS has no memory between page loads, you'd have to re-read it from the session/server for each page request any how, no?
    – Blatant
    Commented May 7, 2015 at 9:04
  • As for sessions - I prefer session, than cookies, but really don't care about it. Which way is more beautiful - I will follow it. And yes, that's why I want to use session or cookies to store location data - just once detect location and for next page load just read that values from cookies\session. I think need to code 2 hidden fields in form and setup values in session on search request
    – namesmile
    Commented May 7, 2015 at 10:23

2 Answers 2

0

Thanks for the question feeedback, I beleive you have 2 options here, it's up to you which you feel is more graceful!

  1. Utilise the Server side PHP/EE Session

For this solution, you'll need a special template that you'll call via AJAX, I normally use a template group called 'API', for example lets call the template 'location'.

On the template, we're going to create 2 modes, one that returns the saved field value and one that saves the field value. This is easy as what we can do is look for a post field and if present and correct set the sesion value, if there's no post then return the value.

For seculrity I'd add a check to ensure the request is an AJAX request (you can google this). Now, this template is going to be a PHP template but there's no reason you can't use EE tags and the plugins Nicholas suggests, but for simplicity I'm going to stick with native PHP.

So here's the process that I'd do :

  1. Create JS that 'fetches' the saved value, if it's blank (not set yet, new session ect) then create the JS that 'sends' the value. Your JS will query the location object and get the value you want to save, and then AJAX Post it to your template.

  2. In the template, use PHP to get the submitted value, be sure to clean it, and save it to the session :

    //check AJAX request here
    
    //are we getting or setting?
    if ( empty ( $_POST ) ) {
        //assume this is the get, make more complex if you wish!
        if ( isset ( $_SESSION['savedvalue'] ) ) {
            echo json_encode(array(
                'savedvalue' => $_SESSION['savedvalue']
            )); 
        } else { 
            echo json_encode(array(  
                'savedvalue' => ''
            ));    
        }
    } else {
        //were gonna set the value!! 
        //use EE to fetch and clean the post
        $postedValue = ee()->input->post('valuename', true); 
        //any extra validation here
        $_SESSION['savedvalue'] = $postedValue;
    }
    

Thats it, at least in principle, hopefully you get the idea of what I'm doing here and can run with it...

  1. Use a cookie on the client side

No need for a PHP template or anything fancy here, as we're not leaving the client side.

What we'll do is create similar JS to before but rather that making AJAX calls to the template (to push the value server side) we replace these calls with JS that sets the session cookie you want. You can use JS like this : http://blog.lysender.com/2011/08/setting-session-only-cookie-via-javascript/

There ya go, you should be able to get either to work for you, the beauty is that the AJAX requests have the PHP SESSION ID in them or at least send it back with the response so you don't need to worry about linking a session to a user. One thing to think about though is the logic to update the saved value if it ever changes, after all I might load your page on my laptop, leave it open, hop in the car, goto work and change location!

Drop us a comment if you have further questions, OR vote up/answer anything that helped you get what you needed...

2
  • Your question doesn't really explain but from your comment it seems you want to use a search fom as the 'trigger' to save the location. if this is the case you could apply either technique but rather than use your own AJAX template you can use the results page of the search results...
    – Blatant
    Commented May 7, 2015 at 11:00
  • Thanks for answer! I'm EE newbie and don't understand how templates working. But in your answer key phrase: "you'll need a special template that you'll call via AJAX" a bit helped me to reach it. I chose sessions instead of cookies.
    – namesmile
    Commented May 7, 2015 at 20:20
2
  1. Remember Me

Save entries for a user during a session. This could be used for a 'add to cart' function or for a 'product compare' function (save entry_id's for later use). Entries are only stored during a session. As of version 1.0 you can also save the session storage to the database on a per member basis.

  1. Session Variables Plugin

Sometimes you may want to temporarily store a variable for a short amount of time without writing a value to the database. ExpressionEngine's sessions do not allow this, however PHP's native sessions do.

  1. You can setup cookies with data using JS and then get this information using Mo’ Variables.

Add more early-parsed, global variables to your EE installation.

1
  • Thanks! I already downloaded Session Variables Plugin and quite interesting this JS plugin. As for Session Variables Plugin, I think add hidden fields to form for coordinates. When form data sent to search controller, grab coordinates and write them into user session. What do you think about it? Is it correct way?
    – namesmile
    Commented May 7, 2015 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.