473,399 Members | 3,603 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,399 software developers and data experts.

Problems setting a cookie that already exists

Hello,

I have discovered that if I try and add a cookie when one by that
already exists, nothing happens. No error, but the cookie is not set to
the new value.

For example (this is running in a DLL, which is why I use
HttpContext)...

HttpCookie cookie = new HttpCookie("fred", "ferret");
DateTime expiryDate = DateTime.Now.AddMonths(1);
cookie.Expires = expiryDate;
HttpContext.Current.Response.Cookies.Add(cookie);

works fine if the cookie "fred" does not already exist, but if it does,
then it is not updated to have the new value.

How do I handle this? I tried...

HttpContext.Current.Response.Cookies["fred"] = cookie;

but that didn't do anything either. Either way, the old value remains in
the cookie.

Any advice appreciated.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
2 3708
>HttpContext.Current.Response.Cookies.Add(cookie );

works fine if the cookie "fred" does not already exist, but if it does,
then it is not updated to have the new value.


I have since discovered that it was added, but the cookie ended up with
two separate values for "fred", and the code was only able to read the
first.

So, I'm still at a loss as to how to update a cookie value when it
already exists. Any suggestions welcome.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #2
>So, I'm still at a loss as to how to update a cookie value when it already
exists. Any suggestions welcome.


Well, as no-one has replied yet, I might as well show my latest failure
;-(

The following code is a complete page that demonstrates my equally
complete failure at using cookies. If you save this as an .aspx and load
it in a browser, it tells you it is creating the cookie. If you reload
the page, it tells you it is changing the value. If you reload it again,
it tells you it is removing the cookie.

All of this is exactly what I expected, except that it didn't work. If
you close the browser window at any stage and reload the page, it always
shows the "creating" message. So, it seems that it's not actually
creating the cookie at all.

Please can someone help me here. I've spent hours trying to solve what
should be a really simple problem. TIA
<%@ Page Language="C#" Debug="true" %>
<script runat="server">

void Page_Load(Object o, EventArgs e) {
string cookieName = "fred";
if (Request.Cookies[cookieName] == null) {
// the cookie does not exist. Create it
x.Text = "Creating a new cookie";
HttpCookie cookie = new HttpCookie(cookieName, "newcookie");
cookie.Expires = DateTime.Now.AddMonths(1);
Response.Cookies.Add(cookie);
} else if (Request.Cookies[cookieName].Value == "newcookie") {
// the cookie was created last time this page was called
x.Text = "Changing value from " + Request.Cookies[cookieName].Value + " to oldcookie";
Response.Cookies[cookieName].Value = "oldcookie";
} else {
// the cookie was created before the previous call to this page. Delete it
x.Text = "Removing the cookie whose value is " + Request.Cookies[cookieName].Value;
Response.Cookies.Remove(cookieName);
}
}

</script>

<html>
<body>
<asp:Literal ID="x" EnableViewState="false" RunAt="server" />
</body>
</html>

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #3

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

Similar topics

1
by: Timur Tabi | last post by:
I have an embedded system with a web interface. One of the web pages has a small JavaScript program that, when run on IE6, always displays the message that cookies need to be enabled: if...
4
by: socialism001 | last post by:
I'm trying to store a value in a cookie but its not working. Can anyone see what I might be doing wrong. Thanks, Chris ~~~~~~~~~~~~~~~~~~ <script language="javascript">...
3
by: Ben Amada | last post by:
Every time a user visits a particular page, I'm using the code below to add a cookie that expires in 1 day. My question is, a user may visit this page a few times within one day. Before adding...
0
by: briand | last post by:
I have the following code in my base page to redirect to a session timeout page. override protected void OnInit(EventArgs e) { base.OnInit(e); //It appears from testing that the Request...
6
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error...
1
by: zorro | last post by:
Greetings, I want users to select the duration time of their sessions. I'm able to do it by setting the PHPSESSID cookie duration. Is this reliable, or is it not recommended for some reason? ...
0
by: Jonathan Wood | last post by:
I seem to be having errors creating and accessing an SQL database. Unfortunatley, I am brand new to SQL setup and administration issues so this really is not my area of expertise. I know I had...
1
by: john.swilting | last post by:
my code index.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta name="generator" content="Bluefish 1.0.7"> <meta name="author" content="root"> <meta...
5
by: sophie_newbie | last post by:
Does anyone know how to do this? I can't seem to make it work. I'm using: c = Cookie.SimpleCookie() c = "unamepwordwhatever" c.expires = time.time() + 300 print c
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.