473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1758
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

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

Similar topics

5
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...
1
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...
1
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,...
3
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...
0
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...
0
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...
6
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...
1
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...
5
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.