Link to home
Start Free TrialLog in
Avatar of shahrahulb
shahrahulb

asked on

cgi session

I have a page http://blah/test.cgi
there is a login button on this page

as soon as someone logins i want to change the page http://blah/test.cgi simply by ading couple of buttons.

how can i do that?

Avatar of shahrahulb
shahrahulb

ASKER

in 1 script i m doing
my $session = new CGI::Session("driver:File", undef, {Directory=>'/u/uname/www/temp/'});
my $CGISESSID = $session->id();
$session->param(-name=>'l_name', -value=>$CGISESSID);

how do i retriev the session id value in my other script
when i run 1.cgi, it is creating the file cgisess_sessionid in /u/uname/www/temp directory
when i do cat cgisess_1ea55e0535761d4361c1c3abcf3fe092
$D = {"l_name" => "1ea55e0535761d4361c1c3abcf3fe092","_SESSION_EXPIRE_LIST" => {},"_SESSION_REMOTE_ADDR" => "172.19.31.217","_SESSION_ATIME" => "1124205645","_SESSION_CTIME" => "1124205645","_SESSION_ID" => "1ea55e0535761d4361c1c3abcf3fe092","_SESSION_ETIME" => undef};

how do i retrieve l_name in my other script 2.cgi
does it always create the file with name cgises_sessionid
Avatar of ozo
Did you see
perldoc CGI::Session::Tutorial
i have seen but i m stuck at 1 point:
 in 1 script i m doing and passing $CGISESSID to script 2 using form parameters.
my $session = new CGI::Session("driver:File", undef, {Directory=>'/u/uname/www/temp/'});
my $CGISESSID = $session->id();
$session->param(-name=>'l_name', -value=>$CGISESSID);

how do i retriev the session id value in my other script from the file so that i can compare that with sessionid of the form parameter
under the heading:  ACCESSING STORED DATA

it says  name = $session->param("my_name");
but how do i initialize session in script 2
why do u have to initalize a new session in script2 ?as far as i underatnd, you want to maintain state between script1 and script2. In that case, in script 1, initialize a session and send the session id as a cookie to the client. In script2, do not initalize. Rather read the cookie and initalize the sesison by passing the second argument to the constructor as the cookie value obtained instead of undef.

Manav
yes i want to maintain session between script 1 and script 2.
in script 1 i m initializing a session and saving in disk
my $session = new CGI::Session("driver:File", undef, {Directory=>'/u/uname/www/temp/'});

now in script 2, how do i retrieve this session id from disk so that i can compare this value with the session id passed thru the url as query parameter
i dont want to use cookies.
Then you can pass it as a hidden field in any of your form.
Here is what I am planning to do. Please let me know, if I am correct.

In my script 1, I will do
my $session = new CGI::Session("driver:File", undef, {Directory=>'/u/uname/www/temp/'});
my $CGISESSID = $session->id();
$session->param(-name=>'l_name', -value=>$CGISESSID);
$session->expire('+5m');

what this does basically is, it creates a file in temp directory
> cat cgisess_1ea55e0535761d4361c1c3abcf3fe092
$D = {"l_name" => "1ea55e0535761d4361c1c3abcf3fe092","_SESSION_EXPIRE_LIST" => {},"_SESSION_REMOTE_ADDR" => "172.19.31.217","_SESSION_ATIME" => "1124205645","_SESSION_CTIME" => "1124205645","_SESSION_ID" => "1ea55e0535761d4361c1c3abcf3fe092","_SESSION_ETIME" => 300};

Now from script 1 i go to page script 2 and pass the parameter, sid=1ea55e0535761d4361c1c3abcf3fe092

In script 2, i will grab the parameter, $sid = param('sid')
then i will open the file  cgisess_$sid and grab the paramter "_SESSION_ID" if this value matches with $sid, means the session is valid, otherwise invalid.

Does this makes sense?

The only problem how can i delete that session, if i don't delete, the file cgisess_1ea55e0535761d4361c1c3abcf3fe092 will remain on the server forever. I can delete if someone hits logout button,but what if someone does not hit logout button and close the browser.
1)
Instead of setting sesion id as a session parameter(which sounds really confusing), pass it as a hidden value in the form. In script2, which is called by the above-mentioned form, access the hidden param using a CGI object.
Alternatively, you can set the hidden field name as CGISESSID in script1. In this case, you can directly pass the CGI object for initializing the session in script2.

$session_for_script_2 = new CGI::Session(undef, $cgi, {Directory=>"/u/uname/www/temp/"}); ##in script2

This will automatically extract the session id as either the cookie or the hidden field. The session will be initialized with the same session id as the id obtaioned in script1. In script2, you need not access the file directly. The session so initialized can be used to fetch the session parameters set.
For example, if my_name was a session parameter set in script1, you can access it in script2 using
$my_new_name = $session_id_for_script2->param('my_name') ;

$session_delete() will clear the session from disk.
this seems to be more clear. still what is $cgi in script 2

but what do u think, should i use session or can i use CGI::Cookie which is more simpler and no need o save data on the disk
also manav can u please answer https://www.experts-exchange.com/questions/21530161/expert-exchange-cookie-issue.html

i need to decide whether to go for CGI::Session or CGI::Cookie
> this seems to be more clear. still what is $cgi in script 2

$cgi is a CGI object
$cgi = new CGI ;

>but what do u think, should i use session or can i use CGI::Cookie which is more simpler and no
need o save data on the disk

Cookie is one of the method of handling CGI state sessions, the other being hidden fields.
It depends on

i guess even expert exchange works on cookie EELOGIN
if i manually delete the cookie EELOGIn then i have to re login, if i disable cookie, it says, cookies should be eanbled. my question is how do i come to know whether clients browser is acception or rejecting cookies?
ASKER CERTIFIED SOLUTION
Avatar of manav_mathur
manav_mathur

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
I was using the example given on CGI::Application to build a login page and display the user home. The login page does not seem to work and no errors occur, the login page keeps looping on to itself.

I have the template file using the "rm" to change the runmode to validating the login page, the sub validating the user input is looped back to the login page, exactly the way the example does.

the subsequent run methods are defined,

what could I have done is the design of the flow that could have caused this flaw. What are the usual mistakes most people commit.

thanks,
Rana