0

I was wondering if there is a way to set session variable in Ajax response, which would basically look like this:

// this is response
function(response)
{
<?php $_SESSION['ID'] = response.id ?>
// or something similar to this? 
}

And then afterwards when I need to use the session so that I can access it.

// (Some php file)

$var_id = $_SESSION['ID']; 

Is this doable?

Cœur
  • 37,241
  • 25
  • 195
  • 267
perkes456
  • 1,163
  • 4
  • 25
  • 49
  • JavaScript code runs on the *client*. It runs in somebody's web browser. It cannot do things in code on your server. – Pointy May 09 '16 at 13:42
  • you can do on your server side, using PHP in your scenario – Muhammad Faran Ali May 09 '16 at 13:43
  • You could take that response.id and send it in an ajax call to a php endpoint which sets it – Steven Kaspar May 09 '16 at 13:43
  • I understand but the value that I need to store into the session should be just done locally on someones web browser, and then later on retrieved in code.. I think its very logical thing that I mentioned, there has to be a way to do this... Perhaps if not storing the value into the session, is there some other way? – perkes456 May 09 '16 at 13:44
  • you could use a cookie instead – Steven Kaspar May 09 '16 at 13:44
  • @StevenKaspar I send out the username and password as values to the PHP action inside the controller... and as the response I get the entire object (users details when he logs in). How do you suggest I do this since the response.id is the product of the response from the action that is called? – perkes456 May 09 '16 at 13:45
  • @StevenKaspar Can you make an official response with an example on how to do this, so that I can accept it as an answer ? And possibly how to clear the cookie when needed? – perkes456 May 09 '16 at 13:46
  • Maybe you should read [this](http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Fatal Error May 09 '16 at 13:52

2 Answers2

1

Here are three functions to set, get, and delete cookies

https://jsfiddle.net/stevenkaspar/gnoj9aop/

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+ d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length,c.length);
        }
    }
    return "";
}
function deleteCookie(cname){
  document.cookie = cname+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
Steven Kaspar
  • 1,147
  • 10
  • 14
0

You can use HTML5 sessionStorage Object to save the data locally but for a single session . The data is deleted when the user closes the specific browser tab.

sessionStorage.id = response.id ;
Megan Fox
  • 435
  • 2
  • 6
  • 20