www.webdeveloper.com
Results 1 to 5 of 5

Thread: Get Cookie values without using CGI.pm

  1. #1
    Join Date
    Aug 2006
    Posts
    46

    Get Cookie values without using CGI.pm

    Hi all,

    I need to be able to read the value of a cookie with Perl. I have recently moved my website onto a hosted Windows environment but for some reason, any perl scripts relying on CGI.pm just will not work.

    Therefore, can someone please post a perl script that I can use to retrieve Cookie values.


    Cormac
    Dublin
    Ireland

  2. #2
    Join Date
    Aug 2006
    Posts
    46
    Hi all,

    Forgive my lack of knowledge but I finally found the following code which works perfectly to retrieve ALL cookies from a given site. However, how would I amend it to retrieve just one specific cookie

    print "\nReload for Cookies:<BR>";
    %cookies = &getCookies; # store cookies in %cookies

    foreach $name (keys %cookies) {
    print "\n$name = $cookies{$name}<br>";
    }

    sub getCookies {
    local(@rawCookies) = split (/; /,$ENV{'HTTP_COOKIE'});
    local(%cookies);

    foreach(@rawCookies){
    ($key, $val) = split (/=/,$_);
    $cookies{$key} = $val;
    }

    return %cookies;
    }


    Cormac

  3. #3
    Join Date
    Jan 2007
    Posts
    3
    Try this

    Code:
    sub Cookie{
       my ($cookiename) = @_;
       my @cookies = split(/\s*;\s*/, $ENV{'HTTP_COOKIE'});
       foreach (@cookies){
         my @tokens = split(/=/, $_);
         return $tokens[1] if($tokens[0] == $cookiename);
       }
       return '';
    }

  4. #4
    Join Date
    Aug 2006
    Posts
    46
    Sorry, i'm a bit lost. If I had a cookie called "MyCookie", how could I retrieve it's value only?


    Cornac

  5. #5
    Join Date
    Jan 2007
    Posts
    3
    Use the function that was given in earlier thread, then call it with the cookie name you want

    i.e

    Code:
    my $cookie = &Cookie("MyCookie");
    where "MyCookie" is the cookie in question, and $cookie will hold the cookie value, if any

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles