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

set cookie and redirect

Status
Not open for further replies.

ibjdt

Programmer
Nov 25, 2002
63
can a script print cookies to the header and redirect?

i have an shtml page calling a perl cgi script through ssi.
the script checks for a sessionID cookie. if not found it displays a login form. the login form points to another cgi script that handles the login, creates a cookie and redirects back to the shtml page.

my intent was that when redirected back, the ssi would find the new cookie and not post the login form. however, although the login script works (it adds my login to the login database), the cookie doesn't print to the header so it isn't found.

normally i create the cookie with

Code:
print $cgi->header(-cookie=>[@cookies]);

but if i do that before the redirect, the resulting page literally shows the redirect path on screen:

"Location:
any suggestions??

thanks.
 
You can always use a javascript redirect.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
how would that work?
the redirect occurs in the perl login script.
once the cookie is created (and usually printed to the header) i redirect the perl script back to the shtml page.

how would i include javascript in the perl script to redirect back to the shtml page?

thanks.

 
i found a solution - i don't know if it is technically correct, but it works.

- in shtml main page - normal SSI to call 'cookie check' perl script
- if cookie - get username and print welcome message
- if not cookie - print login form
- enter username/password in login form - action calls validation script
- validation script creates cookie then.....

Code:
print $cgi->header(-cookie=>[@cookies]);
print "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2//EN'>";
print "<html><head><title>$page_title</title>";
print "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=http://www.backtomainpage'>";
print "</HEAD><BODY></BODY></HTML>";
exit;

since it redirects back to the page i started on, the SSI does its thing again, finds the cookie and therefore displays the welcome message instead of the login form.

hopefully this will help others like me who struggle sometimes.

thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top