ASP Cookies Collection

Request ఆబ్జెక్ట్ పరిచయపు పుస్తకం

The Cookies collection is used to set or get the value of a cookie. If the cookie does not exist, it is created and assigned the specified value.

Note:The Response.Cookies command must be placed before the <html> tag.

Syntax:

Response.Cookies(name)[(key)|.attribute]=value
variablename=Request.Cookies(name)[(key)|.attribute]
Parameters Description
name Required. The name of the cookie.
value Required (for the Response.Cookies command). The value of the cookie.
attribute

Optional. Specifies information about the cookie. It can be one of the following parameters.

  • Domain - Write-only; the cookie is sent only to requests that reach this domain.
  • Expires - Write-only; the expiration date of the cookie. If no date is specified, the cookie will expire at the end of the session.
  • HasKeys - Read-only; specifies whether the cookie has keys (this is the only property that can be used with the Request.Cookies command)
  • Path - Write-only; if set, the cookie is sent only to requests that reach this path. If not set, the application's path is used.
  • Secure - Write-only; indicates whether the cookie is secure.
key Optional. Specifies the key where the value is assigned.

Example

"Response.Cookies" command is used to create a cookie or set the value of a cookie:

<body>
Response.Cookies("firstname")="Alex"
%>

In the above code, we created a cookie named "firstname" and assigned it the value alex.

Also, you can set attributes for cookies, such as setting the expiration time of the cookie:

<body>
Response.Cookies("firstname")="Alex" 
రెస్పాంస్.కూకీస్("ఫస్ట్నేమ్")="Alex"
%>

రెస్పాంస్.కూకీస్("ఫస్ట్నేమ్").ఎక్స్పైర్స్=#మే 10,2002#

ప్రస్తుతం, "ఫస్ట్నేమ్" కూకీ యొక్క విలువ అలెక్స్ ఉంది మరియు అది వినియోగదారు కంప్యూటర్లో అవలంబించబడుతుంది తేదీ 2002 మే 10 న.

"రెక్వెస్ట్.కూకీస్" ఆదేశం కూకీ యొక్క విలువను తీసుకునేందుకు ఉపయోగిస్తారు.

<body>
ఈ ఉదాహరణలో, మేము "ఫస్ట్నేమ్" కూకీ యొక్క విలువను తీసుకుని అది పేజీలో చూపిస్తాము:
ఫ్రేమ్=రెక్వెస్ట్.కూకీస్("ఫస్ట్నేమ్")
%>

అవుట్‌పుట్‌లు:

రెస్పాంస్.వ్రాయించు("ఫస్ట్నేమ్=" & ఫ్రేమ్)

ఫస్ట్నేమ్=అలెక్స్

ఒక కూకీ యొక్క బహుళ విలువల సమాహారాన్ని కలిగి ఉండవచ్చు. దీనిని కూకీ కు కీ అని పిలుస్తారు.

<body>
ఈ ఉదాహరణలో, మేము "user" పేరుతో కూకీ సమాహారాన్ని సృష్టించాము. "user" కూకీ యొక్క కీలు యొక్క సమాహారం యొక్క సమాచారం కలిగి ఉంటుంది.
రెస్పాంస్.కూకీస్("యూజర్")("ఫస్ట్ నేమ్")="John"
రెస్పాంస్.కూకీస్("యూజర్")("ల్యాస్ట్ నేమ్")="Adams"
రెస్పాంస్.కూకీస్("యూజర్")("కంట్రీ")="UK"
%>

రెస్పాంస్.కూకీస్("యూజర్")("ఏజ్")="25"

ఈ కోడ్ సర్వర్ యొక్క వినియోగదారుకు పంపిన అన్ని కూకీలను చదివించవచ్చు. కూకీ కు కీ ఉన్నారా అని నిర్ణయించడానికి మేము HasKeys అనే అంశాన్ని వాడాము:
<html>
<body>
<%
డిమ్ x, y
  ఫోర్ చేయుము యొక్క సమాంతరంలో యొక్క x రెక్వెస్ట్.కూకీస్
  రెస్పాంస్.వ్రాయించు("<p>")
    ఇఫ్ రెక్వెస్ట్.కూకీస్(x).హాస్ కీస్ అని
      ఫోర్ చేయుము యొక్క సమాంతరంలో యొక్క y రెక్వెస్ట్.కూకీస్(x)
      రెస్పాంస్.వ్రాయించు("<br />")
    తరువాత
  లేకపోతే
    రెస్పాంస్.వ్రాయించు(x & "=" & రెక్వెస్ట్.కూకీస్(x) & "<br />")
  ఇఫ్ ఎండ్
  response.write "</p>"
తరువాత
%>
</body>
</html>
%>

అవుట్‌పుట్‌లు:

firstname=Alex
user:firstname=John
user:lastname=Adams
user:
country=UK
user:
age=25

Request ఆబ్జెక్ట్ పరిచయపు పుస్తకం