The PHP crypt function is a one-way encryption function that lets you confirm that an entered password matches a stored encrypted one -- without having to decrypt anything. Chris Root explains how it works.
First let's set up a form for entering a username and password to add a new user into the system. Let's use an example of a form for an admin interface to administer a Company X product database for Web display.
When the form is submitted to "admin_login_newuserbe.php" the crypt function is used to encrypt the new username. We first confirm that both fields have entries:
if(empty($_POST[pword]) || empty($_POST[uname])) { echo "<html><head></head> <body><h1>You must enter both a username and password. <a href = \"admin_login_newuser.html\">Try Again? </a></h1></body></html>"; }
Additional validation could be added to this either on the client side (using Javascript) or on the server side as needed. Next we will use crypt to encrypt our password and neatly trim leading and trailing white space from our username. They can then be stored in a database or flat file.
else { $pwrd = crypt(trim("$_POST[pword]")); $user = trim("$_POST[uname]"); //Code for accessing you database or flat file goes here.