Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Fetching cookies without errors?

by Dazz45X (Novice)
on Aug 23, 2017 at 15:37 UTC ( [id://1197878]=note: print w/replies, xml ) Need Help??


in reply to Re: Fetching cookies without errors?
in thread Fetching cookies without errors?

Read it, re-read it and slept on it but just can't see it in my situation. Like I said this is the first time I've picked it up in years and it's fried my brain. Thanks

Replies are listed 'Best First'.
Re^3: Fetching cookies without errors?
by huck (Prior) on Aug 23, 2017 at 15:59 UTC

    Here is some simple code to do what you are suggesting. On note is i use a custom cookie name TSSID.

    #!/usr/bin/perl use strict; use warnings; select STDOUT; $| = 1; my $session_dir='/home/huck/monks-sessions'; # must exist and be wr +iteable by www userid my $expires='+1m'; # '+7d' '+1h' ; my $cookieexpires=$expires; # use dbi::ezdbiutil qw/ezdbi_gethash/; use CGI; use CGI::Session; use CGI::Cookie; my $style='disk'; # $style='mysql'; my $session; my $notloggedin=''; my $cookies; my %passwords=(admin=>'admin',huck=>'huck'); my $cgi = CGI->new; my $tssid = $cgi->cookie('TSSID'); my $timelast; my $sessiona1; my $sessiona3; if ($style eq 'mysql'){ eval 'use cheap::mysqls qw/sql_connect/; '; my ($dbh,$rc) = sql_connect(dbi=>'mysql',id=>'monks'); die "Cannot connect: $DBI::errstr" unless ($dbh); $sessiona1="driver:MySQL"; $sessiona3={ Handle => $dbh }; } else { $sessiona1=undef; $sessiona3={Directory=>$session_dir}; } # my $session = CGI::Session->load("driver:MySQL", $query, { Handle = +> $dbh }); unless ($tssid){ my $userid =$cgi->param('userid'); my $password=$cgi->param('password'); $userid='' unless ($userid); $password='' unless ($password); unless ( $userid) {$notloggedin='Ple +ase Login';} elsif (! $passwords{$userid}) {$notloggedin='Bad + Userid';} elsif ($password ne $passwords{$userid}) {$notloggedin='Bad + Password';} else { $session = CGI::Session->new($sessiona1, undef,$sessiona3); $cookies = [CGI::Cookie->new(-name => 'TSSID', -value => $session->id, -expires => $cookieexpires )]; $session->expires($expires); $session->param('user_id',$userid); $session->param('timein' , time); $session->param('timelast', time); $timelast=time; $session->flush(); } # ok } # no tssid else { $session = CGI::Session->load($sessiona1, $tssid, $sessiona3); if ( $session->is_expired ) { $notloggedin='login expired ' ; $session->delete(); $session->flush(); } elsif ( $session->is_empty ) { $notloggedin='login not found'; +} else { $cookies = [CGI::Cookie->new(-name => 'TSSID', -value => $session->id, -expires => $cookieexpires )]; $timelast=$session->param('timelast'); $session->param('timelast', time); $session->flush(); } } if ($notloggedin){ $cookies = [$cgi->cookie(TSSID => '')]; print $cgi->header(-cookie=>$cookies); print '<html><head><title>Must login</title></head><body>'."\n"; + print '<h1>Must login</h1>'."\n"; print '<h3>'.$notloggedin.'</h3>'."\n"; print '<form method="POST">'."\n"; print 'Userid:<input type="text" name="userid">'."\n"; print ' Password:<input type="text" name="password">'."\n"; print '<input type="submit" value="Login">'."\n"; print '</form>'."\n"; } else { if ($cookies) {print $cgi->header(-cookie=>$cookies); } else { print $cgi->header(); } print '#html content after seccessfully login'; print '<br>hi '.$session->param('user_id'); print ' loggedin for '.(time-$session->param('timein')).' second +s'; print ' last seen '.(time-$timelast).' seconds ago'; } print '</body></html>'; CGI::Session->find($sessiona1 ,sub {} ,$sessiona3); # clean expired +sessions

      use CGI::Session;

      That rings a bell: Re^4: Randomness encountered with CGI Session - The ID generators in CGI::Session looked questionable in v4.42, and the diff with the current v4.48 does not look like anything relevant was changed in that area.

      I would not use CGI::Session on a production machine, for the reasons explained 7 years ago.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Thank you Huck I'll study that tonight
        use cgi:application with plugins, writing your own session cookies fetching is mindless

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-24 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found