PHP Strings Primer - User Authentication
(Page 36 of 37 )
In PHP the greatest use of the 'md5()' function is for user authentication. Rather than store a user's password on the server, many programmers opt to store only an md5 hash of the password. This way of doing things is much more secure than storing the password itself.
In order to implement this technique, you must store an md5 hash of the user's password when they first register or sign up for a login. Then, every time they log into the web site, you generate an md5 hash of the password they entered to login and compare it against the stored value. If they match, access is granted. Otherwise, it is not.
Here is a simple script that illustrates how this could be done. This script assumes that a form has been submitted and that the password the user entered is stored in the '$_POST['password']' variable. The value that we will set into the '$storedpassword' variable is an md5 has of the word 'password'.
<?php $storedpassword = '5f4dcc3b5aa765d61d8327deb882cf99';
if(md5($_POST['password']) == $storedpassword) { Â Â Â echo "Access is granted!"; } else { Â Â Â echo "Bad Password."; } ?> |
Next: Conclusion >>
More Programming Basics Articles
More By Matt Wade