我在Php中的注册系统上有一个任务。忘记密码后,我必须向用户邮件发送验证链接,以便如果用户单击该验证链接,忘记密码表格将打开,我将无法理解我在哪里做错了,并且为什么我的代码无法正常工作,有人可以指出我要去哪里。在此先感谢
<?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);
}
}
}
?>
答案 0 :(得分:2)
我对您的代码进行了一些更改,请尝试一下。
<?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。 form变量将作为使用mail()
函数发送的电子邮件的标题传递。注意if()语句,如果成功,邮件函数将返回true。
答案 1 :(得分:1)
这里有很多要指出的地方,所有这些都可能导致问题。
首先,将多次检查$ result变量,而不会在两次复位之间进行重置。这意味着您可以从同一表单提交中获得“错误的确认代码”和“您的帐户已被激活”的输出(如果第一个查询成功,但是在数据库中找不到匹配的密码)。
此行看起来可能什么也不做:
SIGINT
可能应该是:
$passkey = isset($_POST['$passkey']) ? $_POST['$passkey'] : '';
这无关紧要,因为$ passkey在下一行代码中分配了一个新值。
最后,此插入使用变量$ name代替$ username:
$passkey = isset($_POST['passkey']) ? $_POST['passkey'] : '';
这可能是预期的:
$sql = "INSERT INTO $user ( name, email, password )VALUES( '$name', '$email', '$password' )";
答案 2 :(得分:1)
我认为问题在于$ _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);
}
}
}
?>
我希望这对您有用。我复制了您的代码并进行了一些更改。 :)
答案 3 :(得分:0)
<?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( '$username', '$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);
}
}
}
?>