?
Solved

Session management with perl

Posted on 2010-03-30
6
Medium Priority
?
481 Views
Last Modified: 2013-12-25
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

0
Comment
Question by:fosiul01
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:kawas
ID: 29094990
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
 
LVL 29

Author Comment

by:fosiul01
ID: 29095651

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
 
LVL 29

Author Comment

by:fosiul01
ID: 29096002
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
LVL 13

Expert Comment

by:kawas
ID: 29096841
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
 
LVL 13

Accepted Solution

by:
kawas earned 2000 total points
ID: 29096934
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
 
LVL 29

Author Comment

by:fosiul01
ID: 29097343
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In a question here at Experts Exchange, a member was looking for "a little app that would allow sound to be turned OFF and ON by simply clicking on an icon in the system tray". This article shows how to achieve that, as well as providing the same OF…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question