views:

59

answers:

3

HI , can we set session variable using javascript in php?

+1  A: 

The session is stored on the server side so you cannot add values to it from javascript. All that you get on the client side is the session cookie which contains an id. One possibility would be to send an AJAX request to a server side script which would set the session variable. Example with jQuery's .post() method:

$.post('/setsessionvariable.php', { name: 'value' });

You should of course be cautious about exposing such script.

Darin Dimitrov
+1  A: 

You can't directly manipulate a session value from Javascript - they only exist on the server.

You could let your Javascript get and set values in the session by using AJAX calls though.

See also

Paul Dixon
+1  A: 

If you want to allow client-side manipulation of persistent data, then it's best to just use cookies. That's what cookies were designed for.

Lèse majesté