Solved

Create persistent Cookie on client

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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