我有一个任务注册系统在Php.after忘记密码,我必须发送一个验证链接到用户邮件,以便如果用户点击该验证链接忘记密码表格将打开,我不知道我在哪里做错了,为什么我的代码不起作用,任何人都可以指出我哪里出错。提前谢谢
<?php
require_once ( "./connect.php" );
if ( !empty( $_POST['submit'] ) ) {
$passkey = isset($_POST['$passkey']) ? $_POST['$passkey'] : '';
// Passkey that got from link
$passkey = $_POST['passkey'];
$user = "registration";
// Retrieve data from table where row that match this passkey
$sql ="SELECT * FROM `user` WHERE confirm_code ='$passkey'";
$result = $db->query($sql);
// If successfully queried
if( $result ) {
// Count how many row has this passkey
$count = mysql_num_rows( $result );
// if found this passkey in our database, retrieve data from table "temp_members_db"
if ( $count == 1 ) {
$rows = mysql_fetch_array( $result );
$username = $rows['username'];
$email = $rows['email'];
$password = $rows['password'];
$user = "registration";
// Insert data that retrieves from "temp_members_db" into table "registered_members"
$sql = "INSERT INTO $user ( name, email, password )VALUES( '$name', '$email', '$password' )";
$result = $db->query($sql);
}
// if not found passkey, display message "Wrong Confirmation code"
else {
echo "Wrong Confirmation code";
}
// if successfully moved data from table"temp_members_db" to table "registered_members" displays message "Your account has been activated" and don't forget to delete confirmation code from table "temp_members_db"
if ( $result ){
echo "Your account has been activated";
// Delete information of this user from table "temp_members_db" that has this passkey
$sql="DELETE FROM `user` WHERE confirm_code = '$passkey'";
$result = $db->query($sql);
}
}
}
?>发布于 2018-10-26 10:08:37
我已经对您的代码做了一些修改,尝试一下。
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['passkey'])){
//Get the Passkey that got from link
$passkey = $_POST['passkey'];
$user = "registration";
// Retrieve data from table where row that match this passkey
$sql ="SELECT * FROM `user` WHERE confirm_code = $passkey ";
$result = $db->query($sql);
// If successfully queried
if( $result ) {
// Count how many row has this passkey
$count = mysql_num_rows( $result );
// if found this passkey in our database, retrieve data from table "temp_members_db"
if ( $count == 1 ) {
$rows = mysql_fetch_array( $result );
$username = $rows['username'];
$email = $rows['email'];
$password = $rows['password'];
$user = "registration";
// Insert data that retrieves from "temp_members_db" into table "registered_members"
$sql = "INSERT INTO $user ( name, email, password )VALUES( $name, $email, $password )";
$result = $db->query($sql);
}
// if not found passkey, display message "Wrong Confirmation code"
else {
echo "Wrong Confirmation code";
}
// if successfully moved data from table"temp_members_db" to table "registered_members" displays message "Your account has been activated" and don't forget to delete confirmation code from table "temp_members_db"
if($result){
// add inside the massage variable the confirmation code $passkey and your customized message
// change the $from variable with the email address you want use to send the verification mail. This is the address who the user will see
$message = "To activate your account please click on the following link https://yoursite.com/?code=$passkey";
$from = "";
if(mail($email, $message, $from)){
echo "Your account has been activated";
// Delete information of this user from table "temp_members_db" that has this passkey
$sql="DELETE FROM `user` WHERE confirm_code = $passkey";
$result = $db->query($sql);
}
else{
// error
}
}
}
}
}
?>我添加了内置在函数邮件中的PHP。有关如何使用它的更多信息,请阅读文档。您可以看到包含邮件地址的var $from和包含要发送的链接的var $message。表单变量将作为使用mail()函数发送的电子邮件的标题传递。注意if()语句,如果成功,则mail函数将返回true。
发布于 2018-10-26 09:08:36
这里有很多要指出的东西,所有这些都可能是问题的原因。
首先,对$result变量进行多次检查,而不会在中间重置。这意味着您可能从相同的表单提交中获得输出“错误的确认代码”和“您的帐户已被激活”(如果第一个查询成功,但在db中找不到匹配的密码)。
这句话看上去很可能什么也做不了:
$passkey = isset($_POST['$passkey']) ? $_POST['$passkey'] : '';可能应该是:
$passkey = isset($_POST['passkey']) ? $_POST['passkey'] : '';不过,这并不重要,因为在下一行代码中为$passkey分配了一个新值。
最后,此插入使用变量$name而不是$username:
$sql = "INSERT INTO $user ( name, email, password )VALUES( '$name', '$email', '$password' )";这可能就是我们的目的:
$sql = "INSERT INTO $user ( name, email, password )VALUES( '$username', '$email', '$password' )";发布于 2018-10-26 09:50:21
我认为问题在于$_POST数组。通常我们在电子邮件中发送确认链接,确认链接包含确认代码作为查询字符串。当我们单击链接时,重定向页面获取确认代码并继续进行。
<?php
require_once ( "./connect.php" );
if ( isset($_GET['passkey']) && !empty( $_GET['passkey'] ) ) {
$passkey = $_GET['passkey'];
// Passkey that got from link
$passkey = $_GET['passkey'];
$user = "registration";
// Retrieve data from table where row that match this passkey
$sql ="SELECT * FROM `user` WHERE confirm_code ='$passkey'";
$result = $db->query($sql);
// If successfully queried
if( $result ) {
// Count how many row has this passkey
$count = mysql_num_rows( $result );
// if found this passkey in our database, retrieve data from table "temp_members_db"
if ( $count == 1 ) {
$rows = mysql_fetch_array( $result );
$username = $rows['username'];
$email = $rows['email'];
$password = $rows['password'];
$user = "registration";
// Insert data that retrieves from "temp_members_db" into table "registered_members"
$sql = "INSERT INTO $user ( name, email, password )VALUES( '$name', '$email', '$password' )";
$result = $db->query($sql);
}
// if not found passkey, display message "Wrong Confirmation code"
else {
echo "Wrong Confirmation code";
}
// if successfully moved data from table"temp_members_db" to table "registered_members" displays message "Your account has been activated" and don't forget to delete confirmation code from table "temp_members_db"
if ( $result ){
echo "Your account has been activated";
// Delete information of this user from table "temp_members_db" that has this passkey
$sql="DELETE FROM `user` WHERE confirm_code = '$passkey'";
$result = $db->query($sql);
}
}
}
?> 我希望这对你有用。我复制您的代码并做了一些更改。:)
https://stackoverflow.com/questions/53004327
复制相似问题