cookies... perl.. javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lisa

    cookies... perl.. javascript

    Can anyone tell me why the cookie created by this javascript...

    <script language=javasc ript type="text/javascript">
    <!--
    function SetCookie(usern ame, value, expires, path, domain)
    { document.cookie = username + "=" + escape(value) +
    ((expires == null) ? "" : "; expires=" + expires.toGMTSt ring()) +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain);
    }
    var expiration = new Date();
    expiration.setT ime(expiration. getTime() + 60000);
    SetCookie('user name', 'Peter', expiration);
    // -->
    </script>

    is not seen by this perl script?

    #!/usr/local/bin/perl
    use CGI;
    $q = new CGI;
    print $q->header;
    $cookie_in = $q->cookie("userna me");
    if($cookie_in)
    {
    print $cookie_in;
    }
    else
    {
    print "Can't find cookie\n";
    }

    -Lisa.
  • Thomas 'PointedEars' Lahn

    #2
    Re: cookies... perl.. javascript

    Lisa wrote:
    [color=blue]
    > Can anyone tell me why the cookie created by this javascript...
    >
    > <script language=javasc ript type="text/javascript">
    > <!--
    > function SetCookie(usern ame, value, expires, path, domain)
    > { document.cookie = username + "=" + escape(value) +
    > ((expires == null) ? "" : "; expires=" + expires.toGMTSt ring()) +
    > ((path == null) ? "" : "; path=" + path) +
    > ((domain == null) ? "" : "; domain=" + domain);
    > }
    > var expiration = new Date();
    > expiration.setT ime(expiration. getTime() + 60000);
    > SetCookie('user name', 'Peter', expiration);[/color]
    ^^^[color=blue]
    > // -->
    > </script>
    >
    > is not seen by this perl script?[/color]

    When a named argument of a function is not provided, its
    value is not `null' (since that represents a null, empty,
    or non-existent reference) but `undefined'. So you set the
    cookie's `path' and `domain' to `undefined' as you do not
    provide those arguments. And a site cannot read the cookies
    not of its domain set which explains why your Perl script
    fails.

    In boolean expressions, `undefined' evaluates to `false',
    so you can use the following:

    function SetCookie(usern ame, value, expires, path, domain)
    {
    document.cookie =
    username + "=" + escape(value)
    + (expires
    ? ""
    : "; expires=" + expires.toGMTSt ring())
    + (path
    ? ""
    : "; path=" + path)
    + (domain
    ? ""
    : "; domain=" + domain);
    }



    HTH

    PointedEars

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: cookies... perl.. javascript

      Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
      [color=blue]
      > Lisa wrote:[/color]
      [color=blue][color=green]
      > > function SetCookie(usern ame, value, expires, path, domain)
      > > { document.cookie = username + "=" + escape(value) +
      > > ((expires == null) ? "" : "; expires=" + expires.toGMTSt ring()) +[/color][/color]
      ....[color=blue]
      >
      > When a named argument of a function is not provided, its
      > value is not `null' (since that represents a null, empty,
      > or non-existent reference) but `undefined'. So you set the
      > cookie's `path' and `domain' to `undefined' as you do not
      > provide those arguments.[/color]

      However, since Lisa uses "==" to compare, it still works, since
      type conversion makes:
      (undefined == null)
      true.

      (but yes, just using "expires" in the condition is sufficient)

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...