webmaster tutorials and internet facts

Generate .htpasswd with PHP

htaccessOne of our tutorial is about protecting file or directory with .htaccess and .htpasswd apache configuration files. Due to raising interest on this issue we decided to create tutorial on how to generate .htpasswd file with PHP online, you can create a great free tool for your website to attract more eye balls to your website, and we think this tutorial will be useful for any webmaster learning PHP and linux.

Create php file called crypt.php or index.php, any name you like and edit it with your favorite editor.

First we will create simple HTML form to collect the input, you can use zend framework forms module, we will talk about it later.

<form action=”" method=”post”>

<input type=”text”  name=”username”>

<input type=”password” name=”encrypted_password”>

<input type=”submit” name=”submit” value=”generate”>

<input type=”hidden” name=”action” value=”go”>

</form>

<?php
// if form submitted
if($_POST['action']) {

$clearTextPassword = $_POST['encrypted_password'];
$username = $_POST['username'];

$password = crypt($clearTextPassword, base64_encode($clearTextPassword));
echo "Encryption successfully, copy and paste output below into your .htpasswd file <br>";

echo $username.":".$password;

}

?>

That’s it, we have nice proper working .htpasswd generator, contunue coding it to make dynamically protected pages and folders, if you have any questions I’m here to answer any time.

Good luck.

Add Twitter response to this post...
Twitter username :   
Twitter password :

Post Metadata

Date
July 8th, 2009

Author
glurt

Category


Leave a Reply