473,782 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=newvalu e';

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 5976
On Tue, 03 Feb 2004 21:23:11 GMT, M Wells
<pl**********@p lanetthoughtful .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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
On Tue, 03 Feb 2004 21:44:16 GMT, Michael Winter
<M.******@bluey onder.co.invali d> wrote:
On Tue, 03 Feb 2004 21:23:11 GMT, M Wells
<pl**********@ planetthoughtfu l.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.******@bluey onder.co.invali d> wrote:

On Tue, 03 Feb 2004 21:23:11 GMT, M Wells
<pl********** @planetthoughtf ul.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 misunderstandin g 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="yourdom ain.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
2948
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 this regular cookie? How about session cookie? and when to use which?
3
6392
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. This doesn't set a cookie. It seems I must put in an expiration for the cookie to set. The code below does work: var the_name = window.document.referrer;
4
3492
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 using JavaScript and closed it. When I open IE agian, the cookie is still there. Why? Does it mean that a session cookie will be deleted at some time after the browser session? (Just like a stored cookie will be deleted at some time after the...
3
2708
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 complex interactive application (wizard like) using PHP and storing the data from various steps in a session.
3
7377
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) supplied by the remote Webserver I can successfuly logon but when I redirect to the supplied url, the webserver does not know me anymore an redirects me back to login page.. I
2
1882
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 situation. Would a session cookie do the trick? From what I have read, it is persistent until the browser window closes. Is that correct? Even if the session expires, will the session cookie be available when the user continues using the application? ...
7
7776
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 ASP.NET always sets the Session cookie domain to the full domain (e.g. "www.mydomain.com") instead of the parent domain (e.g. "mydomain.com") The problem with this is when the visitor goes to a different sub-domain (e.g. "search.mydomain.com"),...
4
1250
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 request. This means the server receives a request without user information and redirect the user to the login page. Somebody any bright ideas? TX!
4
6126
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 website but it wont keep showing as one navigates throughout the site however if he closes the browser and reopens out site it will show again. Here is the script that I was using that started out to work once per session but after publishing the...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.