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 {
| [reply] [Watch: Dir/Any] [d/l] [select] |
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
| [reply] [Watch: Dir/Any] [d/l] [select] |
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 | [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] |
|
cg is defined
my $cg = new CGI;
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] |
|
|
|
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;
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
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,
| [reply] [Watch: Dir/Any] |
|
#!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
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
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__
| [reply] [Watch: Dir/Any] [d/l] |
|
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
| [reply] [Watch: Dir/Any] |
|
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
| [reply] [Watch: Dir/Any] |