Session Problem in Perl

Discussion in 'Perl Misc' started by Praki, Dec 11, 2007.

  1. Praki

    Praki Guest

    Greetings All,

    i m creating a session storing the loging credentials. i want to
    access that data later after crossing two pages.i have created the
    sesion but i could not get the data back some where i m going wrong
    can u point me where ..

    my first first file getting the login credentials

    #!/usr/local/bin/perl5
    &top();
    &login_check();
    &bottom();
    sub top{
    print <<EOM1;
    Content-type: text/html


    <html>
    <head>
    <title>Login Authentication</title>
    </head>
    <body>
    EOM1
    }
    sub bottom{
    print <<EOM2;
    </body>
    </html>
    EOM2
    }
    sub login_check{
    print <<EOM;
    <h3>Enter the login Password</h3>
    <form method="post" action="cookie.cgi">
    <input type="hidden" name="username" value='Submit'>
    <p>Username: <input type="text" name="uid" size="20"></p>
    <p>Password: <input type="password" name="passwd" size="20"></p>
    <input type="submit" value="Submit" name="submit">
    </form>
    EOM
    }

    second file where i m creating the session.

    #!/usr/local/bin/perl5
    use Net::LDAP;
    &top();
    &login_check();
    &bottom();

    sub top{
    print <<EOM1;
    Content-type: text/html


    <html>
    <head>
    <title>Login Authentication</title>
    </head>
    <body>
    EOM1
    }
    sub bottom{
    print <<EOM2;
    </body>
    </html>
    EOM2
    }
    sub login_check{
    my ($ret);
    use CGI;
    my $query = new CGI;
    $query->param("submit");
    my $userid = $query->param("uid");
    my $password = $query->param("passwd");
    use CGI;
    use CGI::Session;

    $session = new CGI::Session("driver:File",undef,{'Directory'=>"/
    tmp"});
    $sid = $session->id();
    print "Session id:".$sid;

    #$session->name($userid);
    # Write the session variable on the server
    $session->param('username',$userid);
    $session->param('password',$password);
    print "username:".$userid."pass:";

    $usernam = $session->param('username');
    $passwor = $session->param('password');

    print "username from the cookie:".$usernam;

    #create session cookie
    $cookie = $query->cookie(CGISESSID => $session->id);
    print $query->header( -cookie=>$cookie );
    #print "cookie:".$cookie;
    print <<EOM;
    <h5>Cookie created <a href="/cgi-bin/sesion/cookie/
    test.cgi">click here to check</a> </h5>
    EOM
    }

    Third file to access the session values.

    #!/usr/local/bin/perl5
    &top();
    &cookie_check();
    &bottom();
    sub top{
    print <<EOM1;
    Content-type: text/html


    <html>
    <head>
    <title>Login Authentication</title>
    </head>
    <body>
    EOM1
    }
    sub bottom{
    print <<EOM2;
    </body>
    </html>
    EOM2
    }
    sub cookie_check{
    use CGI;
    use CGI::Session;
    my $query = new CGI;

    $sid = $query->cookie('CGISESSID') || $query->param('CGISESSID') ||
    undef;
    $session = new CGI::Session(undef, $sid, {Directory=>'/tmp'});
    $username = $session->param('username');
    $password = $session->param('password');

    print <<EOM;
    <h3>Cookie test $username $password</h3>
    EOM
    }

    i m not getting the values when i read here. i dont know where the
    cookie is created. i check with the server i could see the session
    which is created in the server.please tell me where i m going
    wrong.....

    thanks in advance..
    Prakash
     
    Praki, Dec 11, 2007
    #1
    1. Advertising

  2. Praki

    Ron Bergin Guest

    On Dec 11, 7:55 am, Praki <> wrote:
    > Greetings All,
    >
    > i m creating a session storing the loging credentials. i want to
    > access that data later after crossing two pages.i have created the
    > sesion but i could not get the data back some where i m going wrong
    > can u point me where ..
    >
    > my first first file getting the login credentials
    >
    > #!/usr/local/bin/perl5
    > &top();
    > &login_check();
    > &bottom();
    > sub top{
    > print <<EOM1;
    > Content-type: text/html
    >
    > <html>
    > <head>
    > <title>Login Authentication</title>
    > </head>
    > <body>
    > EOM1}
    >
    > sub bottom{
    > print <<EOM2;
    > </body>
    > </html>
    > EOM2}
    >
    > sub login_check{
    > print <<EOM;
    > <h3>Enter the login Password</h3>
    > <form method="post" action="cookie.cgi">
    > <input type="hidden" name="username" value='Submit'>
    > <p>Username: <input type="text" name="uid" size="20"></p>
    > <p>Password: <input type="password" name="passwd" size="20"></p>
    > <input type="submit" value="Submit" name="submit">
    > </form>
    > EOM
    >
    > }
    >
    > second file where i m creating the session.
    >
    > #!/usr/local/bin/perl5
    > use Net::LDAP;
    > &top();
    > &login_check();
    > &bottom();
    >
    > sub top{
    > print <<EOM1;
    > Content-type: text/html
    >
    > <html>
    > <head>
    > <title>Login Authentication</title>
    > </head>
    > <body>
    > EOM1}
    >
    > sub bottom{
    > print <<EOM2;
    > </body>
    > </html>
    > EOM2}
    >
    > sub login_check{
    > my ($ret);
    > use CGI;
    > my $query = new CGI;
    > $query->param("submit");
    > my $userid = $query->param("uid");
    > my $password = $query->param("passwd");
    > use CGI;
    > use CGI::Session;
    >
    > $session = new CGI::Session("driver:File",undef,{'Directory'=>"/
    > tmp"});
    > $sid = $session->id();
    > print "Session id:".$sid;
    >
    > #$session->name($userid);
    > # Write the session variable on the server
    > $session->param('username',$userid);
    > $session->param('password',$password);
    > print "username:".$userid."pass:";
    >
    > $usernam = $session->param('username');
    > $passwor = $session->param('password');
    >
    > print "username from the cookie:".$usernam;
    >
    > #create session cookie
    > $cookie = $query->cookie(CGISESSID => $session->id);
    > print $query->header( -cookie=>$cookie );
    > #print "cookie:".$cookie;
    > print <<EOM;
    > <h5>Cookie created <a href="/cgi-bin/sesion/cookie/
    > test.cgi">click here to check</a> </h5>
    > EOM
    >
    > }
    >
    > Third file to access the session values.
    >
    > #!/usr/local/bin/perl5
    > &top();
    > &cookie_check();
    > &bottom();
    > sub top{
    > print <<EOM1;
    > Content-type: text/html
    >
    > <html>
    > <head>
    > <title>Login Authentication</title>
    > </head>
    > <body>
    > EOM1}
    >
    > sub bottom{
    > print <<EOM2;
    > </body>
    > </html>
    > EOM2}
    >
    > sub cookie_check{
    > use CGI;
    > use CGI::Session;
    > my $query = new CGI;
    >
    > $sid = $query->cookie('CGISESSID') || $query->param('CGISESSID') ||
    > undef;
    > $session = new CGI::Session(undef, $sid, {Directory=>'/tmp'});
    > $username = $session->param('username');
    > $password = $session->param('password');
    >
    > print <<EOM;
    > <h3>Cookie test $username $password</h3>
    > EOM
    >
    > }
    >
    > i m not getting the values when i read here. i dont know where the
    > cookie is created. i check with the server i could see the session
    > which is created in the server.please tell me where i m going
    > wrong.....
    >
    > thanks in advance..
    > Prakash


    There are a number of issues that should be addressed, but the main
    issue is in cookie.cgi. Your &top sub is outputting headers without
    the session cookie and then &login_check tries to output the headers
    again, this time with the session cookie, but it's too late. You can
    only send the header once.
     
    Ron Bergin, Dec 11, 2007
    #2
    1. Advertising

  3. Praki

    Praki Guest

    On Dec 12, 1:41 am, Ron Bergin <> wrote:
    > On Dec 11, 7:55 am, Praki <> wrote:
    >
    >
    >
    > > Greetings All,

    >
    > > i m creating a session storing the loging credentials. i want to
    > > access that data later after crossing two pages.i have created the
    > > sesion but i could not get the data back some where i m going wrong
    > > can u point me where ..

    >
    > > my first first file getting the login credentials

    >
    > > #!/usr/local/bin/perl5
    > > &top();
    > > &login_check();
    > > &bottom();
    > > sub top{
    > > print <<EOM1;
    > > Content-type: text/html

    >
    > > <html>
    > > <head>
    > > <title>Login Authentication</title>
    > > </head>
    > > <body>
    > > EOM1}

    >
    > > sub bottom{
    > > print <<EOM2;
    > > </body>
    > > </html>
    > > EOM2}

    >
    > > sub login_check{
    > > print <<EOM;
    > > <h3>Enter the login Password</h3>
    > > <form method="post" action="cookie.cgi">
    > > <input type="hidden" name="username" value='Submit'>
    > > <p>Username: <input type="text" name="uid" size="20"></p>
    > > <p>Password: <input type="password" name="passwd" size="20"></p>
    > > <input type="submit" value="Submit" name="submit">
    > > </form>
    > > EOM

    >
    > > }

    >
    > > second file where i m creating the session.

    >
    > > #!/usr/local/bin/perl5
    > > use Net::LDAP;
    > > &top();
    > > &login_check();
    > > &bottom();

    >
    > > sub top{
    > > print <<EOM1;
    > > Content-type: text/html

    >
    > > <html>
    > > <head>
    > > <title>Login Authentication</title>
    > > </head>
    > > <body>
    > > EOM1}

    >
    > > sub bottom{
    > > print <<EOM2;
    > > </body>
    > > </html>
    > > EOM2}

    >
    > > sub login_check{
    > > my ($ret);
    > > use CGI;
    > > my $query = new CGI;
    > > $query->param("submit");
    > > my $userid = $query->param("uid");
    > > my $password = $query->param("passwd");
    > > use CGI;
    > > use CGI::Session;

    >
    > > $session = new CGI::Session("driver:File",undef,{'Directory'=>"/
    > > tmp"});
    > > $sid = $session->id();
    > > print "Session id:".$sid;

    >
    > > #$session->name($userid);
    > > # Write the session variable on the server
    > > $session->param('username',$userid);
    > > $session->param('password',$password);
    > > print "username:".$userid."pass:";

    >
    > > $usernam = $session->param('username');
    > > $passwor = $session->param('password');

    >
    > > print "username from the cookie:".$usernam;

    >
    > > #create session cookie
    > > $cookie = $query->cookie(CGISESSID => $session->id);
    > > print $query->header( -cookie=>$cookie );
    > > #print "cookie:".$cookie;
    > > print <<EOM;
    > > <h5>Cookie created <a href="/cgi-bin/sesion/cookie/
    > > test.cgi">click here to check</a> </h5>
    > > EOM

    >
    > > }

    >
    > > Third file to access the session values.

    >
    > > #!/usr/local/bin/perl5
    > > &top();
    > > &cookie_check();
    > > &bottom();
    > > sub top{
    > > print <<EOM1;
    > > Content-type: text/html

    >
    > > <html>
    > > <head>
    > > <title>Login Authentication</title>
    > > </head>
    > > <body>
    > > EOM1}

    >
    > > sub bottom{
    > > print <<EOM2;
    > > </body>
    > > </html>
    > > EOM2}

    >
    > > sub cookie_check{
    > > use CGI;
    > > use CGI::Session;
    > > my $query = new CGI;

    >
    > > $sid = $query->cookie('CGISESSID') || $query->param('CGISESSID') ||
    > > undef;
    > > $session = new CGI::Session(undef, $sid, {Directory=>'/tmp'});
    > > $username = $session->param('username');
    > > $password = $session->param('password');

    >
    > > print <<EOM;
    > > <h3>Cookie test $username $password</h3>
    > > EOM

    >
    > > }

    >
    > > i m not getting the values when i read here. i dont know where the
    > > cookie is created. i check with the server i could see the session
    > > which is created in the server.please tell me where i m going
    > > wrong.....

    >
    > > thanks in advance..
    > > Prakash

    >
    > There are a number of issues that should be addressed, but the main
    > issue is in cookie.cgi. Your &top sub is outputting headers without
    > the session cookie and then &login_check tries to output the headers
    > again, this time with the session cookie, but it's too late. You can
    > only send the header once.


    thanks for your input its working fine ...
    thanks...
    Prakash
     
    Praki, Dec 12, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,824
  2. Jeff Smythe
    Replies:
    3
    Views:
    1,269
    Jeff Smythe
    Jan 2, 2004
  3. =?Utf-8?B?Um9iSEs=?=
    Replies:
    4
    Views:
    5,303
    =?Utf-8?B?Um9iSEs=?=
    Apr 11, 2007
  4. Jazzis
    Replies:
    2
    Views:
    256
    Jazzis
    Sep 23, 2003
  5. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    310
    PerlFAQ Server
    Jan 23, 2011
Loading...

Share This Page