Cookies
Cookies are usually used to store user identity or we can say user data. Cookies are small text files which is created by the client browser and are stored in the hard drive.
When a user makes an initial request for a page the server creates a cookie for that user and stores it in the hard drive and then again the same user tries to access a page in the same web site, the cookie stored is matched and then it grants the access. Cookies can be send from one page to another after creation. It is not possible in the case of View State variables as it works for a single page.
There are two types of cookies:
- Persistent Cookie
- Non-Persistent Cookie
Persistent Cookie
When we set an expiry time to a cookie it is known as Persistent Cookie. It remains stored till the time expires.
Example
Let us take the following example:
Design
This application will save the details entered in the textbox into the cookie when we will click the save button. When we will click display button it displays the saved cookie in the two labels below the button.
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace httpstateless
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e) //save button
{
//Creating cookie and saving the value.
HttpCookie cookie = new HttpCookie("User Details");
cookie["Name"] = TextBox1.Text;
cookie["Blog Name"] = TextBox2.Text;
//Setting the expire time to 30 days
cookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(cookie);
}
protected void Button2_Click(object sender, EventArgs e) //display button
{
//Cookie request.
HttpCookie cookie = Request.Cookies["User Details"];
if (cookie != null)
{
Label1.Text = cookie["Name"];
Label2.Text = cookie["Blog Name"];
}
}
}
}
Output
This is the output after the whole process. Now if you close the browser and then open the page again and display without saving the new cookie, it will display the retained value in the cookie that we saved earlier. This is because we are using the persistent cookie and we gave the expiry time of 30 days. So it will be retained for that period of time.
Non-Persistent Cookie
Non-Persistent Cookies are not saved permanently in the hard drive. It is retain as long as the user is not closing the browser. When the browser is closed all non-persistent cookies are cleared automatically.
Design
Same as above example
Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace httpstateless
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e) //save button
{
//Creating cookie and saving the value.
HttpCookie cookie = new HttpCookie("User Details");
cookie["Name"] = TextBox1.Text; //Note that expiry time is not specified
cookie["Blog Name"] = TextBox2.Text;
Response.Cookies.Add(cookie);
}
protected void Button2_Click(object sender, EventArgs e) //display button
{
//Cookie request.
HttpCookie cookie = Request.Cookies["User Details"];
if (cookie != null)
{
Label1.Text = cookie["Name"];
Label2.Text = cookie["Blog Name"];
}
}
}
}
Output
Output is also same as that of persistent cookies.But when we close the browser after this all the cookies are cleared. So when we load the page again in the browser and click display without entering the value in the textbox the label will remain empty as cookies are not retained after the browser is closed.
You may click the Content tab to see all the posts that i will cover in this tutorial for easy navigation.
No comments:
Post a Comment