How to create Cookies With Example

What is Cookies and how to create:



In computing, a cookie is a small string of text stored on a user’s computer by a web browser. A cookie consists of one or more name-value pairs containing bits of information such as user preferences, shopping cart contents the identifier for a server-based session, or other data used by websites.
It is sent as an HTTP header by a web server to a web client (usually a browser) and them sent back unchanged by client each time it accesses that server . A cookie can be used for authenticating, session tracking (state maintenance), and maintaining specific information about users, such as site preferences or the contents of their electronic shopping carts.


Example of asp.net cookies:


To write a cookie by creating an instance of the HTTP cookie object.
1. Create an object of type HTTP cookies and assign it a name.
2. Assign values of cookies, by set cookies property.
3. Add the cookies to cookies collection.
Example:
http cookie ck1,ck2;
ck1=new http cookies("name",Textbox1.text);
responce,cookies.add(ck1);
}

Creating cookies in asp.net :

HttpCookie myCookie = Request.Cookies["myCookie"];
if (myCookie == null)
{
}
if (!string.IsNullOrEmpty(myCookie.Values["userid"]))
{
    string userId = myCookie.Values["userid"].ToString();
}

Cookies are  known by many other  names,

1.       HTTP Cookie,
2.       Web Cookie,
3.       Browser Cookie,
4.       Session Cookie, etc.
  Clint site storage ,Cookies are one of several ways to store data about web site visitors during the time when web server and browser are not connected. Common use of cookies is to remember users between visits. Practically, cookie is a small text file sent by web server and saved by web browser on client machine.

Disadvantage of cookies :

Size of cookies is limited to 4096 bytes.
Total 20 cookies can be used on a single website;
if you exceed this browser will delete older cookies.
User handled ,End user can stop accepting cookies by browsers,Less security .

Cookies are used for shopping basket or shopping cart at Amazon.com, whereby the cookies store any addition or deletion by the users for the terms they wise to purchase.



Other Asp.net related Post:

Popular posts from this blog