473,857 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cookie path dilema

36 New Member
Hi Guys

I hope this is an easy one for you.

Basically I have a page on my website that creates a javascript based cookie on the users pc. The contents of the cookie when created looks as follows:

------------------------------
DS1113008PT_ToD oItem1
My%20message%20 goes%20here
www.mydomain.co .uk/carsearch/usedcars/
1600
3131308160
29921915
1549220336
29915884
*
DS1113008PT_Num ToDoList
1
www.mydomain.co .uk/carsearch/usedcars/
1600
3131308160
29921915
1549220336
29915884
*
-------------------

I am basically, looking to for a script which will detect this cookie on the users machine. If the cookie is detected then I would simply like my asp page to display the word "cookie found". If the cookie cannot be found on their machine then I would simply like no wording.

Easy you say? I know there are many cookie scripts out there that should allow me to do this and believe me, after 4 hours straight, I believe I have just about trawled thought them all.

The problem I'm having is this. If I the page that creates the cookie is located in a certain directory on my website and the page I use to detect the cookie is in another directory it doesn't detect it. However, when I create the cookie in the same directory as the page that detects it then it all works fine. I therefore need a detection script that will work in any directory of my website no matter what director the cookie was created in. is this possible?.

The script I am currently using to detect my cookie can be seen below.

If anyone can help then it would be fully appreciated

best regards

Rod from the UK

-------------DETECTION SCRIPT--------------
<script>

function getCookie(szNam e)
{
var szValue = null;
if(document.coo kie) //only if exists
{
var arr = document.cookie .split((escape( szName) + '='));
if(2 <= arr.length)
{
var arr2 = arr[1].split(';');
szValue = unescape(arr2[0]);
}
}
return szValue;
}

</script>

<SCRIPT language=JavaSc ript>
<!--

var szName = getCookie("DS11 13008PT_ToDoIte m1");
if(szName && (szName.length != 0)) document.write( '<div class="cook"><c enter>cookie found\/center><\/div>');
// -->
</SCRIPT>
--------------------------------------------------------
Feb 29 '08 #1
3 1792
Logician
210 New Member
The problem I'm having is this. If I the page that creates the cookie is located in a certain directory on my website and the page I use to detect the cookie is in another directory it doesn't detect it. However, when I create the cookie in the same directory as the page that detects it then it all works fine. I therefore need a detection script that will work in any directory of my website no matter what director the cookie was created in. is this possible?.
When a cookie is created, the path parameter determines the highest level at which the cookie can be read. Try setting the path to '/'.
Feb 29 '08 #2
rjoseph
36 New Member
Hi lOGICIAN

Thank you so much for your reply.

The script that creates the cookie is rather complex for me to alter. I basically adapted it from a free javascript resource on the web. I am not too sure what (and where) I need to add the PATH element to it so that it does what you suggest. I can quite easily add the "/" when creating the cookie but the problems lies when the function needs to delete it. Would it be possible for you to take a look at my script? If so then that would be great. I have inlcluded it below. It basically stores, retrieves and deletes the cookie.

I look forward to hearing from you

best regards

Rod from the UK

------------------------
<script>
var expDays = 30;
var exp = new Date();
exp.setTime(exp .getTime() + (expDays*24*60* 60*1000));
var ShowCount = 0;
var SwapColour;
function ListToDoItems() {
var NumToDoItems = GetCookie('<%= orderid %><%= vendor %>PT_NumToDoLis t');
var i;
var ToDoItem;
if (NumToDoItems == null) {
NumToDoItems = 0;
}
ShowCount = 0; SwapColour = 0;
for (i=1; i <= NumToDoItems; i++) {
ToDoItem = GetCookie('<%= orderid %><%= vendor %>PT_ToDoItem'+ i);
if (ToDoItem != null) {
PrintItem(ToDoI tem, i);
}
}
}
function DeleteItem(Coun t) {
DeleteCookie('< %= orderid %><%= vendor %>PT_ToDoItem'+ Count);
window.location = window.location ;
}
function PrintItem (ToDoItem, Count) {
var color = "";
SwapColour = 1 - SwapColour;
if (SwapColour==1) {color = "bgcolor='#F3F3 F3'"} ;
ShowCount++;
document.write( "<tr " + color + ">");
document.write( "<td width=10% align=center valign=top><fon t size=2><b>"+Sho wCount);
document.write( "<td width=75% align=left ><font size=2>"+ToDoIt em);
document.write( "<td width=15% align=center><f ont size=2 color=#333333>" +"<a href='javascrip t:DeleteItem(" + Count + ")'><font size=2 color=#333333>< u>Delete</u></font></a>");
}
function AddItem() {
var NumToDoItems = GetCookie('<%= orderid %><%= vendor %>PT_NumToDoLis t');
var i;
var ToDoItem;
if (NumToDoItems == null) {
NumToDoItems = 0;
}
ToDoItem = prompt("Add your note about this £<%= price %> - <%= Make %> - <%= Model %> here:");
if ((ToDoItem != null) && (ToDoItem != "Undefined" )) {
NumToDoItems++;
SetCookie('<%= orderid %><%= vendor %>PT_ToDoItem'+ NumToDoItems, ToDoItem, exp);
SetCookie('<%= orderid %><%= vendor %>PT_NumToDoLis t',NumToDoItems , exp);
window.location = window.location ;
}
}
function set() {
VisitorName = prompt("Who are you?");
SetCookie ('VisitorName', VisitorName, exp);
SetCookie ('WWHCount', 0, exp);
SetCookie ('WWhenH', 0, exp);
}
function getCookieVal (offset) {
var endstr = document.cookie .indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie .length;
return unescape(docume nt.cookie.subst ring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie .length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cooki e.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie .indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.argum ents;
var argc = SetCookie.argum ents.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTSt ring())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString ();
}

