Cookie always return NULL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mlevit
    New Member
    • Jun 2007
    • 30

    #1

    Cookie always return NULL

    Hi,

    I've written my own JavaScript and used a couple of codes to set and get cookies from the net I found.

    When I set the cookie I can see it in my browser, it is set with the right value and expiration date etc. The only problem I'm getting is when I retrieve the cookie the value is always NULL.

    [CODE=JavaScript]// ==UserScript==
    // @name Utopia Login/Loged
    // @namespace http://games.swirve.co m/utopia/login.htm
    // @description Logs your Utopia login
    // @include http://games.swirve.co m/utopia/login.htm
    // @include http://u1.swirve.com/enter.cgi
    // @include http://u1.swirve.com/logout.cgi?
    // ==/UserScript==

    var cookie;
    var loggedIn = false;
    var lastLoginDate;
    var lastLoginTime;

    var currentLocation = location.href;

    if (currentLocatio n == "http://games.swirve.co m/utopia/login.htm" && loggedIn == false)
    {
    cookie = readCookie("uto piaLoginTimer") ;
    window.alert("L ogin page : cookie_value: " + cookie);

    if (cookie)
    {
    window.alert("L ast login date and time: " + cookie);
    }
    }
    else if (currentLocatio n == "http://u1.swirve.com/enter.cgi")
    {
    window.alert("L ogged in");
    loggedIn = true;
    }
    else if (currentLocatio n == "http://u1.swirve.com/logout.cgi?")
    {
    window.alert("L ogged out");
    loggedIn = false;

    var date = new Date();
    date.setTime(da te.getTime()+(1 *24*60*60*1000) );

    var expires = date.toGMTStrin g();
    createCookie("u topiaLoginTimer ", "test_something ", 5);
    }

    function createCookie(na me,value,days)
    {
    if (days)
    {
    var date = new Date();
    date.setTime(da te.getTime()+(d ays*24*60*60*10 00));
    var expires = "; expires="+date. toGMTString();
    }

    else var expires = "";
    document.cookie = name+"="+value+ expires+"; path=/";

    window.alert("C ookie created : value : " + value);
    }

    function readCookie(name )
    {
    var nameEQ = name + "=";
    var ca = document.cookie .split(';');
    for(var i=0;i < ca.length;i++)
    {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c .length);
    if (c.indexOf(name EQ) == 0) return c.substring(nam eEQ.length,c.le ngth);
    }
    return null;
    }

    function eraseCookie(nam e)
    {
    createCookie(na me,"",-1);
    }[/CODE]

    This is a small script that I'm using with GreaseMonkey for Firefox.

    All the If statements work fine and every gets set and displayed but the cookie value is always returned as NULL?

    Thanks

    P.S. How do I get my current system time, I am GMT+10, the date that comes up in the windows.alert is tomorrows date and 1PM whereas I am at 12AM now.
  • mlevit
    New Member
    • Jun 2007
    • 30

    #2
    Just found out, the code works on:

    http://games.swirve.co m/utopia/login.htm

    but not on:

    http://u1.swirve.com/logout.cgi?

    Anyone know the reason why this might be?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by mlevit
      Just found out, the code works on:

      http://games.swirve.co m/utopia/login.htm

      but not on:

      http://u1.swirve.com/logout.cgi?

      Anyone know the reason why this might be?

      I think this "http://u1.swirve.com/logout.cgi?" will be "http://u1.swirve.com/logout.cgi"
      Simply do remove the "?" then see what happens.

      Debasis Jana

      Comment

      Working...