Cookie Information

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bsr
    New Member
    • Mar 2007
    • 20

    Cookie Information

    Hi,


    I need perl code for getting cookie information.if anybody knows this ,kindly let me know. I am looking forward for this.
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    This is the one place that has more information than anyone.

    Cookie's

    Enjoy,
    - Miller

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      This is what I personally use.

      perldoc CGI::Cookie

      ymmv,
      - Miller

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        The standard CGI module also handles cookies, see the CGI documentation for details.

        Comment

        • Bsr
          New Member
          • Mar 2007
          • 20

          #5
          Hi,

          I tried with many examples , But unable to get that cookies information.
          Please send the sample code for cookie information for google site.

          Cheers
          Bhuvan

          Comment

          • miller
            Recognized Expert Top Contributor
            • Oct 2006
            • 1086

            #6
            Hi Bhuvan,

            The documentation linked to above is clear. If you're having problems, describe your programming environment and show us the code that you are trying to get to work. We can then help you debug and diagnose what your problem is.

            What have you tried so far? Show us.

            - Miller

            Comment

            • Bsr
              New Member
              • Mar 2007
              • 20

              #7
              Hi Miller,


              Please find the sample perl code for to get the cookie information for google site.

              Code:
              use LWP::UserAgent;
              use HTTP::Cookies::Microsoft;
              use HTTP::Request;
              # construct objects
              my $ua = LWP::UserAgent->new;
              my $cookie_jar = HTTP::Cookies::Microsoft->new;
              
              my $request =HTTP::Request->new(POST => 'http://www.google.co.in');
              my $response = $ua->request($request);
              $cookie_jar->add_cookie_header( $request );
              $cookie_jar->extract_cookies( $response ) ;
              Note:In the above code we should give the print command for displaying the cookie information. But here,i have no idea how to display the same information.

              If anything wrong,please correct me.It's very argent for me. Please do the needful.


              Cheers
              Bhuvan
              Last edited by miller; Mar 30 '07, 04:53 PM. Reason: Code tag

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Code:
                use LWP::UserAgent;
                use HTTP::Cookies;
                use HTTP::Request;
                use Data::Dumper;
                my $ua = LWP::UserAgent->new;
                $ua->agent('Mozilla/5.0');
                my $cookie_jar = HTTP::Cookies->new;
                
                my $request =HTTP::Request->new(GET => 'http://www.google.co.in');
                my $response = $ua->request($request);
                $cookie_jar->add_cookie_header( $request );
                $cookie_jar->extract_cookies( $response ) ;
                print Dumper \$cookie_jar;

                Comment

                • Bsr
                  New Member
                  • Mar 2007
                  • 20

                  #9
                  Hi KevinADC,

                  Thanks a lot.I got it. But i have doubts as given below.

                  1. What is this meaning of $ua->agent('Mozil la/5.0')
                  2. Why we need use "use Data::Dumper" package?
                  3. Can we get only cookie ID and Expire date?


                  Please explain the contents of Cookie information in Result.

                  Code:
                  Result:
                  $VAR1 = \bless( {
                  	'COOKIES' => {
                  		'.google.co.in' => {
                  			'/' => {
                  				'PREF' => [
                  					0,
                  					'ID=cf620eca6ea2bc24:TM=1175246752:LM=1175246752:S =GJB0XsrEEVk97uZ9',
                  					undef,
                  					1,
                  					undef,
                  					2147368447
                  				]
                  			}
                  		}
                  	}
                  }, 'HTTP::Cookies' );
                  Last edited by miller; Mar 30 '07, 04:54 PM. Reason: Code tag

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Thanks a lot.I got it. But i have doubts as given below.

                    1. What is this meaning of $ua->agent('Mozil la/5.0')

                    Google does not like people hitting their sites with bots, which is really what this is. If you do not define a user agent (a web browser in other words) google might not send any response back. The line of code above fools google into thinking you are using the Mozilla/5.0 browser.

                    2. Why we need use "use Data::Dumper" package?

                    That is just an easy way to dump the contents of the cookie data, which is returned as a reference to a hash.


                    3. Can we get only cookie ID and Expire date?

                    There is no expire date, it appears to be a cookie that just gets discarded. You can get the cookie data like this:

                    Code:
                    use LWP::UserAgent;
                    use HTTP::Cookies;
                    use HTTP::Request;
                    use Data::Dumper;
                    my $ua = LWP::UserAgent->new;
                    $ua->agent('Mozilla/5.0');
                    my $cookie_jar = HTTP::Cookies->new;
                    my $request =HTTP::Request->new(GET => 'http://www.google.co.in');
                    my $response = $ua->request($request);
                    $cookie_jar->add_cookie_header( $request );
                    $cookie_jar->extract_cookies( $response ) ;
                    $cookie_jar->scan(sub {
                        my ( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard) = @_;
                        print qq~$version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard~;
                    });
                    It's up to you to figure out what to do with:

                    $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard

                    Comment

                    • Bsr
                      New Member
                      • Mar 2007
                      • 20

                      #11
                      Hi,

                      Thanks Kevin.

                      Please clarify for the following questions in given code.

                      1. What is the use of scan()
                      2. Why do we need sub function
                      3. what is meaning of qq~ in the print command.


                      code:

                      $cookie_jar->scan(sub {
                      my ( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard) = @_;
                      print qq~$version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard~;
                      });

                      Bhuvan

                      Comment

                      • KevinADC
                        Recognized Expert Specialist
                        • Jan 2007
                        • 4092

                        #12
                        scan() is a method of HTTP::Cookies. It's covered in the module documentation.

                        sub{} is just an annonymous sub routine in perl. The scan() method expects a reference to a sub as it's argument, so you have to use a reference to a named sub ( scan(\&namedSub ) ) or an annonymous sub, which is a reference.

                        Annonymous subs are good for short functions that do one thing and don't really merit being a named sub routine/function all their own.

                        qq is a quoting operator. You can look that up on the perldoc website:

                        Quote and Quote like Operators

                        Comment

                        Working...