The way you encrypt the password into MySQL is by inserting the new user into the database and setting the password to equal the md5() hash:
Code: Select all
$query = "INSERT INTO members (username, password) VALUES ('".$username."', '".md5($password)."')";
Where $username and $password are set to variables which the user enters in when they sign up on the site. Usually, these will be $_POST['username'] and $_POST['password'].
In response to your second question about cookies...... A cookie is used to store a variable on the user's computer so that when they return to the site, a PHP program can "remember" certain things about them. I'm sure you have seen a site that has "Remember login" or something similar in a login box which you can choose so that you can return to the site at a later date and not have to log in again. If you check the box to "Remember you", then they are most likely storing a cookie on your computer. The cookie will store your username and your encrypted password. This way, when you return to the site, if the cookie exists on your computer, then the script will read it in, check the username/password against the database values, and log you in accordingly.
Code: Select all
$_COOKIE['my_site_login_info'] = $username.",".md5($password);
The above code would be used to set the cookie. This is done when the user chooses to have their password remembered.
Code: Select all
if (isset($_COOKIE['my_site_login_info'])) {
list($username, $encrypted_password) = explode(",", $_COOKIE['my_site_login_info']);
$query = "SELECT * FROM members WHERE username = '".$username."' AND password = '".$encrypted_password."'";
} else {
// the cookie isn't set, so either the user hasn't chosen to remember
// the password, cookies are disabled on his computer, or he deleted
// the cookie himself
}
The above code will check to see if the cookie exists. If it does, then we grab the user's information from the database.
Hopefully this helps a bit. Feel free to ask more questions that you may have.