A PHP implementation of a password generator for basic authentication on apache web servers using the .htaccess and .htpasswd combination.

Consisting of a simple form powered by a fairly simple PHP script, I havent included the forms source code here as I'm pretty sure if you're reading this then you know exactly what needs to be done in terms of passing data to the actual script.

Live Demo

Source Code


/*
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

<?php

if (isset($_POST['submit'])){
  $error = '';
  $username = trim(stripslashes($_POST['username']));
  $password = trim(stripslashes($_POST['password']));
 
	if ($username == '' || preg_match('/\s:/m', $username)) {
		$error .= "<p><strong>Invalid Username.</strong></p>\n";
	}
 
	if ($password == '' || preg_match('/\s/m', $password)) {
		$error .= "<p><strong>Invalid Password.</strong></p>\n";
	}
 
	if ($error == '') {
		echo "<p class=\"notice success\">" . htmlspecialchars($username) . ":" . crypt($password) . "</p>\n";
	} else {
		echo $error;
	}
}
?>