Cookie Help

Hi everyone!

Got a quick (cookie) question.
I looked on the internet for some cookie scripts (redirect ones), but unfortunately haven’t been too lucky with these.

What I want to do is the following:

When the cookie is not found it goes to my main page, for example:
www.anynamehere.com
On the main page I can select where I want to go -
‘Contacts’, ‘News’, ‘Forums’ and so on. The main page has a drop down list with these options, each option has it’s own link.

When I click on ‘continue’ and go to the specific link (ex. www.anynamehere.com/contacts.html) it should remember my selection, so next time I log into www.anynamehere.com it’s automatically will take me to contacts.html. I will not see the main page anymore.

Now, if from the contacts page I select ‘News’ and go to www.anynamehere.com/news.html it has to remember that link as well. So if I close my browser and then reopen it, it should take me to www.anyname.com/news.html.

I hope that makes sense.

If anyone can give me any pointers I would greatly appreciate it. Maybe there is a script like that available online, I just wasn’t lucky enough to find one.

Thank you in advance!

with php it would be as simple as:

$_COOKIE[“name”] = “/page/location.html”;
if($_COOKIE[‘name’]){
header("location: ".$_COOKIE[‘name’]);
}else{ header(“location: /”);}

but the fact you use html makes it a bit harder, and jquery would be the best option (the number of devices without java enabled is amazing)

after reading http://css-tricks.com/forums/discussion/6114/redirecting-to-a-splash-page-but-only-once-cookies/p1

you may have to stick with php, and use .htaccess to rewrite your urls to accomadate the .html extension, or configure apache somehow to parse php inside of an html page

Thank you James,
I’ll look into it.

If you do handle cookies from within JavaScript, these are the cookie handling functions that best do the job.

Here’s a very rough prototype I adapted from my book “JavaScript: Just the Basics.” When you load Test.html, it looks for the FormerLocation cookie. If it finds it, it automatically navigates to that page. Test.html has a link to Test01.html. If you go to Test01.html, the script saves the current URL in the FormerLocation cookie. It saves the cookie for 1 hour, but you can change the lifetime to anything you want.
Test.html:


<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>

<script>

var FormerLocation = getFormerLocationCookie ();

if (FormerLocation)
   window.location = FormerLocation;

function getFormerLocationCookie ()
  {
  /* get all cookies: */
  var allCookies = document.cookie;

  if (allCookies === "")
    return null;

  /* break up all-cookie string into an array: */
  var cookieList = allCookies.split ("; ");

  /* search through the array for the FormerLocation cookie: */
  for (var i = 0; i < cookieList.length; ++i)
    {
    var cookie = cookieList[i];
    var idx = cookie.indexOf ("=");
    /* extract cookie name: */
    var name = cookie.substring (0, idx);
    if (name === "FormerLocation")
      {
      /* correct cookie found: extract, decode, & return cookie value: */
      var value = cookie.substring (idx+1);
      return decodeURIComponent (value);
      }
    }
  return null;
  }

</script>

</head>
<body>
<h2>Home Page</h2>

<a href="Test01.html">Go to Test01</a>

</body>
</html>

Test01.html:


<!DOCTYPE html>
<html>
<head>
<title>JavaScript Tester Page</title>

<script>

/* for IE only: */
var date = new Date();
date.setHours (date.getHours()+1);

document.cookie = "FormerLocation=" + encodeURIComponent (window.location) + "; max-age=3600; expires=" + date.toGMTString();

</script>

</head>
<body>
<h2>JavaScript Tester Page</h2>
</body>
</html>

I’d say there’s room for improvement there, not least the encoding/decoding of the stored value.