Link to home
Start Free TrialLog in
Avatar of fosiul01
fosiul01Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Session management with perl

Hi

I am trying to learn perl .bellow is a simple newbee perl code. where it will open page will take input name and age.

now i need to carry over this name and age to a different page show.pl

Can any one help me to finish the code please about how i will work with session.

thanks for your input, and i am interested on code rather then forwarding me to goggle link. thanks

#!/usr/bin/perl -T
use strict;
use CGI qw/:standard/;
use CGI::Session;
$session = new CGI::Session() or die CGI::Session->errstr;
print $session->header();

print header,
start_html('Hello'),
start_form(-method=>'POST',-action=>"/cgi-perl/show.pl"),
"Enter your name: ",textfield('name'),
"Enter your age : ",textfield('age'),
submit,
end_form,
hr,
end_html;
exit;

Open in new window

Avatar of kawas
kawas
Flag of United States of America image

look at the session perldoc http://search.cpan.org/~sherzodr/CGI-Session-3.95/Session.pm

The script creates an html form. You then need to create another script show.pl that reads in the values for the textfields and then you can save the fields in a session. Until then, there is nothing to do in this script.
Avatar of fosiul01

ASKER


i was looking at that link from last night!!!

now i know how to create and access session

but it would be best if you help to edit my code to show how will you do 2thing:

username and password with post

then

if username is fosiul and password is demo

then go to index.pl page


i know i will be able to do that, but it will take time as i am new to perl but if you can write this simple code for me, it will help me to learn faster

hope it does make sense



Create session, test.pl
#############################################
#!/usr/bin/perl

  # login.pl
  use CGI ':standard';
$cgi = CGI->new;
  use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;

# Object initialization:
    use CGI::Session;

    my $session = new CGI::Session("driver:File", undef, {Directory=>'/tmp'});

    # getting the effective session id:
    my $CGISESSID = $session->id();

    # storing data in the session
    $session->param('f_name', 'fosiul');
    $session->param('f_last', 'alam');
    # or
    # retrieving data
   # my $f_name = $session->param('f_name');
   # my $f_last = $session->param('f_last');
    #print header;

#print start_html('Hello World');
#print h1('Hello World');

 #print $f_name;
 #print $f_last;
#print end_html();
#exit;
my $cookie = $cgi->cookie(CGISESSID => $session->id);
    print $cgi->header( -cookie=>$cookie );
#########################################################

assessing session :
test1.pl
######################################
#!/usr/bin/perl

  # login.pl
  use CGI ':standard';
$cgi = CGI->new;
  use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;

# Object initialization:
    use CGI::Session;


    # getting the effective session idid = $cgi->cookie("CGISESSID") || undef;
    $session    = new CGI::Session(undef, $sid, {Directory=>'/tmp'});

    # or
    # retrieving data
    my $f_name = $session->param('f_name');
   my $f_last = $session->param('f_last');
    print header;

print start_html('Hello World');
print h1('Hello World');

 print $f_name;
 print $f_last;
print end_html();
exit;

Open in new window

code for mylogin.pl

i just need to implement the logic

if username is fosiul and passworsd is demo

then go to index.pl

but in index.pl, it will check if the user has logged in or not by using session




#!/usr/bin/perl -T
use strict;

use CGI qw/:standard/;

print header,
start_html('Hello'),
start_form(-method=>'POST',-action=>"/cgi-perl/myindex.pl"),
"Enter your name: ",textfield('name'),
"Enter your Password: ",textfield('pass'),
submit,
end_form,
hr,
end_html;
exit;

Open in new window

hmm, i will give you pointers, because i dont have too much time today ...

to create a username / password field in your login page:

start_form(-method=>'POST',-action=>"/cgi-perl/show.pl"),
"Username: ",textfield('name'),
"Password : password_field(-name=>'password', -value=>'', -size=>50, -maxlength=>80),
submit,
end_form,


in your index.pl form,

if ($ENV{REQUEST_METHOD} eq 'POST') {
  my $q = CGI->new;
  my $user = $q->param('name');
  my $pass = $q->param('password');
  # check the password and username ... if correct save session, etc
} else {
 # tell the user that the username/password is incorrect and possibly redirect to previous form
}
ASKER CERTIFIED SOLUTION
Avatar of kawas
kawas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok i will try this by myself.let you know how its goes

but if you have any time today or tomorrow , try to give  me the full code...  please
thanks