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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read cookie when form submitted

Status
Not open for further replies.

Roebuck

Technical User
Apr 23, 2003
14
0
0
GB
I have a problem with a script which I want to read a previously set cookie when a form is submitted. The aim is to check the value of the cookie, then process the form depending on the result.

The read cookie routine (example from script below) seems to work only when there is no form parse routine called elsewhere in the script.

Is it possible to do these 2 things together?

Any ideas greatly appreciated...

#####
use CGI;
&ReadCookie;
sub ReadCookie {
$query = new CGI;

print $query -> header;
print &quot;<HTML><head><title>Cookies</title></head><body>&quot;;

print &quot;<table border=\&quot;1\&quot;>\n&quot;;
print &quot;<tr><th>name</th><th>value</th></tr>\n&quot;;
foreach my $cookie_name ($query->cookie()){
print &quot;<tr><td>Name: $cookie_name</td>\n&quot;;
print &quot;<td>Value: &quot;,$query->cookie
($cookie_name),&quot;</td></tr>\n&quot;;
}
print &quot;</table>&quot;;
}
#####

Thanks.
 
It depends on the logic of the entire CGI script but from your explanation, you shouldn't be doing them at the same time anyway. You should be reading the cookie first, then, depending on the contents (or existance) of the cookie, you would then process the form data. An &quot;if/else&quot; statement would seem to be in order here:

read cookie....

if (something about the cookie)
parse the form data
else
do something else

There's always a better way. The fun is trying to find it!
 
It should work. What kind of error are you getting?

Kevin
A+, Network+, MCP
 

The problem is still not solved...

I have tried reading/writing cookies using the 'use CGI;
$query = new CGI;' method and it works fine if no form data is parsed.

If I include a call to parse form data (using a parsform script from matt's script archive) either before or after reading/writing a cookie the script just freezes and can't be called again for a while (until something times out?).

Does anyone have sample code that shows this process working? The aim is to submit data from a form field, read a cookie, parse the form data then show the values from both the form & the cookie.

I'm probably missing something simple... Thanks again.
 
Here's some code that's working for me. I tried to leave in only the more relevant lines. This program checks the username/password stored in the client's cookie and verifies it against a database. It then checks a cgi parameter to see if a file was submitted and gets the file if it was. If not, then it displays the form to select and upload a file.

Code:
...
if(!&loggedIn){
  print $cgi->redirect('login.cgi');
  exit(0);
}
print $cgi->header();
print $cgi->start_html();
...
if($cgi->param('images')){
  #Get a file from a submitted form
}
else{&showForm();}
print $cgi->end_html();

######################
sub loggedIn{
  my $user = $cgi->cookie(-name=>'user');
  my $password = $cgi->cookie(-name=>'pass');

  ## Do some database stuff to verify username/password
}


Kevin
A+, Network+, MCP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top