Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting session variable using javascript

I am getting name and email id of a user after he logs in via facebook to my website.. I want to add those variables in session on login form itself using javascript; I tried following:

FB.api('/me', function(me) {         if (me.name) {             document.getElementById('auth-displayname').innerHTML = me.name; <% Session["fbName"] = me.name; %>         } } 

it gives error like me (in this line: <%Session["fbName"] = me.name; %>) does not exist in the current context etc.. my div "auth-displayname" is getting that value but I'm having problem with session variable

How can I do this

like image 219
Sam Avatar asked Aug 06 '13 09:08

Sam


People also ask

Can you set a session variable in JavaScript?

Javascript can not directly set session values.

What are session variables in JavaScript?

Session is a variable on the backend server side, while JS is a previous script. There is no ready-made method in JS to get the value of Session, and it needs to be obtained through the server language. For example, java can be used to get the value of Session and assign it to JS variable.

Can I get session value in JavaScript?

JavaScript is a Client Side language and hence directly it is not possible to get Session value in JavaScript.

How do you create a session variable?

Before you can store any information in session variables, you must first start up the session. To begin a new session, simply call the PHP session_start() function. It will create a new session and generate a unique session ID for the user. The PHP code in the example below simply starts a new session.


2 Answers

You can use

sessionStorage.SessionName = "SessionData" ,

sessionStorage.getItem("SessionName") and

sessionStorage.setItem("SessionName","SessionData");

See the supported browsers on http://caniuse.com/namevalue-storage

like image 142
User Avatar answered Oct 11 '22 15:10

User


A session is stored server side, you can't modify it with JavaScript. Sessions may contain sensitive data.

You can modify cookies using document.cookie.

You can easily find many examples how to modify cookies.

like image 35
Halcyon Avatar answered Oct 11 '22 15:10

Halcyon