Help With Detecting Multiple Cookies using one script

He has: 57 posts

Joined: Dec 2004

On my website I am trying to get one script to check for up to 3 possible cookies that may be present on a visitors computer as they attempt to enter the site from one of three pages, and upon finding any one of the three cookies, it then allows the visitor through to the requested page.

I have attempted to get the script to set the same cookie (by name) on all 3 pages but it doesn't work because the cookies are automatically set using the name of the folder where the html document is located. So when the browser attempts to located the cookie it can't locate it because it already has a different name even though I'm trying to force the cookie to be set with a name of MY choosing.

I've also attempted to solve that issue by placing the entire script twice in the head portion of the webpage, and getting each script to check for it's own cookie (of a different name). That didn't work either. (okay so I'm a novice)

Here's the script I'm trying to use:

<script language="javascript" type="text/javascript">
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();
}

var favorite = GetCookie('secular');

if (favorite != null) {
switch (favorite) {
case 'yes' : url = 'homepage.html'; // change these!
break;
}
window.location.href = url;
}
// End -->
</script>

Also, I was wondering if it is possible to get the following code

var favorite = GetCookie('secular');

to check for one of 3 cookies at one time. I've tried using a variety of changes such as the following with no luck:

var favorite = GetCookie('secular, stress_release');
var favorite = GetCookie(' "secular", "stress_release" ');
var favorite = GetCookie(' "secular, stress_release" ');
var favorite = GetCookie('secular', 'stress_release');

Any suggestions would be appreciated.

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi,

Maybe I haven't been awake long enough (or haven't had enough espresso). Any rate, i don't see where you are looking for anything other than the default cookie. Try creating the cookieName & then looking for it:

var cookieNames = new Array('cookie1', 'cookie2', 'cookie3');
function getName()
{
    if(document.cookie)  // there are cookies
   {
       for (var i = 0; i < cookieNames.length; i++)
       {
          index = document.cookie.indexOf(cookieNames[i]);
          if (index != -1)  // we found our cookie
          {
              namestart = (document.cookie.indexOf("=", index) + 1);
              nameend = document.cookie.indexOf(";", index);
              if (nameend == -1)
                  nameend = document.cookie.length;
          
              return(document.cookie.substring(namestart, nameend));
        }
     }
  }
}
'

Where the world once stood
the blades of grass cut me still

He has: 57 posts

Joined: Dec 2004

Hi again,

Thanks for your input. I should have told you earlier that I'm actually trying to change the code that I posted. That's why it doesn't appear to be looking for anything other than the default cookie. I'm probably not explaining it well enough so I'll try one more time.

I have a site, and when people enter the url and press enter they go to the "index.html" file as in the following http://www.mysite.com/index.html. Once they get there they have to agree to some terms and conditions and when they do they get my cookie, and are then forwarded to the homepage.html file. Each time they come back to the site they automatically arrive at the homepage.html page because the cookie allows them to be redirected (but you knew that already).

The problem is......when people enter my site through any one of the OTHER pages on the site they don't get the "Terms and Conditions" page....so in essence....they skip the agreement stage. This is bad. I want all of the pages on the site to check for the cookie, and if it's not present then they are redirected to the index.html page which right now, is the ONLY page that produces a cookie. However, after they AGREE and the cookie is accepted, they automatically arrive at the page they we're trying to access (which is not always the homepage).

I hope I'm not being to much of a pain here, but I'm kinda stumped on this. Once again, thanks for any suggestions.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.