in reply to Authentication Suggestion in CGI
If you want a method that utilizes cookies that doesn't use any perl modules (although I recommend using modules), you can use this:
sub cookieRead { local(@rawCookies) = split (/; /,$ENV{'HTTP_COOKIE'}); foreach (@rawCookies) { ($key, $val) = split (/=/, $_); $cookie{$key} = $val; } } sub cookieWrite { local($name, $value, $expiration, $path, $domain, $secure) = @_; print "Set-Cookie: "; print ($name, "=", $value, "; path=", $path, "; domain=", $domain, + "; ", $secure, "\n"); }
Here is the code I use to write cookie information:
&cookieWrite("session", "$userName::$Password", "$expDate", "/cgi-bin/ +", "fittrend.com");
Here is the code I use to read cookie information
&cookieRead; ($cookie{'user'}, $cookie{'pass'}) = split (/::/, $cookie{'session'}); &loadUserSession($cookie{'user'});
a couple of things to point out:
1. If the user name or password contains a :: then this code could potentially break.
2. I recommend that you encrypt this information for security reasons. Using Crypt or another modules on cpan. Lastly, if you have a large amount of data, it won't fit on a single variable in the cookie. I just needed mine to track user name and password so I can use other code to load the entire user's profile from a MySQL back-end.
At the time, I was having problems with multiple variables in a cookie, so I simply delimited it. If I spent more time on it, I'd probably fix it.
Hope this helps
-Marc
|
|---|