</script>
------------------------
Feb 29 '08 #3
mrhoo
428 Contributor
The relevant lines are in the function SetCookie :

var argv= SetCookie.argum ents;
var argc= SetCookie.argum ents.length
var path= (argc > 3) ? argv[3] : null;
The most arguments sent in this script to setCookie are three,
which makes an argv[3] undefined, so the path set is to whatever page the script runs on- too bad. You can only delete them from the same path.

If you specify the path, you can delete it from anywhere on the site, but I'll leave it up to you- find the places that calls setCookie, and add the path as the fourth argument, unless expires (3rd argument) is also not set.


It is easier to look at code that has been white space formatted.
I save mine without whitespace, but I have a routine that puts in in when I want to look at the source code. So here:
Expand|Select|Wrap|Line Numbers
  1. var expDays= 30;
  2. var exp= new Date();
  3. exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
  4. var ShowCount= 0;
  5. var SwapColour;
  6. function ListToDoItems() {
  7.     var NumToDoItems= GetCookie('<%= orderid %><%= vendor %>PT_NumToDoList');
  8.     var i;
  9.     var ToDoItem;
  10.     if (NumToDoItems== null) {
  11.         NumToDoItems= 0;
  12.     }
  13.     ShowCount= 0; SwapColour= 0;
  14.     for (i= 1; i <= NumToDoItems; i++) {
  15.         ToDoItem= GetCookie('<%= orderid %><%= vendor %>PT_ToDoItem'+i);
  16.         if (ToDoItem != null) {
  17.             PrintItem(ToDoItem, i);
  18.         }
  19.     }
  20. }
  21.  
  22. function DeleteItem(Count) {
  23.     DeleteCookie('<%= orderid %><%= vendor %>PT_ToDoItem'+Count);
  24.     window.location= window.location;
  25. }
  26.  
  27. function PrintItem (ToDoItem, Count) {
  28.     var color= "";
  29.     SwapColour= 1 - SwapColour;
  30.     if (SwapColour== 1) {color= "bgcolor='#F3F3F3'"} ;
  31.     ShowCount++;
  32.     document.write("<tr " + color + ">");
  33.     document.write("<td width=10% align=center valign=top><font size=2><b>"+ShowCount);
  34.     document.write("<td width=75% align=left ><font size=2>"+ToDoItem);
  35.     document.write("<td width=15% align=center><font size=2 color=#333333>"+"<a href='javascript:DeleteItem(" + Count + ")'><font size=2 color=#333333><u>Delete</u></font></a>");
  36. }
  37.  
  38. function AddItem() {
  39.     var NumToDoItems= GetCookie('<%= orderid %><%= vendor %>PT_NumToDoList');
  40.     var i;
  41.     var ToDoItem;
  42.     if (NumToDoItems== null) {
  43.         NumToDoItems= 0;
  44.     }
  45.     ToDoItem= prompt("Add your note about this £<%= price %> - <%= Make %> - <%= Model %> here:");
  46.     if ((ToDoItem != null) && (ToDoItem != "Undefined" )) {
  47.         NumToDoItems++;
  48.         SetCookie('<%= orderid %><%= vendor %>PT_ToDoItem'+NumToDoItems, ToDoItem, exp);
  49.         SetCookie('<%= orderid %><%= vendor %>PT_NumToDoList',NumToDoItems, exp);
  50.         window.location= window.location;
  51.     }
  52. }
  53.  
  54. function set() {
  55.     VisitorName= prompt("Who are you?");
  56.     SetCookie ('VisitorName', VisitorName, exp);
  57.     SetCookie ('WWHCount', 0, exp);
  58.     SetCookie ('WWhenH', 0, exp);
  59. }
  60.  
  61. function getCookieVal (offset) {
  62.     var endstr= document.cookie.indexOf (";", offset);
  63.     if (endstr== -1)
  64.     endstr= document.cookie.length;
  65.     return unescape(document.cookie.substring(offset, endstr));
  66. }
  67.  
  68. function GetCookie (name) {
  69.     var arg= name + "=";
  70.     var alen= arg.length;
  71.     var clen= document.cookie.length;
  72.     var i= 0;
  73.     while (i < clen) {
  74.         var j= i + alen;
  75.         if (document.cookie.substring(i, j)== arg)
  76.         return getCookieVal (j);
  77.         i= document.cookie.indexOf(" ", i) + 1;
  78.         if (i== 0) break;
  79.     }
  80.     return null;
  81. }
  82.  
  83. function SetCookie (name, value) {
  84.     var argv= SetCookie.arguments;
  85.     var argc= SetCookie.arguments.length;
  86.     var expires= (argc > 2) ? argv[2] : null;
  87.     var path= (argc > 3) ? argv[3] : null;
  88.     var domain= (argc > 4) ? argv[4] : null;
  89.     var secure= (argc > 5) ? argv[5] : false;
  90.     document.cookie= name + "=" + escape (value) +
  91.     ((expires== null) ? "" : ("; expires=" + expires.toGMTString())) +
  92.     ((path== null) ? "" : ("; path=" + path)) +
  93.     ((domain== null) ? "" : ("; domain=" + domain)) +
  94.     ((secure== true) ? "; secure" : "");
  95. }
  96.  
  97. function DeleteCookie (name) {
  98.     var exp= new Date();
  99.     exp.setTime (exp.getTime() - 1);
  100.     var cval= GetCookie (name);
  101.     document.cookie= name + "=" + cval + "; expires=" + exp.toGMTString();
  102. }
