Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

cookie problem

by bigup401 (Pilgrim)
on Nov 11, 2014 at 17:48 UTC ( [id://1106856]=perlquestion: print w/replies, xml ) Need Help??

bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

it looks to be sending cookie now, bt the problem is still bring me back to home.pl instead of wel.pl

Location: wel.pl?Set-Cookie: login=root; path=/; expires=Tue, 11-Nov-2014 23:33:34 GMT Date: Tue, 11 Nov 2014 23:31:34 GMT Content-Type: text/html; charset=ISO-8859-1 Content-type: text/html

home.pl my $cg = new CGI; my $cookie = $cg->cookie(-name => 'login', -value => $username, -expires => '+2m', -path => '/'); print $cg->header(); print "Location: wel.pl?"; print $cg->header(-cookie => $cookie); wel.pl my $cg = new CGI; my $cookie = cookie('login'); if ( ! $cookie ) { print $cg->redirect("home.pl"); } elsif($cookie) { continue; }

Original node content restored below by GrandFather

help me to fix this cookie problem! my cookie script i don't see any problem with it bt i cant understand why its not working, when i put the user and press Submit button it reach at wel.pl and redirect me back to home.pl

home.pl my $cookie = $cg->cookie(-name => 'id', -value => $user, -expires => '+2m', -path => '/'); print "Location: wel.pl?"; print $cg->header(-cookie => $cookie); wel.pl my $cookie = $cg->cookie('id'); if ( ! $cookie ) { print $cgi->redirect("home.pl"); } elsif(my $cookie) { continue; #continue to the site if cookie exists }

Replies are listed 'Best First'.
Re: cookie problem
by toolic (Bishop) on Nov 11, 2014 at 18:18 UTC
    elsif(my $cookie) {
    That should probably be:
    elsif($cookie) {

    or just:

    else {
Re: cookie problem
by fishmonger (Chaplain) on Nov 11, 2014 at 18:28 UTC

    The cookie was never sent due to the print "Location: wel.pl?";

    EDIT:
    I see that you've updated your post to use print $cg->redirect("wel.pl"); instead of the "Location:" header. That change won't fix the problem you're having because the attempt to send the cookie following that redirect statement won't be sent.

    Personally, I prefer to keep all the cookie data on the server side via the CGI::Session module.
    CGI::Session::Tutorial

Re: cookie problem
by poj (Abbot) on Nov 11, 2014 at 18:35 UTC
    my $cookie = $cg->cookie('id'); if ( ! $cookie ) { print $cgi->redirect("home.pl"); }

    Which is correct $cg or $cgi ?

    poj

      its

      $cg
Re: cookie problem
by GotToBTru (Prior) on Nov 11, 2014 at 20:26 UTC

    What is $cg? I don't see it defined anywhere, nor do you have any module names that might provide a clue.

    1 Peter 4:10

      cg is defined

      my $cg = new CGI;

        You should post short but complete scripts that demonstrate your issue.

        Why do you have that continue statement? Using it as you have shown doesn't make any sense.

Re: cookie problem
by fishmonger (Chaplain) on Nov 12, 2014 at 15:33 UTC

    Here's an example taken from one of my working apps that uses CG::Session, which I feel is a cleaner and better way to handle the cookie/session data. I had to strip out a few things for privacy reasons, but all of the key elements are there.

    The login page

    #!/usr/bin/perl use warnings; use strict; use DBI; use CGI; use CGI::Session; use HTML::Template; use Crypt::PasswdMD5; my $title = 'Email Administration Login'; my $cgi = CGI->new; my $self = $cgi->url; my %login = $cgi->Vars; my $session = CGI::Session->new or die CGI::Session->errstr; my $template = HTML::Template->new( filename => '../../html/emadmin/login.t +mpl', associate => [$session], die_on_bad_params => 0, global_vars => 1, cache => 0, ); $session->clear if $cgi->param('logout'); $session->param('hostname', `hostname`); if ( $cgi->param('Login') ) { my $home = 'http://emailadmin.company.com/admin/search.pl'; print $cgi->redirect($home) if authenticated_user(\%login); } print $session->header; print $template->output; ###################################################################### +########## sub authenticated_user { my $login = shift; $session->param('login_failed', 'Invalid username, or password...Please try again' +); if ( defined $login->{'username'} && defined $login->{'password'} +) { my ($encrypted_pass, $roll, $name) = query_DB($login->{'userna +me'}); if ( $encrypted_pass ) { my $salt = substr($encrypted_pass, 3,8); my $password = unix_md5_crypt( $login->{'password'}, $salt + ); if ( $password eq $encrypted_pass ) { $session->clear('login_failed'); $session->param('logged_in', 1); $template->param('logged_in', 1); $session->param('admin', $login->{'username'}); $session->param('roll', $roll); $session->param('gic', 1) if $roll eq 'admin'; return 1; } } } return 0; }

    The page/script it redirects to on successful login

    #!/usr/bin/perl use warnings; use strict; use DBI; use CGI; use CGI::Session; use HTML::Template; my $cgi = CGI->new; my %form = $cgi->Vars; my $session = CGI::Session->load; if ($session->is_empty or $session->is_expired or !$session->param('lo +gged_in')) { my $login_page = 'http://emailadmin.company.com'; print $cgi->redirect($login_page); } print $cgi->header; my $template = HTML::Template->new(filename => '../../html/emadmin/sea +rch.tmpl', associate => [$session], global_vars => 1); $template->param(title => 'Email Administration - Search Page'); $template->param(gic => 1) if $session->param('roll') eq 'admin'; if ( exists $form{'locate'} ) { my @search_results = search_abook($form{searchfield}, $form{search +value}); if ( @search_results ) { $template->param(found => scalar @search_results); $template->param(results => \@search_results); } else { $template->param(not_found => 'Unable to locate any users matc +hing your query'); } } print $template->output;
      yes cgi::session works for me well and is very nice and easy and faster bt i have tried cookies and failed 1 time so i want to understand where is the problem i really dont understand why cookie doesn;t work, i have seen a lot of post reporting problems on cookie, perl cookie is so complicated,
        #!perl # home.pl use strict; use CGI; my $q = new CGI; print $q->header(),$q->start_html, $q->start_form(-method=>"post",-action=>"wel.pl"), $q->b("ID : "), $q->textfield(-name=>"id", -value=>$q->cookie('id')), $q->submit(),$q->end_form,$q->end_html; #!perl # wel.pl use strict; use CGI; my $q = new CGI; my $id = $q->param('id'); my $cookie = $q->cookie( -name => 'id', -value => $id, -expires => '+2m', -path => '/'); if ( ! $id ){ print $q->redirect(-uri=>"home.pl", -cookie=>$cookie); } else { print $q->header(-cookie=>$cookie); print $q->start_html, $q->a({-href=>'home.pl'},'home'), $q->p("Hello $id"),$q->end_html; }
        poj
Re: cookie problem (cgi101)
by Anonymous Monk on Nov 12, 2014 at 03:25 UTC

    This is how you write that stuff ( both home.pl and wel.pl ), you print from Main, your ThisPage() or ThatPage() returns $headers, $body

    #!/usr/bin/perlml -- ## ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while " -otr + -opr -ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use CGI (); use CGI::Carp qw( fatalsToBrowser ); Main( @ARGV ); exit( 0 ); sub Main { my $q = CGI->new; return print GoToWellWithCookies( $q ); } ## end sub Main sub UrlFor { my( $q, $path ) = @_; return $path; } ## end sub UrlFor sub ValidLogin { my( $q ) = @_; my $user = $q->param( 'login' ); my $pass = $q->param( 'pass' ); ## YOUR JOB :) return !!1 if $user eq $pass; return !!0; } ## end sub ValidLogin ## sub TryAndLoginToGoHome ## accepts $query ## returns $headers, $body; ## sub GoToWellWithCookies { my( $q ) = @_; if( my $user = ValidLogin( $q ) ) { my $cookie = $q->cookie( -name => 'id', -value => $user, -expires => '+2m', -path => '/', ); return $cg->redirect( -location => UrlFor( $q, "wel.pl" ), -cookie => $cookie, ); } else { ; return $cg->redirect( -location => UrlFor( $q, "home.pl" ), ); } } ## end sub GoToWellWithCookies __END__ __END__
      i have understand your post bt wat am confused of wat to use in home.pl and wel.pl i see it as one scrip for 1 page

        i have understand your post bt wat am confused of wat to use in home.pl and wel.pl i see it as one scrip for 1 page

        Its an example of how you should write your programs so that cookies work for you (Main prints, ThisPage or ThatPage return $headers, $body, which Main prints)

        Its an example based on code you posted for home.pl

        You should fix it up and write the same way for wel.pl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1106856]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-19 20:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found