如果您只想更改自己的密码或其他用户密码,请使用 passwd 命令。
我被要求设置一个基于 PHP 的界面来更改密码。由于我对php的了解有限。这是我所做的:
警告:这是过时且不安全的信息。请参阅 php.net 官方文档以获取更多信息。已警告您忽略此帖子。
所需工具/设置:
您必须安装以下工具/软件
=> 更改密码的 Shell 脚本
=> 须藤访问
=> Apache 或 Lighttpd Web 服务器
=> PHP服务器端
步骤#1:设置 shell 脚本来更改密码
该 shell 脚本使用expect 工具来更改密码(请参阅此处有关expect工具的更多信息)。
示例 shell 脚本代码
#!/bin/sh # exec Expect -f "$0" ${1+"$@"} 设置密码[ lindex $argv 1 ] spawn passwd [ lindex $argv 0 ] sleep 1 Expect "assword:" send " $密码r " 期望"assword:" 发送" $password r " 期望 eof
您可以按如下方式执行此脚本(下载链接):
$ chpasswd username password
下载脚本并复制到您的网站根目录或网络服务器可以读取此脚本文件的位置:
$ cp chpasswd /var/www/
或者,如果您使用 Ligtttpd Web 服务器:
$ cp chpasswd /home/lighttpd
步骤#2:设置 sudo 以 root 用户身份执行命令
Apache 或 Lighttpd Web 服务器一旦进入后台就会放弃 root 权限。这使得更改密码场景变得困难,因为 passwd 命令需要 root 权限才能更改其他用户帐户密码。
通常,Apache 2 使用 www-data 用户,Lighttpd 使用 lighttppd 用户名来放弃权限。以 root 用户身份登录并键入以下命令:
# visudo
现在允许您的 Web 服务器执行无需密码的脚本 (chpasswd)。如果您使用 Apache Web 服务器,请键入以下命令:
www-data ALL=NOPASSWD: /var/www/chpasswd
或者,如果您使用 Ligtttpd Web 服务器,请键入以下命令:
lighttpd ALL=NOPASSWD: /home/lighttpd/chpasswd
保存并关闭文件。
步骤#3:创建基于 PHP 的界面
现在您需要编写一个 php 脚本。这是示例 php 脚本。这是一个演示脚本。您应该根据您的要求或设置对其进行修改。至少,您需要设置正确的 shell 脚本位置,以便它可以立即为您工作。打开 php 脚本并找到行:
$shellscript = "sudo /home/lighttpd/chpasswd";
将其更改为指向正确的位置。PHP 源代码:
<?php // change .. me! - shell script name $shellscript = "sudo /home/lighttpd/chpasswd"; // Make sure form is submitted by user if(!(isset($_POST['pwdchange']))) { // if not display them form writeHead("Change password"); writeForm(); writeFoot(); } else { // try to change the password $callshell=true; // get username and password $_POST['username'] = stripslashes(trim($_POST['username'])); $_POST['passwd'] = stripslashes(trim($_POST['passwd'])); // 如果用户跳过我们的 javascript ... // 确保只有在我们同时拥有用户名和密码的情况下才能更改密码 if (空( $_POST['username'])) { $callshell=false; } if(empty($_POST['passwd'])) { $callshell=false; } if ( $callshell == true ) { // command to change password $cmd="$shellscript " . $_POST['username'] . " " . $_POST['passwd']; // call command // $cmd - command, $output - output of $cmd, $status - useful to find if command failed or not exec($cmd,$output,$status); if ( $status == 0 ) { // Success - password changed writeHead("Password changed"); echo '<h3>Password changed</h3>Setup a new password'; writeFoot(); } else { // Password failed writeHead("Password change failed"); echo '<h3>Password change failed</h3>'; echo '<p>System returned following information:</p>'; print_r($output); echo '<p><em>Please contact tech-support for more info! Or try <a href='.$_SERVER['PHP_SELF'].'again</a></em></p>'; writeFoot(); } } else { writeHead("Something was wrong -- Please try again"); echo 'Error - Please enter username and password'; writeForm(); writeFoot(); } } // display html head function writeHead($title) { echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title> ' .$title. '</title> <style type="text/css" media="screen"> .passwdform { position: static; overflow: hidden; } .passwdleft { width: 25%; text-align: right; clear: both; float: left; display: inline; padding: 4px; margin: 5px 0; } .passwdright { width: 70%; text-align: left; float: right; display: inline; padding: 4px; margin: 5px 0; } .passwderror { border: 1px solid #ff0000; } .passwdsubmit { } </style> </head> <body>'; } // display html form function writeForm() { echo ' <h3>Use following form to change password:</h3> <script> function checkForm() { if (document.forms.changepassword.elements['username'].value.length == 0) { alert('Please enter a value for the "User name" field'); return false; } if (document.forms.changepassword.elements['passwd'].value.length == 0) { alert('Please enter a value for the "Password" field'); return false; } return true; } </script> <div class="contactform"> <form action="' . $_SERVER[PHP_SELF]. '" method="post" onSubmit="return checkForm()" name="changepassword"> <div class="passwdleft"><label for="lblusername">User Name: </label></div> <div class="passwdright"><input type="text" name="username" id="lblusername" size="30" maxlength="50" value="" /> (required)</div> <div class="passwdleft"><label for="lblpasswd">Password: </label></div> <div class="passwdright"><input type="password" name="passwd" id="lblpasswd" size="30" maxlength="50" value="" /> (required)</div> <div class="passwdright"><input type="submit" name="Submit" value="Change password" id="passwdsubmit" /> <input type="hidden" name="pwdchange" value="process" /></div> </form> </div> '; } // display footer function writeFoot(){ echo '</body> </html> '; } ?>
Step # 4: Run the script
将 Web 浏览器指向您的服务器 URL – https://mydomain.com/changepassword.php。您应该看到如下的用户名和密码表单:
如果密码更改成功,您应该得到如下确认:
由于某种原因,如果密码更改失败,您应该收到如下详细错误消息:
步骤#5:安全
请注意,这是一个示例,而不是最终解决方案,因为它有点不安全。
- 切勿通过 http 会话运行此脚本。始终通过 https 会话运行。
- 将脚本放入受密码保护的目录中(请参阅如何设置Apache或Lighttpd Web 服务器受密码保护的目录)。
- 永远不要相信用户输入。上面的 php 脚本只是一个示例,对于现实生活中的生产,您应该考虑更强大的用户输入验证。有关 PHP 编程安全性的讨论超出了本文的范围。您可以查阅一本好的 PHP 书籍或使用您喜欢的搜索引擎搜索网络