这篇文章主要为大家详细介绍了php 处理.htpasswd文件函数示例,具有一定的参考价值,可以用来参考一下。
对一个用于处理.htpasswd文件的php函数感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
/**
* 一个用于处理.htpasswd文件的php函数
*
* @param
* @arrange 五一二笔记网: www.q1010.com
**/
// function to register user
function regUser() {
$filename = 'members/password/.htpasswd';
$data = $_POST['username'].":".htpasswd($_POST['password'])."\n";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $data) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
// echo "Success, wrote ($data) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
}
// function to show all users and passwords ( encrypted )
function showUser()
{
$file = file('members/password/.htpasswd');
$array = array();
$count = count($file);
for ($i = 0; $i < $count; $i++)
{
list($username, $password) = explode(':', $file[$i]);
$array[] = array("username" => $username, "password" => $password);
}
return $array;
}
function delUser($username) {
$fileName = file('members/password/.htpasswd');
$pattern = "/". $username."/";
foreach ($fileName as $key => $value) {
if(preg_match($pattern, $value)) { $line = $key; }
}
unset($fileName[$line]);
if (!$fp = fopen('members/password/.htpasswd', 'w+'))
{
print "Cannot open file ($fileName)";
exit;
}
if($fp)
{
foreach($fileName as $line) { fwrite($fp,$line); }
fclose($fp);
}
}
// function for encrypting password
function htpasswd($pass)
{
$pass = crypt(trim($pass),base64_encode(CRYPT_STD_DES));
return $pass;
}
/*** 来自四海网(www.q1010.com) ***/
本文来自:http://www.q1010.com/173/775-0.html
注:关于php 处理.htpasswd文件函数示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:.htpasswd
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。