Hi List members:

I am writing a perl script that automatically logs in an a password protected
admin area. Once logged in I want the script to be able to fetch pages and
also be able to submit any form post/get requests within the admin area.

However it's not working. So I need some help. Here is my current problem.

From the Perl script, I was able to login with my credentials by using the
following code:

$browser = LWP::UserAgent->new( );
$browser->env_proxy( ); # if we're behind a firewall

# Login into the script
$response = $browser->post(
  "$login",
  [
    'user'  => $username,
    'password' => $password,
    'submit' => 'Login'
  ],
);

where, 
$login = the url script that processes the login data that the forms submits;
$username, and $password are the credentials.

I was able to login as I passed the following line of code:

die &error_file("Error1: $response->status_line", "sub login()") unless
$response->is_success;

Now from the browser when I login, it takes me to the admin area which is a
".php" script (but I see the html code). In my script after the above line I
did the following:

# save the headers returned 
open (FH, ">contents.dat");
   print FH $response->headers_as_string;
close (FH);

@headers = $response->headers_as_string;

# now append the html data returned
open (FH, ">>contents.dat");
   print FH "\n";
   print FH $response->content();
close (FH); 

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
Pragma: no-cache
Server: Apache/1.3.26 (Unix) mod_perl/1.27 PHP/4.2.1 mod_ssl/2.8.10
OpenSSL/0.9.6e
Content-Type: text/html
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Client-Date: Sun, 01 Dec 2002 05:30:08 GMT
Client-Response-Num: 1
Client-Transfer-Encoding: chunked
Set-Cookie: PHPSESSID=3de06093c289ef6f2b07dd47c73594e1; path=/
Title: Login
X-Powered-By: PHP/4.2.1

By the looks of this, they are using a Cookie for the current session.

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>

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 ?

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( {} );

Now I actually use all the headers that was returned after login, and added
the following to fetch a page from the admin area:

$response = $browser->get($url, @headers);

die &error_file("Error2: $response->status_line", "sub login()") unless
$response->is_success;

# save the headers returned 
open (FH, ">first.html");
   print FH $response->headers_as_string;
close (FH);

# now append the html data returned
open (FH, ">>first.html");
   print FH "\n";
   print FH $response->content();
close (FH); 

$url = the url for the "inside_home.php" script;
@headers = $response->headers_as_string;

This did not fetch the html code that I see form my browser. Instead in the
file "first.html", I saw that the error page was to login again. I presume
this is because I did treat the cookie session I was assigned correctly. So
this goes back to problem 2 above.

If anyone can help me out on this that would be greatly appreciated.

Louis.

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to