As others noted, login procedures vary by implementation, but the main case (simple web application authentication) uses something like the following pseudo-code:
function login(username, password) { user = db->get_user(username) if (user == false) { report_error("Unknown username") exit } if (user->password != hash(password)) { report_error("Incorrect password") exit } // User authenticated, set session cookie session->set_data('current_user', user->username) }
Of course, in most cases it becomes a little more attractive than this, but each login function starts its life, looking mostly like the one shown above. Now, if we add autoline ("remember me") to the mix, we get something like this:
function login(username, password, remember_me) { user = db->get_user(username) if (user == false) { report_error("Unknown username") exit } if (user->password != hash(password)) { report_error("Incorrect password") exit } // User authenticated, set session cookie session->set_data('current_user', user->username) if (remember_me == true) { cookie_token = random_string(50) set_cookie('autologin_cookie', cookie_token, ONE_MONTH) // Finally, save a hash of the random token in the user table db->update_user(user, 'autologin_token', hash(cookie_token)) } }
Plus, the function of automatic login if a cookie is present:
function cookie_login() { cookie = get_cookie('autologin_cookie') if (cookie == false) { return false } // Only for demonstration; cookie should always include username as well user = db->get_user_by_cookie(cookie) if (user == false) { // Corrupt cookie data or deleted user return false } // User authenticated, set session cookie session->set_data('current_user', user->username) return true }
NOTE. The above approach is not a “best practice” and it is not very safe. In production code, you should always include the user ID in the cookie data, use several levels of throttling, store data about failed and successful logins, etc. All of this has been removed to simplify the basic authentication structure.
In any case, I hope this is what you were looking for, koldfyre. I do not know your background, but if you do not know how sessions and cookies you should read them separately, and if you need more detailed information, just ask.
PS: You can also check the " Ultimate Website Authentication Guide " question for practical approaches