Solved

Create persistent Cookie on client

Posted on 2001-07-18
3
407 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update Stored Procedure question 8 48
Syntax Help on SP 4 57
How to hide jquery hemiintro in returning user? 1 68
Generate report pulling data (links) from three tables 31 59
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…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

21 Experts available now in Live!

Get 1:1 Help Now