gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:27:10 31 4
gpt4 key购买 nike

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

我现在想在同一页面上添加一个表单,让用户可以选择更改他们的用户名。 (个人,所以他们更改了用户名但不更改密码)。

我是否需要重复完全相同的以下代码并将其粘贴到它下面,显然需要调整特定单词以用于用户名而不是密码,或者有什么方法可以添加到现有代码所以它是一个单一的处理代码?我该怎么做呢?例如第一行:

if ($submit == '更改密码') {,

在我的英语头脑中,我希望代码说IF“更改密码”OR“更改用户名”按钮已提交...做以下代码等

//处理代码

<?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
}

关于php - 如何编写代码以允许用户更改密码或更改用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34581089/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com