Solved

Create persistent Cookie on client

Posted on 2001-07-18
3
403 Views
Last Modified: 2008-02-01
Hi All!
How can I create persistent cookie on client. I work with ASP.
 Please send me some code that explains it.
0
Comment
Question by:LMikl
3 Comments
 
LVL 8

Accepted Solution

by:
drittich earned 30 total points
ID: 6293660
Here is some info:

What is a Cookie?
A cookie is a small file that the server embeds in a user's browser. The cookie is used to identify the user. Each time the same browser asks for a page, it sends the cookie too. ASP scripts can both get and set the values of cookies.


--------------------------------------------------------------------------------

Set Cookies
To set a cookie value, use the "Response.Cookies" command. If the cookie does not exist, it will be created, and take the value that is specified.

In the example below, we send a cookie named "userid" with a value of "25" to the browser. This cookie only lasts during the current user session. This command must appear before the <html> tag:

<%
Response.Cookies("userid")=25
%>
 

If you want to identify a user after the user has stopped and restarted the browser, you must use the "Expires" attribute for "Response.Cookies" and set the date to some date in the future:

<%
Response.Cookies("userid")=25
Response.Cookies("userid").Expires="May 10, 2002"
%>
 


--------------------------------------------------------------------------------

Get Cookies
To get a cookie value, use the "Request.Cookies" command.

In the example below, we want to retrieve the value of the "userid" cookie. We retrieve 25:

<%
Response.Write(Request.Cookies("userid"))
%>
 


--------------------------------------------------------------------------------

Set Cookie Paths
Each cookie stored on the browser contains path information. When the browser requests a file stored in the same location as the path specified in the cookie, the browser sends the cookie to the server.

By default the path of the cookie is set to the name of the application that contains the file that created the cookie. If a file, in an application called "userapp", creates a cookie, then each time a user's browser gets any file in that application, the browser will send the cookie.

To specify a path for a cookie, you can use the "Path" attribute for "Response.Cookies".

The example below assigns the path Sales/Customer/Profiles/ to a cookie called Zip:

<%
Response.Cookies("Zip")="12"
Response.Cookies("Zip").Expires="January 1, 2001"
Response.Cookies("Zip").Path="/Sales/Customer/Profiles/"
%>
 

Note: Make sure all addresses to .asp files have the same case to ensure that the browser sends stored cookies.

The example below sets the cookie path so that the user's browser will send a cookie whenever the browser requests a file from your server, regardless of application or path:

<%
Response.Cookies("Zip").Path="/"
%>
 


--------------------------------------------------------------------------------

No Cookie Support
Not all browsers support cookies. If your application is in contact with browsers that do not support cookies, you can not use cookies. You will then have to use other methods to pass information from page to page in your application.  

There are two ways to do this:

Add Parameters to a URL
Add parameters to a query string, like this:

http://www.w3schools.com/asp/greeting.asp?userid=Hege
 

Add Hidden Controls to a Form
Add hidden controls to a form.

The example below contains a hidden control. The form passes a user id value, in addition to the rest of the information:

<form method="post" action="greeting.asp">
First Name:  <input type="text" name="fname" value="">
Last Name: <input type="text" name="lname" value="">
<input type="hidden" name="userid" value="Hege">
<input type="submit" value="Send">
</form>
 
from: http://www.w3schools.com/asp/asp_cookies.asp
0
 
LVL 7

Expert Comment

by:John844
ID: 6294139
this is the portion of text that describes what you need.
drittich provided it... he should get the points.

If you want to identify a user after the user has stopped and restarted the browser, you must use the
"Expires" attribute for "Response.Cookies" and set the date to some date in the future:

<%
Response.Cookies("userid")=25
Response.Cookies("userid").Expires="May 10, 2002"
%>



--------------------------------------------------------------------------------

Get Cookies
To get a cookie value, use the "Request.Cookies" command.

In the example below, we want to retrieve the value of the "userid" cookie. We retrieve 25:

<%
Response.Write(Request.Cookies("userid"))
%>

0
 
LVL 33

Expert Comment

by:hongjun
ID: 6294183
<%
' set cookie
Response.Cookies("cookie_name") = "cookie_value"
' cookie expires only after 365 days later
Response.Cookies("cookie_name").Expires = Now() + 365

' write cookie value onto screen
Response.Write Request.Cookies("cookie_name")
%>

hongjun
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now