我正在尝试创建一个激活页面,因此当用户单击其激活链接时,它会将用户导航到 activate.php 页面并显示一条消息,说明该帐户是否已激活。
我遇到的问题是它一直显示消息“代码和用户名不匹配!帐户未激活。” 即使点击了激活链接,它也根本不会显示成功消息“帐户已激活”。
此外,如果识别出正确的用户名和代码,数据库中的“活动”列应更改为“1”,但仍显示“0”,表示帐户仍处于非活动状态。
我的问题是为什么它不能识别用户名和激活码是正确的?
下面是 registration.php 脚本,它插入用户的注册详细信息并通过电子邮件发送激活链接:
$teacherpassword = md5(md5("j3j".$teacherpassword."D2n"));
$code = md5(rand(1,9));
$insertsql = "
INSERT INTO Teacher
(TeacherForename, TeacherSurname, TeacherEmail, TeacherAlias, TeacherUsername, TeacherPassword, Code)
VALUES
(?, ?, ?, ?, ?, ?, ?)
";
if (!$insert = $mysqli->prepare($insertsql)) {
// Handle errors with prepare operation here
}
$insert->bind_param("sssssss", $getfirstname, $getsurname,
$getemail, $getid, $getuser,
$teacherpassword, $code);
$insert->execute();
if ($insert->errno) {
// Handle query error here
}
$insert->close();
$getuser = $_POST['user'];
$code = md5(rand(1,9));
$site = "http://helios.hud.ac.uk/......../Mobile_app";
$webmaster = "Mayur Patel <.......@hud.ac.uk>";
$headers = "From: $webmaster";
$subject = "Activate Your Account";
$message = "Thanks for Registering. Click the link below to Acivate your Account. \n";
$message .= "$site/activate.php?user=$getuser&code=$code\n";
$message .= "You must Activate your Account to Login";
if(mail($getemail, $subject, $message, $headers)){
$errormsg = "You have been Registered. You must Activate your Account from the Activation Link sent to <b>$getemail</b>";
$getfirstname = "";
$getsurname = "";
$getid = "";
$getuser = "";
$getemail = "";
}
下面是 activate.php 页面,它检查激活链接并执行 mysqli 操作并显示消息:
<?php
$user_to_be_activated = $_GET['user'];
$code_to_be_matched = $_GET['code'];
// don't use $mysqli->prepare here
$query = "SELECT TeacherUsername, Active, Code FROM Teacher WHERE TeacherUsername = ? AND Code = ? ";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("ss",$user_to_be_activated, $code_to_be_matched);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbTeacherUsername, $dbActive, $dbCode);
//get number of rows
$stmt->store_result();
$counting = $stmt->num_rows();
if($counting == '1')
{
$updatesql = "UPDATE Teacher SET Active = ? WHERE TeacherUsername = ? AND Code = ?";
$update = $mysqli->prepare($updatesql);
$update->bind_param("iss", 1, $user_to_be_activated, $code_to_be_matched);
$update->execute();
$update->close();
echo "Account is Activated";
}
else
{
echo "The Code and Username doesn't match! Account is not Activated.";
}
?>
我只是希望它能够在第一次单击链接时激活帐户。