Saving Usernames and Passwords in the Database

Often we will want several users with different passwords to be able to log in.

We might also want to allow new users to register, or old users to change passwords.

In this case we should store the password information in a database or in a file.

We might create a Users table and inserte a couple of users as follows:

CREATE TABLE `Users` (
`name` CHAR(10) NOT NULL,
`passwd` CHAR(32) NOT NULL,
PRIMARY KEY (`name`),
UNIQUE (`name`)
)
INSERT INTO Users (name, passwd) VALUES
("Thomas",MD5("MyPass")),
("Homer",MD5("Homer"));

Note the use of MD5 in MySQL.

The following code implements a password check against the Users table, assuming that connect() makes a connection to the Database containing the table.

You may test it here.