473,322 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Cookie Information

20
Hi,


I need perl code for getting cookie information.if anybody knows this ,kindly let me know. I am looking forward for this.
Mar 28 '07 #1
11 1794
miller
1,089 Expert 1GB
This is the one place that has more information than anyone.

Cookie's

Enjoy,
- Miller
Mar 28 '07 #2
miller
1,089 Expert 1GB
This is what I personally use.

perldoc CGI::Cookie

ymmv,
- Miller
Mar 28 '07 #3
KevinADC
4,059 Expert 2GB
The standard CGI module also handles cookies, see the CGI documentation for details.
Mar 28 '07 #4
Bsr
20
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
Mar 29 '07 #5
miller
1,089 Expert 1GB
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
Mar 29 '07 #6
Bsr
20
Hi Miller,


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

Expand|Select|Wrap|Line Numbers
  1. use LWP::UserAgent;
  2. use HTTP::Cookies::Microsoft;
  3. use HTTP::Request;
  4. # construct objects
  5. my $ua = LWP::UserAgent->new;
  6. my $cookie_jar = HTTP::Cookies::Microsoft->new;
  7.  
  8. my $request =HTTP::Request->new(POST => 'http://www.google.co.in');
  9. my $response = $ua->request($request);
  10. $cookie_jar->add_cookie_header( $request );
  11. $cookie_jar->extract_cookies( $response ) ;
  12.  
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
Mar 30 '07 #7
KevinADC
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. use LWP::UserAgent;
  2. use HTTP::Cookies;
  3. use HTTP::Request;
  4. use Data::Dumper;
  5. my $ua = LWP::UserAgent->new;
  6. $ua->agent('Mozilla/5.0');
  7. my $cookie_jar = HTTP::Cookies->new;
  8.  
  9. my $request =HTTP::Request->new(GET => 'http://www.google.co.in');
  10. my $response = $ua->request($request);
  11. $cookie_jar->add_cookie_header( $request );
  12. $cookie_jar->extract_cookies( $response ) ;
  13. print Dumper \$cookie_jar;
Mar 30 '07 #8
Bsr
20
Hi KevinADC,

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

1. What is this meaning of $ua->agent('Mozilla/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.

Expand|Select|Wrap|Line Numbers
  1. Result:
  2. $VAR1 = \bless( {
  3.     'COOKIES' => {
  4.         '.google.co.in' => {
  5.             '/' => {
  6.                 'PREF' => [
  7.                     0,
  8.                     'ID=cf620eca6ea2bc24:TM=1175246752:LM=1175246752:S =GJB0XsrEEVk97uZ9',
  9.                     undef,
  10.                     1,
  11.                     undef,
  12.                     2147368447
  13.                 ]
  14.             }
  15.         }
  16.     }
  17. }, 'HTTP::Cookies' );
  18.  
Mar 30 '07 #9
KevinADC
4,059 Expert 2GB
Thanks a lot.I got it. But i have doubts as given below.

1. What is this meaning of $ua->agent('Mozilla/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:

Expand|Select|Wrap|Line Numbers
  1. use LWP::UserAgent;
  2. use HTTP::Cookies;
  3. use HTTP::Request;
  4. use Data::Dumper;
  5. my $ua = LWP::UserAgent->new;
  6. $ua->agent('Mozilla/5.0');
  7. my $cookie_jar = HTTP::Cookies->new;
  8. my $request =HTTP::Request->new(GET => 'http://www.google.co.in');
  9. my $response = $ua->request($request);
  10. $cookie_jar->add_cookie_header( $request );
  11. $cookie_jar->extract_cookies( $response ) ;
  12. $cookie_jar->scan(sub {
  13.     my ( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard) = @_;
  14.     print qq~$version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard~;
  15. });
It's up to you to figure out what to do with:

$version, $key, $val, $path, $domain, $port, $path_spec, $secure, $expires, $discard
Mar 30 '07 #10
Bsr
20
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
Apr 2 '07 #11
KevinADC
4,059 Expert 2GB
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
Apr 2 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: bagsmode | last post by:
Hi, I'm trying to set a session cookie and then redirect, however I get the error: Status: 302 Moved Location: /index.cgi I thought I recall getting an error like this when I first tried...
1
by: Matt | last post by:
I want to know what's the differences between session cookie and regular cookie. In ASP, when we create cookie, we do the following to identify an user: Response.Cookies("name") = value Is...
4
by: Shannon Jacobs | last post by:
I'm doing some trivial surveys, and I want to know if the same user answers twice. Can't really know that, but at least I thought I could check for the same browser/computer combination by using a...
17
by: neerolyte | last post by:
how would i go about setting a cookie in javascript that can be read in javascript on the next page load, but will NOT be passed to the server?
6
by: Mark | last post by:
I am designing a game for a forum. When the user has finished playing I need to save their data to a cookie then navigate to a page which holds their score data (I can't have both sets of data on...
9
by: Marco Krechting | last post by:
Hi All, I have a page with a list of hyperlinks. I want to save information in a cookie about the fact that I entered an hyperlink or not. When I click one of the hyperlinks I want this stored...
4
by: nico | last post by:
I store the authenticated user in a session cookie. This cookie expires after an hour. Now for some reason sometimes the client browser decides not to send the session cookie information with a new...
2
by: junOnline | last post by:
Hi, I am developing an asp.net 2.0 application using visual studio 2005 on my computer and I am getting very inconsistent results when I test on my computer versus from other computers. The...
6
by: Alessandro Fachin | last post by:
Hi, i am trying to forge a new cookie by own with cookielib. But i don't still have success. This a simply code: import cookielib, urllib, urllib2 login = 'Ia am a cookie!' cookiejar =...
7
by: =?Utf-8?B?SlA=?= | last post by:
I need to design a WS that will after authenicating the user, create a cookie on the users PC that made the request. All the code I keep finding is how to get a WS to read a cookie, I need it to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.