This is a discussion on How to set time for cookie in ASP.NET? within the ASP and ASP.NET Programming forums, part of the Web Development category; Hi all, How to set time for cookie? Is any sample?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Yes, You can easily set the time when a cookie expires. We'll set the Expires property of myCookie to the current time number of hours: Code: myCookie.Expires = DateTime.Now.AddHours(2); ![]()
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Yes u can, for that use as following., Code: myCookie.Expires = DateTime.Now.AddDays(7);
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| <script runat="server"> void WriteClicked(Object Sender, EventArgs e) { //Create a new cookie, passing the name into the constructor HttpCookie cookie = new HttpCookie(NameField.Text); //Set the cookies value cookie.Value = ValueField.Text; //Set the cookie to expire in 1 minute DateTime dtNow = DateTime.Now; TimeSpan tsMinute = new TimeSpan(0, 0, 1, 0); cookie.Expires = dtNow + tsMinute; //Add the cookie Response.Cookies.Add(cookie); Response.Write("Cookie written. <br><hr>"); } </script> <html> <body> <h3>Use the button below to write cookies to your browser </h3> The cookies will expire in one minute. <form runat="server"> Cookie Name <asp:textbox id="NameField" runat="server"/><br> Cookie Value <asp:textbox id="ValueField" runat="server"/><br> <asp:button text="WriteCookie" onclick="WriteClicked" runat="server" /><br> </form> <a href="readcookies.aspx">Read the cookies</a> </body> </html> |
| |||
| Hi buddy.... A small information about cookies.... Sometimes you'll want to set a path for a cookie so that it will be available only for that path in your website. You can set a cookie's path with the Path property: Code: myCookie.Path = "/test";
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Another one information in cookie is here..... We can set the domain for a cookie as follows, Code: myCookie.Domain = "test.com"; Code: myCookie.Domain = "forums.test.com";
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Response.Cookies["userName"].Value = "patrick"; Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1); // the above creates the cookie, or overwrites it if the cookie already exists. HttpCookie aCookie = new HttpCookie("lastVisit"); aCookie.Value = DateTime.Now.ToString(); aCookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(aCookie); |
| |||
| hi all it really helps me but one doubt, what happens when we create a cookie in the name which has already exists. thanks in advance |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| show clocks, one showing the current time, and another showing a time 12 hours behind | itbarota | HTML, CSS and Javascript Coding Techniques | 1 | 02-13-2008 10:15 PM |
| How can I set the time zone for MySQL to UK time? | itbarota | PHP Programming | 1 | 09-12-2007 01:13 AM |
| How can I set a cookie in JSP? | anbuchezhians | Java Server Pages (JSP) | 1 | 07-27-2007 03:02 AM |
| Google to shorten time 'cookie' data is stored | senthilkannan | 0 | 07-19-2007 05:01 AM | |
| Diff between Transaction-time and Server response-time | vadivelanvaidyanathan | Software Testing | 0 | 03-21-2007 07:29 AM |