WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP Cookies


A cookie is often used to identify a user.



What is a Cookie?

A treat is regularly used to distinguish a client. A treat is a little document that the server installs on the client's PC. Each time a similar PC asks for a page with a program, it will send the treat as well. With ASP, you can both make and recover treat values.


How to Create a Cookie?

The "Response.Cookies" order is utilized to make cookies.

Note: The Response.Cookies order must show up BEFORE the <html> tag.

In the precedent beneath, we will make a treat named "firstname" and allot the esteem "Duo" to it:

<%
Response.Cookies("firstname")="Duo"
%>

It is likewise conceivable to allocate properties to a treat, such as setting a date when the treat ought to expire:

<%
Response.Cookies("firstname")="Duo"
Response.Cookies("firstname").Expires=#May 10,2018#
%>

How to Retrieve a Cookie Value?

The "Request.Cookies" order is utilized to recover a treat value.

In the precedent underneath, we recover the estimation of the treat named "firstname" and show it on a page:

<%
fname=Request.Cookies("firstname")
response.write("Firstname=" & fname)
%>

Output: Firstname=Duo


A Cookie with Keys

If a treat contains an accumulation of numerous qualities, we state that the treat has Keys.

In the precedent underneath, we will make a treat accumulation named "user". The "user" treat has Keys that contains data about a user:

<%
Response.Cookies("user")("firstname")="dwayan"
Response.Cookies("user")("lastname")="jhonson"
Response.Cookies("user")("country")="canada"
Response.Cookies("user")("age")="28"
%>

Read all Cookies

Look at the accompanying code:

<%
Response.Cookies("firstname")="Duo"
Response.Cookies("user")("firstname")="dwayan"
Response.Cookies("user")("lastname")="jhonson"
Response.Cookies("user")("country")="canada"
Response.Cookies("user")("age")="28"
%>

Assume that your server has sent every one of the treats above to a user.

Now we need to peruse every one of the treats sent to a client. The precedent underneath shows step by step instructions to do it (note that the code beneath checks if a treat has Keys with the HasKeys property):

<!DOCTYPE html>
<html>
<body>

<%
diminish x,y
for every x in Request.Cookies
  response.write("<p>")
  on the off chance that Request.Cookies(x).HasKeys then
    for every y in Request.Cookies(x)
      response.write(x & ":" & y & "=" & Request.Cookies(x)(y))
      response.write("<br>")
    next
  else
    Response.Write(x & "=" & Request.Cookies(x) & "<br>")
  end if
  response.write "</p>"
next
%>

</body>
</html>

Output:

firstname=Duo

user:firstname=dwayan
user:lastname=jhonson
user:country=canada
user:age=28


What if a Browser Does NOT Support Cookies?

If your application manages programs that don't bolster treats, you will need to utilize different strategies to pass data starting with one page then onto the next in your application. There are two different ways of doing this:

1. Add parameters to a URL

You can add parameters to a URL:

<a href="welcome.asp?fname=dwayan&lname=jhonson">Go to Welcome Page</a>

And recover the qualities in the "welcome.asp" record like this:

<%
fname=Request.querystring("fname")
lname=Request.querystring("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>

2. Utilize a form

You can utilize a structure. The structure passes the client contribution to "welcome.asp" when the client taps on the Submit button:

<form method="post" action="welcome.asp">
First Name: <input type="text" name="fname" value="">
Last Name: <input type="text" name="lname" value="">
<input type="submit" value="Submit">
</form>

Retrieve the qualities in the "welcome.asp" document like this:

<%
fname=Request.form("fname")
lname=Request.form("lname")
response.write("<p>Hello " & fname & " " & lname & "!</p>")
response.write("<p>Welcome to my Web site!</p>")
%>


© 2018 - All Rights Reserved.

welookups.comWelookups spring