PHP Username/Password Script

potentialeight

Expert Gambling Writer
Oct 30, 2010
2,201
41
0
NC
www.potentialeight.com
I've been considering creating password-protected pages to maintain an archived list of articles I've written for specific clients. The idea would be to give the clients a username and password as an extra perk so that they would have access to backup copies of any work I've done for them. I found the following script with a quick Google search and I understand enough about PHP to know how this works, but I don't understand enough about PHP to know its vulnerabilities. If I create the passwords based on random strings and just email them to my clients to keep for their records, and there is no personal information on any of the pages being protected, what problems can I run into with using this?

Code:
<?php 

// Define your username and password 
$username = "someuser"; 
$password = "somepassword"; 

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) { 

?> 

<h1>Login</h1> 

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    <p><label for="txtUsername">Username:</label> 
    <br /><input type="text" title="Enter your Username" name="txtUsername" /></p> 

    <p><label for="txtpassword">Password:</label> 
    <br /><input type="password" title="Enter your password" name="txtPassword" /></p> 

    <p><input type="submit" name="Submit" value="Login" /></p> 

</form> 

<?php 

} 
else { 

?> 

<p>This is the protected page. Your private content goes here.</p> 

<?php 

} 

?>
 


The generally accepted practice is to not store a plaintext version of their password anywhere. If you need to send it to them, just send it to them once but don't keep it. What you DO keep is a hash of the password (like md5 or sha1). A hash is just an encryption algorithm that generates a unique hash of X characters (an md5 is 32 characters) for every string. So the hash of "mypassword" will always be the same. If you keep the hash, then all you need to do when they enter their password is compare the hash of what they entered to what you have stored.

Code:
<?php $str = 'mypassword'; md5($str); // generate the md5 hash of 'mypassword' ?>
 
  • Like
Reactions: potentialeight