This script will change your .htpasswd password to a new random password (with encryption) and send an email to notify your users of the change. You can run this script automatically using cron at what ever interval you like, or run it manually.
- - Start Script Here - -
- - End Script Here - -
- - Start Script Here - -
- - End Script Here - -
- - Start Script Here - -
- - End Script Here - -
- - Start Script Here - -
<?php
$username = "XXXXXXX"; // the username specified in the .htaccess file$length = "10"; // length of the password$emailaddress = "mailinglist@yourdomain.com"; // email address that forwards to your mailing list
// generate password$spec_charset = array("!","@","#","$","%","^","&","*","_","+"."?","=");$chars = array();$pass = "";
for ($i = 1; $i <= $length; $i++) {
for ($i = 48; $i <= 57; $i++) { $chars[] = chr($i); } // numbers
for ($i = 65; $i <= 90; $i++) { $chars[] = chr($i); } // upper
for ($i = 97; $i <= 122; $i++) { $chars[] = chr($i); } // lower
foreach ($spec_charset as $i) { $chars[] = $i; } // special
for ($i = 1; $i <= $length; $i++) { $pass .= $chars[rand(0, count($chars)-1)]; }
}$encrypted = crypt($pass);// build & write .htpasswd file$ht_pass = "$username:$encrypted\n";$filename = "/www/X/XXXXXX/.htpasswd";$handle = fopen($filename,"w");fwrite($handle,"$ht_pass");fclose($handle);// notify$message = "The password has been changed to $pass\n";mail($emailaddress, "Password Notification" , $message, "From: Website <>");?> - - End Script Here - -
Auto generate .htpasswd file
This php script reads a MySQL database, encrypts the passwords and writes an htpasswd file. Using this script you can maintain a database of users and generate an .htpasswd file from the database.- - Start Script Here - -
<?php
$host="host"; // database host address$dbuser="user"; // database user name$dbpswd="password"; // database password$mysqldb="db_name"; // name of database$table="passwd_table"; // name of tablemysql_connect("$host", "$dbuser", "$dbpswd");mysql_select_db ("$mysqldb");$query = mysql_query("SELECT * FROM $table");
while ($row = mysql_fetch_array($query)) {
$user = $row['user'];
$pass = $row['password'];
$encrypted = crypt($pass);
$record .= "$user:$encrypted\r\n";
}$filename = "your htpasswd file name - complete unix path - or relative to this script";$handle = fopen($filename,"w");fwrite($handle,"$record");fclose($handle);?>- - End Script Here - -
Passing user authentication information to PHP
The best way to protect your directory is with htaccess/htpasswd protection. But you may also want to pass the user information to your php scripts so you can do custom processing based on who logged in. You can retrieve the current authenticated user with one php line. Put this in the target script in the protected directory (used in the script after authentication).- - Start Script Here - -
<?php
$user = $_SERVER['REMOTE_USER'];?>- - End Script Here - -
Comments
Post a Comment