Solved

Create persistent Cookie on client

Posted on 2001-07-18
3
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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 is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

695 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