Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] Perl and LWP::UserAgent With Cookies Support


Hi Louis,

On Mon, Dec 02, 2002 at 09:28:08PM +1100, Louis Selvon wrote:
> For the headers I see this:
> 
> Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
> Connection: close
> Date: Sun, 01 Dec 2002 06:08:04 GMT
<snip>
> Set-Cookie: PHPSESSID=3de06093c289ef6f2b07dd47c73594e1; path=/
> 
> By the looks of this, they are using a Cookie for the current session.

The LWP cookie jar should handle this cookie fine if you set it up (see
below).

> For the html returned, it just returned the following:
> 
> <html>
> <head><title>Login</title>
> <script language="javascript">
> function setCookie(name,value,days)
> {
> var expires = new Date();
> expires.setTime(expires.getTime() + days*24*60*60*1000);
> document.cookie = name + "=" +value +
> ((expires == null) ? "" : "; expires=" + expires.toGMTString()) ;
> }//eof function setCookie()
> </script>
> 
> </head>
> 
> <body>
> <script>document.location='inside_home.php';</script>
> </body>
> </html>

This setCookie function is to set a cookie from javascript, but it isn't
actually used. But be aware that if they do use thing function on other
pages you won't pick the cookie up, because it's being done client-side
(by the javascript), not server-side (which is what you really need for
LWP).

> I do not see the html code that I see from my browser. I just see the php
> script name.
> 
> Problem 1:
> How do I get the script to return the html code for the php script ?

I'm not sure what you mean here. Do you mean you want the php source?

> Problem 2:
> For the cookies that was set, how do I set the Cookie jar to use this session
> for all my requests ? If I add the following line when I create the "$browser"
> object, will it work:
> 
> $browser->cookie_jar( {} );

I usually do something like:

$browser->cookie_jar(HTTP::Cookies->new(file => 'cookies.txt', autosave => 1, 
  ignore_discard => 1));

Tweak the HTTP::Cookies parameters to taste - ignore_discard is useful
for keeping session-only cookies around for debugging.

Cheers,
Gavin

-- 
Open Fusion P/L - Open Source Business Solutions [ Linux - Perl - Apache ]
http://www.openfusion.com.au
- Fashion is a variable, but style is a constant - Programming Perl