Mar 1 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
8937
by: michel | last post by:
Hi all, A simple problem that requires a simple solution and I should be able and read php.net.... but can someone please help me out for a sec? I need to store a cookie and read it from a different path. using: setcookie('pros','$data','/');
1
2359
by: J. Bruce | last post by:
Does anyone know how to set the path in the script below so that the cookie is accessible to all directories? TIA <SCRIPT LANGUAGE="JavaScript"> <!-- Begin var expDays = 30; var exp = new Date();
1
2118
by: Lisa | last post by:
how would i set the path to my cgi-bin directory in this code..? Lisa. <html> <body> <script language=javascript type="text/javascript"> <!-- function SetCookie(name, value, expires, path, domain) { document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expires.toGMTString()) + ((path == null) ? "" : "; path=" + path) +
3
2065
by: Ben | last post by:
I'm having problems with cookies from asp.net to asp back to asp.net. It seems like I can set a cookie in asp.net fine, and alter it at will, as soon as asp touches it, asp.net won't have anything to do with it. Can someone please help! The code below, going from aspx to aspx, works great the cookie as expected goes from qwerty to zxcvb and back. As soon as you hit the asp page, the cookie goes to asdfg and stays there no matter how...
0
1189
by: Boyan | last post by:
I set the Path Property of cookie to some value like "/Appcation1/folder1". After reloading the page and read the cookie from Request.Cookies, the path value turns out to be "/". I found no matter what value I set on Path cannot be retrieved for reading. Could anyone tell me the reason? Thanks in advance.
0
2527
by: svilst | last post by:
How can I change the path of the cookie 'ASP.NET_SessionId'? The path is "/" and I'd like to set the path to particular virtual directory. The following approach does not work: protected void Session_Start(Object sender, EventArgs e) { HttpCookie cookie = Response.Cookies; if (cookie != null)
6
2709
by: kelvlam | last post by:
Hello all, I'm still a bit new with JavaScript, and I hope the guru here can shed some light for me. It's regarding handling cookie and the case-sensitive nature of JavaScript itself. My problem is how do I handle the "path" parameter in cookie. First, the sequence start at http://www.testServer1.com/TestApp/page1.htm, and a cookie is set at
1
6243
by: ken | last post by:
How to remove specified cookie (via a given name) in cookie jar? I have the following code, but how can I remove a specified cookie in the cookie jar? cj = cookielib.LWPCookieJar() if cj is not None: if os.path.isfile(COOKIEFILE): print 'Loading Cookie--------------' cj.load(COOKIEFILE)
5
2306
by: Bill Nicholson | last post by:
Maybe I don't understand cookies correctly. When some visits my site and logs in, they get a cookie that stores their login information. When they come back, that info is read from the cookie and they can log in without having to remember their id/pw. Assume the site is www.foobar.com It works as long as they browse to www.foobar.com. If they browse to foobar.com it doesn't see the cookie. It they browse to www.foobar.com/Stuff.aspx, it...
0
9768
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10711
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9549
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7940
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5774
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5971
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4191
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.