繁体   English   中英

如何编写代码以允许用户更改密码或更改用户名?

[英]How do I write my code to allow a user to change password or change username?

我正在制作的网站的一部分为用户提供了更改密码的选项。 我已经配置了此代码,并且可以正常工作。

我现在想在同一页面上添加一个表单,该表单使用户可以选择更改其USERNAME。 (因此,它们分别更改了用户名,但不更改密码)。

我是否需要重复完全相同的以下代码并将其粘贴在代码下面,显然是为了使用户名而不是密码适应特定的单词,或者是否有办法我可以添加到现有代码中,从而使其成为一个处理代码? 我该怎么做呢? 因此,例如第一行:

如果($ submit =='更改密码'){

我想用英文说, 如果 “更改密码” “更改用户名”按钮被提交的代码...执行以下代码,等等。

//处理代码

<?php
require_once('checklog.php');
require_once('functions.php');
// Grab the form data
$submit         = trim($_POST['submit']);
$password       = trim($_POST['password']);
$newpassword    = trim($_POST['newpassword']);
$repeatpassword = trim($_POST['repeatpassword']);
// Create some variables to hold output data
$message        = '';
// Start to use PHP session
session_start();
if ($submit == 'Change Password') {
    include_once('connect.php');
    mysqli_select_db($db_server, $db_database) or die("Couldn't find db");
    //clean the input now that we have a db connection                      
    $password       = clean_string($db_server, $password);
    $newpassword    = clean_string($db_server, $newpassword);
    $username       = $_SESSION['username'];
    $repeatpassword = clean_string($db_server, $repeatpassword);
    if ($password && $newpassword) {
        if ($repeatpassword == $newpassword) {
            if (strlen($newpassword) > 25 || strlen($newpassword) < 6) {
                $message = "Password must be 6-25 characters long";
            } else {
                // check whether username exists
                $password = salt($password);
                $query    = "SELECT username FROM users WHERE username='$username' and password='$password'";
                $result   = mysqli_query($db_server, $query);
                //if theres a result change password to new password
                if ($row = mysqli_fetch_array($result)) {
                    $newpassword    = salt($newpassword);
                    $repeatpassword = salt($repeatpassword);
                    //update password
                    $query          = "UPDATE users SET password='$newpassword' WHERE username='$username'";
                    mysqli_query($db_server, $query) or die("Update failed. " . mysqli_error($db_server));
                    $message = "<strong>Password change successful!</strong>";
                } else {
                    $message = "User account not found.";
                }
                mysqli_free_result($result);
            }
        } else {
            $message = "Password must match";
        }
    } else {
        $message = "Please enter all fields";
    }
    include_once('db_close.php');
}


include_once('header_logged.php');

?>

//更改密码或用户名的表格

!--change password form --> 
To change your Password:

<form method="post" action="changepassword.php">
Current password: <input type='password' name='password'>
<br>
<br>
New Password: <input type='password' name='newpassword'>
<br>
<br>
Repeat New Password: <input type='password' name='repeatpassword'>

<input type='submit' name='submit' value='Change Password'>
<input type='reset' name='reset' value='Reset'>
</form>
<p><strong><?php
echo $message;
?></strong></p>


<!--change username form --> 
To change your Username:
<form method="post" action="changepassword.php">
Current Username: <input type='username' name='username'>
<br>
<br>
New Username: <input type='username' name='newusername'>
<br>
<br>
Repeat New Username: <input type='username' name='repeatusername'>

<input type='submit' name='change' value='Change Username'>
<input type='reset' name='reset' value='Reset'>
</form> 

感谢您的帮助,非常感谢。

用这个:

if(isset($_POST['submit']) || isset($_POST['change'])){
//write your code here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM