How to share a cookie across domains
May 2003
The Problem: Student Center
cookies are not accessible across different domains (and therefore
important information will not appear in the browser window).
The Cause: Web pages and applications in the
same domain can share cookies, but a web page or application in
another domain cannot access them.
The Solution: The technique described below
shows how a Webmaster can enable cooperating applications to share
cookies across domains and thereby making sure all needed information
gets displayed in the browser. This discussion assumes the reader
is familiar with Active Server Pages and JavaScript.
In this example, assume two web domains, domain1 and domain2.
Each domain can be anywhere on the web. Assume domain1 has created
a cookie named cookiename and we want a page, testpage.htm, in
domain2 to read that cookie.
In testpage.htm in domain2, include the following lines:
<script language='javascript' src='http://domain1/cookietest.asp'>
</script>
In domain1, create cookietest.asp, with the following code:
<% strCookie = Request.Cookies("cookiename") Response.Write
"document.cookie = 'cookiename='" & strCookie & "';" %>
What is happening here? When testpage.htm
runs, the JavaScript code tells the browser to include some JavaScript
code from another file. This is usually a file with the extension
.js. We, however, tell the browser to get the JavaScript code
form cookietest.asp.
Cookietest.asp has access to the cookies belonging to domain1,
and it reads the value of the cookiename cookie. It then sends
back to testpage.htm a line of JavaScript that saves that cookie,
but the saver is testpage.htm, thus the cookie is now saved by
the browser for domain2.
For example, to access the Personal Registrar person-name cookie
from another domain, you would create the ASP in the PR virtual
directory and replace cookiename with PN.
Want to Know More? For more information about
the Pathlore LMS Student Center, refer to the
"Introduction" chapter in your Pathlore Design Center
manual. Keep in mind this technical tip applies to the current
shipping release and may or may not work on earlier releases.
More Tips and Solutions Await You. We invite
you to visit the Pathlore Knowledge Base and the Online Support
Site featuring convenient 24x7 access at support.pathlore.com.
|