Computer
Computer
Storing Passwords
Never store password as clear text or in a form that can be decrypted. On the
client side use a hash function in JavaScript to hash the password before it is
sent to the server. Think of the hash function as a one-way encryption. You can get
JavaScript MD5 hash functions at this site. On the server side use the crypt()
function to generate a one-way encryption.
$password = crypt('mypassword');
// store the encrypted password in a file and compare the
// encrypted version of the user input with this password
if (crypt($user_input, $password) == $password)
{
echo "Password verified";
}
Do Not Trust Input Data
Input data will be coming in through forms. You will be doing two checks on the
input data - at the client side using JavaScript and on the sever side using PHP.
Always have an upper limit on the amount of data that you are willing to accept.
This can be checked on the client side as well as on the server side. Impose what
characters are acceptable to you - like alphanumeric and check using regular
expressions.
If you are accepting user input such as comments to a guestbook that you are then
using for others to view be sure to strip anything wrapped in HTML tags. There are
several PHP functions that allow you to remove the tags - strip_tags() and
htmlentities().
To handle remote form posting generate a token based on a random String and
timestamp and place that token into a Session variable and the form. Once the form
is submitted check to see if the two tokens match. The token is changed each time
the form is created so a would be hacker cannot make a permanent Web form to post
unwanted requests to your application.