?
Solved

Create persistent Cookie on client

Posted on 2001-07-18
3
Medium Priority
?
426 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 120 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 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