Javascript: sessions

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Javascript: sessions

Post by raghavan20 »

Is it possible to create and manipulate sessions in javascript?

I have a situation where if one clicks an option inside a select box, I have to store the value of the option in the session variable for future use.

so I am looking for syntax to create session variables with Javascript.

1. I have been having this doubt for a long time.
Is session and cookie related to each other in any way?
2. Where sessions are stored while cookies are stored in the client machine?
3. How do you set timeout for sessions in javascript?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Javascript: sessions

Post by feyd »

raghavan20 wrote:Is it possible to create and manipulate sessions in javascript?
only to the extent that you can create a session cookie. You cannot manipulate any of the variables associated with one from within Javascript directly.
raghavan20 wrote:1. I have been having this doubt for a long time.
Is session and cookie related to each other in any way?
2. Where sessions are stored while cookies are stored in the client machine?
3. How do you set timeout for sessions in javascript?
  1. they can be, that all depends on how your sessions are created. If you use a cookie for them then yes. A url based session does not need a cookie to operate however.
  2. Not sure what you are trying to say there so I'll explain the difference: Session data is stored on the server in some fashion. Only a session ID is [normally] stored in/on the client machine if a cookie is used to keep the session.
  3. sessions do not timeout normally like a standard cookie. A session cookie stays with the browser until it's closed or deleted by the user or server. A page can force the termination of the session by destroying the cookie or whatever reference the browser is using to reference the session.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

alright, you say that session id is alone stored on the client machine and all the session variables for a particular session id will be stored in the server.
are these stored as files in the server???

Can you help me find the syntax for creating session variables using javascript?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

They can be stored in any fashion. (PHP allows you to change how they are handled.) By default, PHP stores sessions as files, yes.

Setting a session variable or a session from Javascript? Session variables would involve making a seperate request in some fashion (XMLHTTP, iframe, image, whatever) to the server asking it to store a session variable. I consider it quite dangerous however unless you perform a LOT of checking on the variable to make sure it doesn't affect your other pages adversly. It's an additional security hole..

As for setting a session, you require some way of generating an ID (the server should supply it.) Then simply have javascript create the cookie without an expiration time marker. It's far better to have php do this though, because there are injections of the session information it often performs to ensure the session transfers between pages.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I thought of using cookie for the same purpose.
I tried to set the cookie using javascript.

the page can be viewed at action:
http://raghavan20.allhyper.com
pls login to the chat to go to the main window
I am trying to print the cookie value on top of the page, user index

the javascript code

Code: Select all

function modifyCookie(value){
		alert ("function called:" + value);
		var expire = new Date();
		expire.setTime(today.getTime() + (3600000*30));
		document.cookie = "selectedUserId=" + escape(value) + ";expires=" + expire.toGMTString();	}
but you can find that the value inside the function is there, you will get the value when you click on the select input control to the right of the chat window.

I would need help to find out why the cookie does not set?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that page has like 5 HTML pages being sent at one time..
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

I earlier had the refresh time around 15 sec for some debuggin in the iframe.
Now, I have brought down the refresh time to 2 sec.
But can you find wot exactly is the problem with setting the cookie?
Post Reply