Page 1 of 1
Javascript: sessions
Posted: Mon Aug 15, 2005 8:28 am
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?
Re: Javascript: sessions
Posted: Mon Aug 15, 2005 8:40 am
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?
- 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.
- 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.
- 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.
Posted: Mon Aug 15, 2005 9:12 am
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?
Posted: Mon Aug 15, 2005 10:04 am
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.
Posted: Mon Aug 15, 2005 2:30 pm
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?
Posted: Mon Aug 15, 2005 2:46 pm
by feyd
that page has like 5 HTML pages being sent at one time..
Posted: Mon Aug 15, 2005 2:56 pm
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?