I am getting a strange problem with two cookie scripts I've written.

The first, cookie_maker.cgi, creates two cookies containing a user id 
and a security key:

    #!/usr/bin/perl -w

    use strict;

    # creates cookies
    print "Set-Cookie:user=cbfb\n";
    print "Set-Cookie:secid=1234\n";

    # creates html header
    print "Content-type: text/html\n\n";
    print "<html><head></head><body>";

    # prints message
    print "<p>Created a cookie!</p>";

    # prints html footer
    print "</body></html>";

The second, cookie_reader.cgi, reads the cookies in a browser:

    #!/usr/bin/perl -w

    # creates html header
    print "Content-type: text/html\n\n";
    print "<html><head></head><body>\n";

    # reads cookie data
    if ($ENV{'HTTP_COOKIE'}) {
        @cookies = split (/;/, $ENV{'HTTP_COOKIE'});
        foreach $cookie (@cookies) {
            ($name, $value) = split (/=/, $cookie);
            print "<p>$name=$value</p>\n";
            $crumbs{$name} = $value;
        }

    # found cookies
        print "<p>Found some cookies.</p>\n";
        print "<p>user = $crumbs{'user'}</p>\n";
        print "<p>secid = $crumbs{'secid'}</p>\n";

    # no cookies found
    } else {
        print "<p>No cookies found.</p>\n";
    }

    # creates html footer
    print "</body></html>\n";

The second script outputs the following (in most browsers, see below):

    user=cbfb

    secid=1234

    Found some cookies.

    user = cbfb

    secid =

For some reason, looking at the page source, a space has been inserted 
before the name of the second cookie, secid, so $crumbs{'secid'} doesn't 
seem to return anything:

        <html><head></head><body>
        <p>user=cbfb</p>
        <p> secid=1234</p>
        <p>Found some cookies.</p>
        <p>user = cbfb</p>
        <p>secid = </p>

        </body></html>

To confuse me even more, if I look at the cookie in Mozilla cookie 
manager, it seems to be fine (ie has no leading space).

It does the same thing in Firefox, Epiphany, Lynx and Galeon. But 
interestingly in Konqueror it is 'user' that has the leading space instead!

BTW, I am using MDK Linux 10.1 with Perl 5.8.5 on Apache.

I am sure I am overlooking something obvious here, but no amount of 
fiddling has sorted it out. Hopefully someone with a better knowledge of 
Perl/CGI can point out what I'm doing wrong? If so, I look forward to 
hearing from you!

Kind regards,
Chris


I don't know that I can help directly... but here's away that I do the same
thing...

This creates the cookie...
<snip>
my %secure = (USER=>$user->{USER_ID},LEVEL=>0);
            my $my_cookie = $q->cookie(-name=>'TechDirect',
                -value=>\%secure);
            print $q->header({-type=>"text/html", -cookie=>$my_cookie});
</snip>

This read the cookie...

<snip>
my %secure = $q->cookie('TechDirect');
#
#   did i have a cookie and was User logging on ?
#
if (%secure) {
    $REQUEST{USER} = $secure{USER};
    $REQUEST{LEVEL} = $secure{LEVEL};
    }
</snip>

For more info the specs are in the CGI doc... I have not tried this with
many browsers. We have a closed set of browsers we work with.

jwm

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to