473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Changing session cookie via javascript?

Hi All,

Just wondering how you go about changing the value of a session cookie
via javascript?

I have a PHP page that sets a session cookie when it first loads. I'd
like to be able to change the value of that session cookie in response
to a button click in a form, without resubmitting the page.

For some reason, the following doesn't seem to work:

document.cookie = 'myinfo=newvalue';

When I reload the page, the value in the session cookie originally set
by the PHP code has been retained, rather than changing to the new
value set by the JavaScript.

Can anyone give me any thoughts on why this session cookie is
resisting the JavaScript change?

Many thanks,

Murray
Jul 20 '05 #1
3 5931
On Tue, 03 Feb 2004 21:23:11 GMT, M Wells
<pl**********@planetthoughtful.org> wrote:
When I reload the page, the value in the session cookie originally set
by the PHP code has been retained, rather than changing to the new
value set by the JavaScript.

Can anyone give me any thoughts on why this session cookie is
resisting the JavaScript change?


When you reload the page, wouldn't the Set-Cookie header be re-applied,
thereby resetting the cookie value?

Try checking the value immediately after setting the cookie with
JavaScript to see if it was altered in the first place.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
On Tue, 03 Feb 2004 21:44:16 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:
On Tue, 03 Feb 2004 21:23:11 GMT, M Wells
<pl**********@planetthoughtful.org> wrote:
When I reload the page, the value in the session cookie originally set
by the PHP code has been retained, rather than changing to the new
value set by the JavaScript.

Can anyone give me any thoughts on why this session cookie is
resisting the JavaScript change?


When you reload the page, wouldn't the Set-Cookie header be re-applied,
thereby resetting the cookie value?

Try checking the value immediately after setting the cookie with
JavaScript to see if it was altered in the first place.


Hmmm. It seems PHP and JavaScript may be saving their session
variables in different places on the server.

When I retrieve the list of session variables available to the PHP in
the page, I get a variable with the name I want with the value I want
changed and when I retrieve the list of session variables available to
the JavaScript in the page, I get the same variable name with the new
value.

I wonder if the PHP code is saving the session into the current
directory, while the JavaScript is saving the session into the root
directory of my site?

Thanks for your suggestion!

Much warmth,

Murray
Jul 20 '05 #3
M Wells wrote:
On Tue, 03 Feb 2004 21:44:16 GMT, Michael Winter
<M.******@blueyonder.co.invalid> wrote:

On Tue, 03 Feb 2004 21:23:11 GMT, M Wells
<pl**********@planetthoughtful.org> wrote:

When I reload the page, the value in the session cookie originally set
by the PHP code has been retained, rather than changing to the new
value set by the JavaScript.

Can anyone give me any thoughts on why this session cookie is
resisting the JavaScript change?


When you reload the page, wouldn't the Set-Cookie header be re-applied,
thereby resetting the cookie value?

Try checking the value immediately after setting the cookie with
JavaScript to see if it was altered in the first place.

Hmmm. It seems PHP and JavaScript may be saving their session
variables in different places on the server.

When I retrieve the list of session variables available to the PHP in
the page, I get a variable with the name I want with the value I want
changed and when I retrieve the list of session variables available to
the JavaScript in the page, I get the same variable name with the new
value.

I wonder if the PHP code is saving the session into the current
directory, while the JavaScript is saving the session into the root
directory of my site?

Thanks for your suggestion!

Much warmth,

Murray


I think you are misunderstanding cookies....

Putting PHP aside for a moment, let's talk about the way a web server
sends cookies, using HTTP. A web server sends a header, including the
content type, content length, and other things. One of those things can
be a cookie string... For example:

Set-cookie: myinfo=newvalue; domain="yourdomain.com"
Content-type: text/html
Content-length: 3344

<HTML>
-- rest of the HTML code

With that said, let's bring PHP back. In php, you set a session. It
tells the web server to run the Set-cookie header in the response...
which is why you need to do it at the top of the script, before you send
any content.

The cookie is never saved on the server.

So where is it saved? The browser gets the Set-cookie command, and the
cookie is saved some place (browser specific) on the user's machine. It
then reads the rest of the HTML code, and processes it.

** Enter your javascript **

At this point, your javascript can read document.cookie, and it will be
what the server set. If you change it, you WILL change the value saved
on your side... but if you reload the page, you will get the same HTTP
header, and it will be reset to the original value.

So, you may be asking... how does the server know the cookie value?
Well, it is pretty simple... When your browser makes a request to the
server, and a cookie exists for that domain, it will exist in the
request header:

GET /somepage.html HTTP/1.0
Cookie: <the cookie string>

Now, the server can tell PHP what the cookie is, and you can retrieve it
via PHP libraries.

So, in short, your cookie is only stored in one place... on the client's
machine. It is never stored on the server. The state is always
maintained on the client.

I hope this helps.
Brian

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
I want to know what's the differences between session cookie and regular cookie. In ASP, when we create cookie, we do the following to identify an user: Response.Cookies("name") = value Is...
3
by: Brett | last post by:
I'd like to set a session cookie that expires once the browser is closed. I've tried: document.cookie = "wm_javascript=" + escape("SomeName|" + window.document.referrer); with no expires. ...
4
by: yingjian.ma1955 | last post by:
Hi All, According to a book, the cookie without expires lasts the browser session. I think a browser session ends if you close all the browser windows. I setup a cookie without expires in IE6...
3
by: John Drako | last post by:
Short Question: is there a Javascript library that can decode and encode data into the format used by PHP sessions stored in the session cookie? The longer version: I'm creating a fairly...
3
by: Karsten Grombach | last post by:
Hi, I'm trying the following: - Imitate a Logon using a Post with HttpWebRequest on remote Webserver (asp 3.0 page using https) - On success redirect to the page (encapsuled in an iframe)...
2
by: Earl Teigrob | last post by:
I am trying to find storage mechanism that will store a variable until the browser window is closed. A query string variable would be my first choice for this but that is not easily done in my...
7
by: Doug | last post by:
An ASP.NET session cookie set on "www.mydomain.com" can not be accessed on "search.mydomain.com"; hence, a new session and cookie are being created on every sub-domain. This is occuring because...
4
by: nico | last post by:
I store the authenticated user in a session cookie. This cookie expires after an hour. Now for some reason sometimes the client browser decides not to send the session cookie information with a new...
4
by: Yonih | last post by:
Hey Yall, I am trying to incorporate a Once Per session Cookie that will expire once the browser is closed so some one who comes to my website will see my popup the first time her visits the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.