468,110 Members | 1,827 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

Cookie path dilema

36
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_ToDoItem1
My%20message%20goes%20here
www.mydomain.co.uk/carsearch/usedcars/
1600
3131308160
29921915
1549220336
29915884
*
DS1113008PT_NumToDoList
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(szName)
{
var szValue = null;
if(document.cookie) //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=JavaScript>
<!--

var szName = getCookie("DS1113008PT_ToDoItem1");
if(szName && (szName.length != 0)) document.write('<div class="cook"><center>cookie found\/center><\/div>');
// -->
</SCRIPT>
--------------------------------------------------------
Feb 29 '08 #1
3 1492
Logician
210 100+
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
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_NumToDoList');
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(ToDoItem, i);
}
}
}
function DeleteItem(Count) {
DeleteCookie('<%= orderid %><%= vendor %>PT_ToDoItem'+Count);
window.location = window.location;
}
function PrintItem (ToDoItem, Count) {
var color = "";
SwapColour = 1 - SwapColour;
if (SwapColour==1) {color = "bgcolor='#F3F3F3'"} ;
ShowCount++;
document.write("<tr " + color + ">");
document.write("<td width=10% align=center valign=top><font size=2><b>"+ShowCount);
document.write("<td width=75% align=left ><font size=2>"+ToDoItem);
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>");
}
function AddItem() {
var NumToDoItems = GetCookie('<%= orderid %><%= vendor %>PT_NumToDoList');
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_NumToDoList',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(document.cookie.substring(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.cookie.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.arguments;
var argc = SetCookie.arguments.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.toGMTString())) +
((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 256MB
The relevant lines are in the function SetCookie :

var argv= SetCookie.arguments;
var argc= SetCookie.arguments.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

Post your reply

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

Similar topics

5 posts views Thread by michel | last post: by
1 post views Thread by J. Bruce | last post: by
1 post views Thread by Lisa | last post: by
3 posts views Thread by Ben | last post: by
reply views Thread by Boyan | last post: by
6 posts views Thread by kelvlam | last post: by
5 posts views Thread by Bill Nicholson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.