Discuss Using CGI.pm for cookies in the Perl Programming forum on Dev Shed. Using CGI.pm for cookies Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 863
Time spent in forums: 22 sec
Reputation Power: 12
Out of curiosity, what is the alternative to storing a password in a cookie, if you're not using a secure connection or having logins being handled by the browser?
Posts: 4,635
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 80
Your alternative is to create a cryptographically secure session id, send that as the value of the cookie, and store user info linked to that session id server-side to match up users later.
When a user comes back to the site, they send the session id in the cookie you set. You have your script look up the session id in your database and get user info from there. Much better than storing username and password info client-side.
I suggest using Digest::MD5's md5_hex() method (don't use md5)base64() because it includes characters that could break a cookie) to hash some random info (like localtime() and some random text) and use that as your session id. These types of ids would be VERY hard to crack, if it's even practically possible.
Posts: 44
Time spent in forums: < 1 sec
Reputation Power: 12
I'm alreadly using CGI ':standard' so I don't have to use CGI::Cookie anymore, right?
The cookies that are produced at the moment look like that:
-------------------------------------------------------------------------------
user=username; domain=www.mydomain.org; path=/; expires=Saturday, 23-Feb-2002 24:00:00 GMT; secure
Posts: 4,635
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 80
Quote:
I'm alreadly using CGI ':standard' so I don't have to use CGI::Cookie anymore, right?
Nope. Just call the methods for setting/getting cookies like you would any CGI.pm method.
I think the problem might be with your -Refresh in the header. CGI.pm will allow you to send anything you want in an HTTP header, and I don't think that will work as you intend it. If you want to do a redirect with a cookie set/delete, you should either do it like this-
in the <head> statement of a page you return with your cookie you delete above.
You can simplify your "expire" thing by just using the shortcuts provided by CGI- for instance, just set the expires to "-1d" (expire yesterday, which deletes the cookie) and let CGI.pm figure out the correct date syntax. If your date syntax is bungled (1970 is awfully early, and it wouldn't surprise me if browsers didn't handle it correctly) you'll probably get strange errors.
One other trick I use is pass basic info along with a redirect. For instance:
I then have my login.pl script look for $q->param('logged_out') on invocation- if it exists, I have my script print a message that says "You have successfully logged out. Thanks!". I actually think this looks more professional, because it avoids the time-wasting "wait three seconds and redirect" stuff. I hate that type of coding. Since when is it user-friendly to make folks wait on purpose?
Please tell me that the %DATA hash isn't a hand-rolled form parser. Given that you're using CGI.pm already, as you should be, you can get your form data with param('parameter_name'), which is one of the best reasons to use CGI.pm in the first place.
And you aren't going to be storing usernames and passwords in cookies, right? This is a terrible idea.
Posts: 44
Time spent in forums: < 1 sec
Reputation Power: 12
Only with Netscape?
Alright,
thank you very much for you detailed answer ;-)
a) I'm using -location instead of -refresh in my header() now
b) Instead of an exact date I'm using "+1d" and "-1d" in my expiration
c) I like your idea with the logged_out=1 thing and I will use it after I made this cookie thing work, for sure
d) the %DATA hash really came from some parser, but then I found out that the param() method is able to handle "post" and "get" forms and now I'm not using this parser anymore, but I did not want to change the whole variable names ;-)
e) This cookie with the user//password is just a test because I'm trying to build my very first mysql database at the moment, although I'm using PERL for about a year now.
BUT AFTER ALL:
THE COOKIE STILL DOES NOT WORK
Maybe this cookie() function just works with Netscape Navigator, but I cannot image that.
Posts: 44
Time spent in forums: < 1 sec
Reputation Power: 12
I think it might be, that I have a fundamental problem with cookies on my computer, but I'm not sure, because on sites like devshed.com etc. my login is not saved.
My complete code would be much too confusing, I think, but I amost posted the whole think, that regards the cookie.
I just create the cookie with cookie() from CGI.pm and then I try to set it with
Posts: 4,635
Time spent in forums: 4 Days 23 h 44 m 19 sec
Reputation Power: 80
Read the docs supplied with your distribution of perl, there are plenty of sample scripts there.
You can read them by typing "perldoc CGI" at a command prompt, or online at www.perldoc.com.
It's HIGHLY unlikely that you don't have CGI.pm installed- it's been a standard module since perl4 and up. Search the forum next time, there are plenty of scripts here that use CGI.