Hi,

I have a login page. The login details are authenticated upon submission of the page. If successful, a cookie is setup and the page is redirected to "application.cgi". If user details are invalid, then it goes to the logon page. On every page, user authentication is done. Cookie information is used here. If cookie does not exist, then the user is forced to sign in. This works fine when I go through the logon page.

However, I have links to this application from some other application. I do not want to force the users to login every time. Hence, we came up with an idea to POST these details using LWP::UserAgent. This way everything else works excpet creation of cookie. When the user is successfully authenticated, the page is redirected and status_line prints the status as redirected.

Any of you have any idea why cookie is not being setup?

Here is the code.

1. setcookie.cgi:

#!/usr/local/bin/perl use CGI; use DbFunctions; my $q = new CGI; my $username = $q->param("username"); my $password = $q->param("password"); my $role = ''; my $redirectto; if ( $username ne "" && $password ne "" ) { my $validated = Authenticate($username,$password); if ( $validated ){ if( $role eq 'view' ){ $redirectto = "searchresults.cgi"; } else{ $redirectto = "application.cgi"; } my $cookie_value = $username."^".$password."^".$role; my $cookie = $q->cookie( -name => "cookie_user", -value => "$cookie_value", -domain => "", -expires => "+1d", -path => ""); #print $q->header(-cookie => $cookie); print $q->redirect( -location => $redirectto, -cookie => $cookie ); } else{ print $q->redirect( -location => "index.cgi?msg=1"); exit; } } else { print $q->redirect( -location => "index.cgi"); } exit;

2. post.pl ( Here I will be passing some id as input. I had removed that part of code.)

#!/usr/local/bin/perl use CGI; my $q = new CGI; use LWP; use HTTP::Request::Common qw(POST GET); use LWP::UserAgent; print "Content-type: text/html\n\n"; $ip_line="http://XXXXXX/setcookie.cgi"; my $ua = new LWP::UserAgent; #$ua->agent("AgentName/0.1 " . $ua->agent); #my $req = new HTTP::Request "POST","$ip_line"; #$req->content("username=user&password=pwd"); $ua->timeout(45); # 45-sec timeout my $req = POST $ip_line, [ username => "user", password => "pwd" ]; my $res = $ua->request($req); print "status=[".$res->status_line."]n"; #$content = $res->content(); #print $content;

Edited by Chady -- code tags.


In reply to HTTP POST & Cookie by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.