1 回答

TA貢獻1824條經驗 獲得超5個贊
值得注意的一件事是您可以優化 AJAX 功能。絕對沒有理由發出那么多 AJAX 請求。您可以在一個 AJAX 請求中發送所有數據并完成所有成功功能。
另一件需要注意的事情是,如果 post 變量存在,您的 PHP 代碼將執行數據庫邏輯submit?,F在您根本不通過 AJAX 函數解析它。您沒有使用帶有提交的序列化方法,而是解析非常具體的數據,通過指定每個元素值手動獲取。
您可以做的就是解析submit為另一個數據變量。我冒昧地根據這個想法優化了您的 AJAX 代碼。
jQuery AJAX 示例:
$(document).ready(function() {
$('#submit').click(function(event) {
event.preventDefault();
var fullname = $("#fullname").val();
var username = $("#username").val();
var email = $("#email").val();
var password = $("#password").val();
var submit = "1";
$.ajax({
url: 'registercontrol.php',
method: 'POST',
data: {
fullname : fullname,
username : username,
email : email,
password : password,
submit : submit
},
success:function(response){
$("#vfullname").html(response);
$("#vusername").html(response);
$("#vemail").html(response);
$("#vpassword").html(response);
}
});
});
});
現在您肯定會有一個提交POST變量,它將輸入if()數據庫插入的語句。
您可以做的另一件事是更具體地檢查是否應該輸入允許數據庫插入的語句?,F在它只圍繞POST變量submit。沒有其他邏輯。你可能想重新考慮一下。創建變量,FALSE當驗證檢查一切正常時,將它們設置為 true。相反,圍繞它構建數據庫插入if()語句,因為這比提交變量是否存在更相關。
另一件事是您md5()對密碼使用哈希函數。這是非常不安全的。參考這篇文章。
您也沒有在告訴用戶單擊激活鏈接的行上正確連接 PHP 變量。你連接super globals得很好,但沒有連接 PHP 變量。
話雖如此,除了我指出的之外,沒有什么本質上的錯誤。
這是你的 PHP 代碼:
<?php
if( isset( $_POST['fullname'] ) ) {
//fullname validation
$fullname = $_POST['fullname'];
if( empty( $_POST['fullname'] ) ) {
$warningfn = "Please fill this field";
echo '<style type="text/css"> #fullname {border-color: #f6c23e !important;} </style>';
echo '<p class="p-3 text-warning">'.$warningfn.'</p>';
} else if( !$user->isValidFullname($fullname) ) {
$infofn = 'Your name must be alphabetical characters';
echo '<style type="text/css"> #fullname {border-color: #36b9cc !important;} </style>';
echo '<p class="p-3 text-info">'.$infofn.'</p>';
} else {
echo '<style type="text/css"> #fullname {border-color: #1cc88a !important;} </style>';
}
}
if( isset( $_POST['username'] ) ) {
//username validation
$username = $_POST['username'];
if( empty( $_POST['username'] ) ) {
$warningun = "Please fill this field";
echo '<style type="text/css"> #username {border-color: #f6c23e !important;} </style>';
echo '<p class="p-3 text-warning">'.$warningun.'</p>';
} else if( !$user->isValidUsername($username) ) {
$infoun = 'Your username must be at least 3 alphanumeric characters';
echo '<style type="text/css"> #username {border-color: #36b9cc !important;} </style>';
echo '<p class="p-3 text-info">'.$infoun.'</p>';
} else if ( !$user->isUsernameAlreadyinUse($username) ) {
$errorun = 'This username already in use';
echo '<style type="text/css"> #username {border-color: #e74a3b !important;} </style>';
echo '<p class="p-3 text-danger">'.$errorun.'</p>';
} else {
echo '<style type="text/css"> #username {border-color: #1cc88a !important;} </style>';
}
}
if( isset( $_POST['email'] ) ) {
//email validation
$email = htmlspecialchars_decode( $_POST['email'], ENT_QUOTES );
if( empty( $_POST['email'] ) ) {
$warningm = "Please fill this field";
echo '<style type="text/css"> #email {border-color: #f6c23e !important;} </style>';
echo '<p class="p-3 text-warning">'.$warningm.'</p>';
} else if( !$user->isValidEmail($email) ) {
$warningm = 'Please enter a valid email address';
echo '<style type="text/css"> #email {border-color: #f6c23e !important;} </style>';
echo '<p class="p-3 text-warning">'.$warningm.'</p>';
} else if( !$user->isEmailAlreadyinUse($email) ) {
$errorm = 'This email already in use';
echo '<style type="text/css"> #email {border-color: #e74a3b !important;} </style>';
echo '<p class="p-3 text-danger">'.$errorm.'</p>';
} else {
echo '<style type="text/css"> #email {border-color: #1cc88a !important;} </style>';
}
}
if( isset( $_POST['password'] ) ) {
$password= $_POST['password'];
if( empty( $_POST['password'] ) ) {
$warningpw = "Please fill this field";
echo '<style type="text/css"> #password {border-color: #f6c23e !important;} </style>';
echo '<p class="p-3 text-warning">'.$warningpw.'</p>';
} else if ( !$user->isValidPassword($password) ) {
$warningpw = 'Your password must be at least 6 characters long';
echo '<style type="text/css"> #password {border-color: #f6c23e !important;} </style>';
echo '<p class="p-3 text-warning">'.$warningpw.'</p>';
} else {
echo '<style type="text/css"> #password {border-color: #1cc88a !important;} </style>';
}
}
if( isset( $_POST['gender'] ) ) {
$gender = $_POST['gender'];
if( !in_array($gender, ['Male','Female','Other']) ) {
$gender = 'Other';
}
} else {
$gender = 'Other';
}
if( isset( $_POST['submit'] ) ) {
//hash the password
$hashedpassword = password_hash( $password, PASSWORD_BCRYPT );
//create the activasion code
// this is highly insecure, see: https://www.php.net/manual/en/function.md5.php
$activasion = md5( uniqid( rand(),true ) );
try {
//insert into database with a prepared statement
$stmt = $db->prepare('INSERT INTO members (fullname,username,password,email,gender,active) VALUES (:fullname, :username, :password, :email, :gender, :active)');
$stmt->execute(array(
':fullname' => $fullname,
':username' => $username,
':password' => $hashedpassword,
':email' => $email,
':gender' => $gender,
':active' => $activasion
));
$id = $db->lastInsertId('memberID');
//send email
$to = $_POST['email'];
$subject = "Confirm Your Account";
$body = "<p>Thank you for registering on the demo site.</p>
<p>Hello ".$fullname.", please click this link to activate your account: <a href='".DIR."activate.php?x=".$id."&y=".$activasion."'>".DIR."activate.php?x=".$id."&y=".$activasion."</a></p>";
$mail = new Mail();
$mail->setFrom(SITEEMAIL);
$mail->addAddress($to);
$mail->subject($subject);
$mail->body($body);
$mail->send();
//redirect to index page
header('Location: register.php?action=joined');
exit;
//else catch the exception and show the error.
} catch(PDOException $e) {
$error[] = $e->getMessage();
}
}
?>
我相信這$someVar->isValid()指的是有效的東西,因為我對此沒有其他見解。
如果您現在在數據庫插入之外遇到更多錯誤,則問題出在其他地方。要么你沒有遵循你的表結構邏輯(拼寫錯誤、無效的數據格式等)
- 1 回答
- 0 關注
- 172 瀏覽
添加回答
舉報