Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI:Session not working 1

Status
Not open for further replies.

NigeW

Programmer
Jun 10, 2002
134
NZ
Hi all

Have been trying to get CGI:Session integrated on my site . . .

I am wanting to store some variables in session ID's for retrieval at a later date.

My application is a content management system, with user authentication.

So the process is :

1. User logs in and a session variable gets set for the username.
2. Page redirects to content management.
3. Session variable needs to be retrieved.

So the extract of my user validation code is :
################################
use CGI:Session;
my $session = new CGI::Session("driver:File", undef, {Directory=>"/tmp"});
$session->param(-name=>'name', -value=>$username);
################################

And the extract of my other pages is :
################################
use CGI::Session
my $name = $session->param('name');
print "Your name is " . $name;
################################

Basically I am not getting any output (not even the words 'Your name is')

Any ideas ??

Thanks in advance

Nigel Wilson
"kiwi-kid"
 
First, read through the CGI::Session tutorial.

I believe if you were to check your cookies that you wouldn't see one set with the session for your site. This is because you're creating the session but not sending the session id as a cookie to the user.

Similarly, you're not getting the session id from the user's cookie when you go to get the username. You're calling $session->param, but according to your code $session hasn't been initialized.

Here's what I've used that's been successful for me:
Code:
use CGI;
use CGI::Session;

$cgi = new CGI;
## Get the existing session or create a new one if necessary
$session = new CGI::Session("driver:File", $cgi, Directory=>"/tmp"});
## Create a cookie with the session ID
$cookie = $cgi->cookie(CGISESSID => $session->id );
## Send the session ID cookie in case this is a new session
print $cgi->header(-cookie=>$cookie);

 
Hi Philote

Thanks for the feedback - I'll take a look 2nite and see how things go.

From what you are saying obviously the big issue (which I haven't picked up on) is the need to store the session ID and then use that to retrieve other user defined session variables.


Nigel Wilson
"kiwi-kid"
 
Right, and you can either store the session ID as a cookie or pass it in the url.

 
Wahooo - Philote - you're a legend - got things working in a couple of minutes . . .

Thanks for highlighting where I was goinf wrong - I was dizzy from going rounf in circles.

2 stars for you ! (that is if you can assign 2)

Nigel Wilson
"kiwi-kid"
 
Heh, you're welcome. Sessions are a bit confusing at first.

 
OK - now that I can set a session ID and session variables I am having problems using the session variables.

I have set a session variable however cannot use the variable as part of a string to open a text file, as follows :

$data_file = $session->param("siteURL");
open(DAT, $data_file . "myfile.txt" ) || die("Could not open file!");

I can print the variable to screen and it display as expected.

Any ideas ?

Thanks in advance.



Nigel Wilson
"kiwi-kid"
 
Ignore above post - call it loser error - spent 2 hours going round in circles and couldn't see the wood for the trees.

Time I gave up on programming I think !!

Nigel Wilson
"kiwi-kid"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top