Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 更改cpanel上的电子邮件密码_Php_Email_Passwords_Cpanel_Bluehost - Fatal编程技术网

Php 更改cpanel上的电子邮件密码

Php 更改cpanel上的电子邮件密码,php,email,passwords,cpanel,bluehost,Php,Email,Passwords,Cpanel,Bluehost,我正在尝试使用此代码更改电子邮件密码 <?php $message = ""; $found = $valid = false; if ($_POST['username'] != "") { $domain_pos = strpos($_POST['username'], "@"); if ($domain_pos === false) { $username = $_POST['username'];

我正在尝试使用此代码更改电子邮件密码

<?php

    $message = "";
    $found = $valid = false;

    if ($_POST['username'] != "") {
        $domain_pos = strpos($_POST['username'], "@");
        if ($domain_pos === false) {
            $username = $_POST['username'];
            $domain = $_POST['domain'];
        } else {
            $username = substr($_POST['username'], 0, $domain_pos);
            $domain = substr($_POST['username'], $domain_pos + 1);
        }

        $current_password = $_POST['current_password'];
        $new_password1 = $_POST['new_password1'];
        $new_password2 = $_POST['new_password2'];

        $root = $_SERVER['DOCUMENT_ROOT'];
        $path_elements = explode('/', $root);
        $root = "/{$path_elements[1]}/{$path_elements[2]}"; // for bluehost, extracts homedir ex: /homeN/blueuser may work with other hosts?
        $shadow_file = "$root/etc/$domain/shadow";

        // check if the shadow file exists. if not, either an invalid
        // domain was entered or this may not be a bluehost account...?
        if (file_exists($shadow_file)) {
            // compare the new passwords entered to ensure they match.    
            if ($new_password1 == $new_password2) {
                if (trim($new_password1) != "") {
                    // get the contents of the shadow file.
                    $shadow = file_get_contents($shadow_file);
                    $lines = explode("\n", $shadow);

                    // go through each line of shadow file, looking for username entered.
                    for ($i = 0; $i < count($lines); $i++) {
                        $elements = explode(":", $lines[$i]);
                        // found the user...
                        if ($elements[0] == $username) {
                            $found = true;
                            $passwd = explode("$", $elements[1]);
                            $salt = $passwd[2]; // get the salt currently used 

                            // crypt the "Current Password" entered by user. Can use either builtin 
                            // php crypt function or command line openssl command.
                            if (CRYPT_MD5 == 1) { // indicates whether or not the crypt command supports MD5.
                                $current = crypt($current_password, '$1$'.$salt.'$');
                            } else {
                                $current = trim(`openssl passwd -1 -salt "$salt" "$current_password"`);
                            }
                            // check if the current password entered by the user
                            // matches the password in the shadow file.
                            $valid = ($current == $elements[1]);

                            if ($valid) {
                                // if they match then crypt the new password using the same salt
                                // and modify the line in the shadow file with the new hashed password
                                if (CRYPT_MD5 == 1) {
                                    $new = crypt($new_password1, '$1$'.$salt.'$');
                                } else {
                                    $new = trim(`openssl passwd -1 -salt "$salt" "$new_password1"`);
                                }
                                $elements[1] = $new;
                                $lines[$i] = implode(":", $elements);
                            }

                            break;
                        }
                    }

                    if (!$found) {
                        $message = "The username you entered is not valid.";
                    } else if (!$valid) {
                        $message = "The password you entered is not valid.";
                    } else {
                        // write the new contents of the shadow back to the shadow file.
                        $shadow = implode("\n", $lines);
                        file_put_contents($shadow_file, $shadow);
                        $message = 'Your password has been updated.';
                    }
                } else {
                    $message = "Your password cannot be blank.";
                }
            } else {
                $message = "Both new passwords must match.";
            }
        } else {
            $message = "The domain you entered is not valid.";
        }
    }

    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
        <head>
            <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
            <title>Change Password</title>
        </head>

        <body>            
            <?php
                if ($message != "") {
                    $color = $found && $valid ? "green" : "red";
                    echo "<span style=\"color:$color;\">$message</span>";
                }
            ?>

            <form action="" method="post">
                <input type="hidden" name="domain" value="somebluehostdomain.com" />
                <table>
                    <tbody>
                        <tr>
                            <td><label for="username">Username</label></td> 
                            <td><input name="username" id="username" type="text" /></td> 
                        </tr>
                        <tr>
                            <td><label for="current_password">Current Password</label></td> 
                            <td><input name="current_password" id="current_password" type="password" /></td> 
                        </tr>
                        <tr>
                            <td><label for="new_password1">New Password</label></td> 
                            <td><input name="new_password1" id="new_password1" type="password" /></td> 
                        </tr>
                        <tr>
                            <td><label for="new_password2">New Password</label></td> 
                            <td><input name="new_password2" id="new_password2" type="password" /></td> 
                        </tr>
                        <tr>
                            <td colspan="2" style="text-align:center;">
                                <input type="submit" value="Change Password" />
                            </td>
                        </tr>
                    </tbody> 
                </table> 
            </form>
        </body>
    </html>

我正在bluehost服务器上使用PHP5.4。这可能是不相容的吗?还有其他方法允许用户从PHP更改电子邮件密码(无需管理员登录到cPanel)?

什么是“不工作”?有错误消息吗?嗯,它不会更改密码。这是错误日志:PHP严格标准:非静态方法PEAR::setErrorHandling()不应在/home1/account/public_html/site/mail/program/include/iniset.PHP第132行静态调用[02-Apr-2014 15:16:49 America/Denver]PHP注意:未定义的索引:第6行/home1/account/public\u html/site/mail/changepassword.PHP中的用户名,所以没有错误,只是没有更改密码?对不起,错误日志添加到我之前的评论和问题中。好的,在第6行你可以把
if(isset($\u POST['password'])&$\u POST['password'!=''放在
中,这样错误就会被清除。
PHP Strict Standards: Non-static method PEAR::setErrorHandling() should not be called statically in /home1/account/public_html/site/mail/program/include/iniset.php on line 132 

PHP Notice: Undefined index: username in /home1/account/public_html/site/mail/changepassword.php on line 6