#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Ireland
    Posts
    16
    Rep Power
    0

    Question Perl/JavaScript/Cookies


    Hi,

    I have just quickly written a cookie creator in perl

    all it does is accept a username/password (uid/password) from a html form it then checks if the login details are correct, and if so it will create a cookie.

    It seems to set the cookie okay, because it shows up in my cookies on my harddrive, but when I do a getCookie in a html page, it returns null. I'm really stuck now.

    Can somebody explain to me where I'm going wrong in my thinking? A simple example of how getCookie should be working would be great.


    Here are the 3 relevant files

    1. the form -> http://ards.csn.ul.ie/testCGI.html

    -----------------
    2. the CGI code.....

    #!/usr/bin/perl

    print "Content-type:text/html\n\n";

    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split (/&/, $buffer);
    foreach $pair (@pairs)
    {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack("c",hex($1))/ge;
    $value =~ s/\n/ /g; #Deal with newline characters
    $FORM{$name} = $value;
    }
    $uid = $FORM{'uid'};
    $password = $FORM{'password'};


    print <<HTMLHead;
    <html>
    <head>
    <title>Testing Cookies</title>
    <script language="JavaScript">

    <!-- hide
    function setCookie(NameOfCookie, value, expiredays) {
    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 *
    1000));
    document.cookie = NameOfCookie + "=" + escape(value) +
    ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
    }
    // -->
    </script>
    </head>
    <body>
    HTMLHead
    ;

    if($uid ne "john" || $password ne "password"){
    print "wrong username/password";
    }
    else{print "correct usename. I will set a session cookie for you";
    print<<SetCookieHTML;
    <script language="JavaScript">
    <!-- hide
    setCookie('abbeycapitallogin','$uid',1)
    // -->
    </script>
    SetCookieHTML
    ;
    }

    print <<EndHTML;
    </body>
    </html>
    EndHTML
    ;

    -------------------

    3. The HTML page with embedded javascript and getCookie

    <html>
    <head><title> testing cookie</title>


    <script language="JavaScript">

    <!-- hide

    function getCookie(NameOfCookie){
    if (document.cookie.length > 0){
    begin = document.cookie.indexOf(NameOfCookie+"=");

    if (begin != -1) {
    begin += NameOfCookie.length+1;

    end = document.cookie.indexOf(";", begin);

    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(begin, end));

    }

    }

    return null;

    }

    // -->

    </script>
    </head>

    <body>
    <script language="JavaScript">

    <!-- hide

    username=getCookie("abbeycapitallogin");

    if (username!=null) { document.writeln('Found the cookie')}

    else {document.writeln('Please login')}
    //-->

    </script>
    </body>
    </html>
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    3
    Rep Power
    0
    What's the URL of this HTML page with embedded javascript and getCookie?
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    3
    Rep Power
    0
    I think I know what's the problem: the cookie path is set to "cgi-bin/" so any script/page outside the cgi-bin directory isn't able to read it.

    Try this:


    Code to set the cookie:

    function setCookie (name, value, expires) {
    if (!expires) expires = new Date();
    document.cookie = name + "=" + escape (value) +
    "; expires=" + expires.toGMTString() + "; path=/";
    }

    Function to check the cookie:

    function getCookie (name) {
    var dcookie = document.cookie;
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
    while (cbegin < clen) {
    var vbegin = cbegin + cname.length;
    if (dcookie.substring(cbegin, vbegin) == cname) {
    var vend = dcookie.indexOf (";", vbegin);
    if (vend == -1) vend = clen;
    return unescape(dcookie.substring(vbegin, vend));
    }
    cbegin = dcookie.indexOf(" ", cbegin) + 1;
    if (cbegin == 0) break;
    }
    return null;
    }

    regards
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    A cookie is only valid starting from the next page after it has been set. Put a hyperlink on your page pointing to itself and test.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Ireland
    Posts
    16
    Rep Power
    0
    c4n: Thanks, you are 100% correct on that one. I should have allowed the cookie to apply for my full site.
  10. #6
  11. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    83
    <broken_record>Use CGI.pm (which uses CGI::Cookie internally) to create,set,and get cookies. It will save you A LOT of time, and will create code that will remain compatible into the future.</broken_record>
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Ireland
    Posts
    16
    Rep Power
    0
    Hero Zzyzzx: I only want to set the cookie in a cgi-file once, the rest of the time I want to check the cookie with JavaScript in HTML. Thanks
  14. #8
  15. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    83
    Great. You should still use CGI.pm to create your cookie and set it, as it will do a much better job creating RFC compliant cookies than you will.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Ireland
    Posts
    16
    Rep Power
    0
    Thanks for the advice but why would I bother?
  18. #10
  19. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    83
    Originally posted by Hero Zzyzzx
    it will do a much better job creating RFC compliant cookies than you will.
    and is maintained and bugfixed by thousands of volunteers. What happens with your hand-rolled cookie-creation code if cookie standards change down the road? If you're using CGI.pm, your code will automatically get updated. With yours, you'll have to figure it out again and potentially fix it in a lot of places.
  20. #11
  21. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    83
    Jeez. You're using two languages to set a cookie. You're writing yourself into a tangled pit of misery, in terms of maintenance.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Ireland
    Posts
    16
    Rep Power
    0
    Eh, not really actually. I'm just writing one simple login script in perl and getting on with my life. Not that much of a big deal i'm afraid :-)

IMN logo majestic logo threadwatch logo seochat tools logo