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

Re^2: cookie problem

by Anonymous Monk
on Nov 12, 2014 at 16:31 UTC ( [id://1106993]=note: print w/replies, xml ) Need Help??


in reply to Re: cookie problem
in thread cookie problem

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,

Replies are listed 'Best First'.
Re^3: cookie problem
by poj (Abbot) on Nov 12, 2014 at 16:34 UTC
    #!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

      There are 2 things to be aware of when using print $q->redirect(-uri=>"home.pl", -cookie=>$cookie); as pointed out in the CGI documentation under Generating_a_redirection_header.

      1) You should always use full URLs (including the http: or ftp: part) in redirection requests. Relative URLs will not work correctly.
      2) All names arguments recognized by header() are also recognized by redirect(). However, most HTTP headers, including those generated by -cookie and -target, are ignored by the browser.

      I take from those 2 comments that the results could be unpredictable.

        this is the full script

        HOME.PL use DBI; use CGI; use strict; my $cgi = CGI->new(); my $db ="databa"; my $usr ="root"; my $pwd =""; my $host ="localhost"; my $dbh = DBI->connect("DBI:mysql:$db:$host", $usr, $pwd { AutoCommit => 0, RaiseError => 1, } ) or die $DBI::errstr; my $username = $cgi->param('username'); my $password = $cgi->param('password'); my $sth = $dbh->prepare("select id from mysql_auth where username=? AN +D password=?"); $sth->execute($username,$password) if(my $x = $sth->fetchrow()); { $sth->finish(); my $cookie = $cgi->cookie( -name => 'login', -value => $username, -expires => '+2m', -path => '/'); print $cgi->redirect(-url=>"welcome.pl", -cookie=>$cookie); } else { print"wrong password\n"; } WELCOME.PL use DBI; use CGI; use strict; my $cgi = CGI->new(); $sid = $cgi->param('login'); if ( ! $sid ){ # exit and return to home.pl! we have no cookies print $cgi->redirect(-uri=>"home.pl", -cookie=>$cookie); } else { #start using the page! we have cookies print $cgi->header(-cookie=>$cookie); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found