sub write_cookies {
print "Set-Cookie: reguser=$username; expires=$expires; path=/;\n";
print "Set-Cookie: regpass=$password; expires=$expires; path=/;\n";
print "Set-Cookie: regtype=$regtype; expires=$expires; path=/;\n";
}
So it prints a username, password, and regtype (regtype tells whether they're a member or not). So then I have the following code on pages that I only want members to see:
This works if I only use this script. The scripts checks the database and only let's people in that are registered, and presents a login screen to others. My problem is that I'm also running an online photo album software that sets a 4th entry into my cookie. In IE, my code still works but in Firefox I can only get it to work by changing the 3 to a 4 in this line in the javascript:
var the_regtype = broken_cookie[3];changes to:
var the_regtype = broken_cookie[4];If I change the 3 to a 4, it works in Firefox but not in IE. Is there a way around this? Is this a Firefox bug? If I delete the photo script's cookie my script works fine. Any insight would be appreciated.
On another note. How secure is this type of security? If people can login once, view the source of my HTML and get the javascript code, could they conceivably build their own cookie to get around my script? Thanks. Tom
On another note. How secure is this type of security?Not at all. It would be extremely easy to work around this security entirely. It looks like your example could be worked around by simply disabling JavaScript. The problem is that by using JavaScript, you're trusting the client to do the authentication, and ultimately the client is under the control of the user. You really need to do your authentication on the server, which is (hopefully) under your control.
As sgifford points out, it's insecure. You might be able to do something like create a cookie that has no information other than a random, unique ID number. Fetching the cookie, you could then check it against information kept in your database, to ensure that this user is allowed access. Doing this would prevent the sensitive information from being seen.
Incidentally, I recommend storing the password in an encrypted form. To check password validity, put the login password through the same encryption and compare with the stored value.
BUT
Just to drive it home:
That's not a great way to do session IDs. There are a few reasonably reliable ways:
In either case, save only the session ID in a cookie (or, if you prefer {and are willing to do a little extra work}, you can pass it in the URL and not use cookies at all). In the sessions table, store an expiration time; each time you check for a valid session, you can update the expiration (unless, of course, the session has already expired).
This seems to be the best approach short of implementing some kind of full-featured auth scheme on the server side.
But if you only send the session ID, where is the username coming from?
Can the user establish two sessions at the same time?
That's up to you. ;-) Nothing prevents it, though such things always take a bit of thought to do well.
I prefer simple solutions, and when I am able to fully satisfy the requirement with 50 rows of code, I am not using the modules, having thousands rows, potentially with bugs.
You're... not using modules? Because they might have bugs? I'm willing to bet that the mainstream modules suggested in this conversation have a better test suite than your code, and the advantage of hundreds or thousands of testers. Sorry to be blunt, but that's a really stupid reason to avoid CPAN.
You wrote that my way is not the great, but you did not write why
Because you were randomly generating data, then checking for collisions. The busier your site becomes under those conditions, the more likely a collision, meaning more re-checks. Your site becomes slower at a non-linear rate -- thus, bad idea.
perlmonks.org content © perlmonks.org and blogical, htmanning, pajout, radiantmatrix, sgifford, spiritway
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03