Results 1 to 9 of 9

Thread: CGI and Cookies [Resolved]

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    CGI and Cookies [Resolved]

    Can someone explain cookies when used with CGI to me? I want to be able to retrieve a cookie using CGI.
    Last edited by The Hobo; Mar 4th, 2002 at 01:38 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    They are sent by HTTP Headers - the server sends a Set-Cookie: header and the Browser sends a Cookie: header. So they web server makes the cookie readable by your CGI script thru the HTTP_COOKIE environmental variable. Do a google search for "cookie " and your CGI language of choice for specific info on that language.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I can't find anything usefull. Everything I find is too complex (full cookie management).

    I just want a basic example of how to read a cookie named user@dogs or whatever.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    scoutt
    Guest

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    scoutt (or anybody else), I need a little help. I plugged in that code into a file named cookies.pl, and tried it out with this, however, the cookies never save. What am I doing wrong?

    Code:
    $adminpass = "bah";
    
    &breakup;
    &checklogin;
    
    sub checklogin {
        require "cookie.pl";
        #read cookies
        $cookie = &get_cookie("password");  
    
        if ($adminpass eq $cookie) {
            print "login with cookie sucessful.";
        } else {
            if ($in{'action'} eq 'login') {
                if ($in{'pass'} eq $adminpass) {
                    print "Content-type: text/html \n\n";
                    print "password correct";
                    $cookie = &set_cookie("password",$adminpass,0,"/","www.mindlessdrone.com");
                    print $cookie;
                } else {
                    print "Content-type: text/html \n\n";
                    print "password incorrect";
                }
            } else {
                print "Content-type: text/html \n\n";
                print qq~
                <html>
                <form action="login.cgi" method="post">
                <input type="hidden" name="action" value="login">
                <input type="text" name="pass">
                <input type="submit">
                </form>
                </html>
                ~;
            }
        }
    }
    
    #get input
    sub breakup {
        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        if ($buffer eq '') {
            $buffer = $ENV{'QUERY_STRING'};
        }
        
        my @pairs = split(/&/, $buffer);
        
        foreach $pair (@pairs) {
        ($name, $value) = split(/=/, $pair);
            $value =~ tr/+/ /;
            $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
            $in{$name} .= $value;
        }
    }
    Sorry if the code is lengthy. Thanks for any help.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    You need to "print" the Cookie with the HTTP headers, not as part of the HTML document - so before the two newlines that mark the end of the HTTP headers:
    Code:
    print <<HTTPHEADERS;
    Set-Cookie: username=josht&language=english
    Content-type: text/html
    
    HTTPHEADERS
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I'll try this when I get home from school. One more quick question:

    Code:
    &set_cookie("password",$adminpass,0,"/","www.mindlessdrone.com");
    If my script is located in www.mindlessdrone.com/cgi-bin, should my code be:

    Code:
    &set_cookie("password",$adminpass,0,"/cgi-bin/","www.mindlessdrone.com");
    or...?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    If the function works the way I think it works, the first snippet will cause the UA to return the cookie for any page in your site, where as the second snippet will cause the UA to return the cookie only for pages in or below the cgi-bin.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Oh! I understand now. So basically I was printing the context type before I was setting the cookie which made it so the cookie was never set.

    I needed to change it to:

    Code:
    $cookie = &set_cookie("password",$adminpass,0,"/","www.mindlessdrone.com");
    print $cookie;
    print "Content-type: text/html \n\n";
    print "password correct";
    It all makes sense now. Thanks a million, Josh
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width