我已经在那里上htpasswd的唯一用户能够访问受保护的目录,但有时它要求用户更改密码或用户名,编辑特定的用户名密码,以自己的用户名他的自我
i have a protected directory where only user on .htpasswd can access, but sometimes it requires the user to change password or username, edit a specific username password to his username him self
sample users
kevien : kka
mike : mike
和让说,我想kevien更改为XYZ
And let say i want to change kevien to XYZ
和同样的事情会发生到密码
And same thing goes to password
OFC这只是一个样本,将读取当前的文件,找到指定的用户名和更改要么是用户名密码。
Ofc this is just a sample, that will read your current file, find the given username and change either it is password of username.
请记住,这code是不是安全的,您仍然需要解析的用户名和密码,所以它不会破坏你的文件。
$username = $_POST['user'];
$password = $_POST['pass'];
$new_username = $_POST['newuser'];
$new_password = $_POST['newpass'];
$action = $_POST['action'];
//read the file into an array
$lines = explode("n", file_get_contents('.htpasswd'));
//read the array and change the data if found
$new_file = "";
foreach($lines as $line)
{
$line = preg_replace('/s+/','',$line); // remove spaces
if ($line) {
list($user, $pass) = split(":", $line, 2);
if ($user == $username) {
if ($action == "password") {
$new_file .= $user.':'.$new_password."n";
} else {
$new_file .= $new_username.':'.$pass."n";
}
} else {
$new_file .= $user.':'.$pass."n";
}
}
}
//save the information
$f=fopen(".htpasswd","w") or die("couldn't open the file");
fwrite($f,$new_file);
fclose($f);