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

LVL 29
fosiul01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kawasCommented:
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.
0
fosiul01Author Commented:

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

0
fosiul01Author Commented:
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

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

kawasCommented:
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
}
0
kawasCommented:
oops, had the wrong logic in the ELSE portion ...

You need to check the user if you get a POST

If you get a GET, then you need to show the 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 {
 # show form
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fosiul01Author Commented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.