所以我正在处理一个注册表单,我有四个字段:名称,用户名,电子邮件和密码。我在jQuery中获取这些字段的值,并且根据是否填充了所有字段,我通过ajax将它们传递到PHP脚本。表格验证是否安全?我担心用户会操纵数据。
接下来在php脚本中,我检查所有发布的值是否都包含数据,然后才会进行一些验证...验证是我担心它不是最好的部分。它有很多缺陷。
$name = $_POST['name'];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
if (!preg_match("/^[A-Za-z '-]+$/i", $name)) {
$errors = "Please enter a valid name.";
} else if (!preg_match("/^[A-Za-z]+\s[A-Za-z]+$/", $name)) {
$errors = "Please enter your first and last name.";
} else if (strlen($username) <= 3 || strlen($username) > 15) {
$errors = "Please pick a username between 4 - 15 characters. Be creative.";
} else if (!preg_match("/^[A-Za-z][A-Za-z0-9_.-]{4,15}$/", $username)) {
$errors = "Please pick an alphanumeric username between 4 - 15 characters.";
} else if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$errors = "Please enter a valid email.";
} else if (strlen($username )< 6 || strlen($username) > 32) {
$errors = "Your password must atleast be 6 characters.";
} else {
echo "valid";
}
这些验证步骤是否安全?是否有用户可以操纵数据的循环孔?此外,我希望输入全名,用户将输入第一个和最后一个名字。这些足够好吗?
谢谢。
答案 0 :(得分:3)
当你要求复习时,请阅读你在这里写的内容......
} else if (strlen($username )< 6 || strlen($username) > 32) {
$errors = "Your password must atleast be 6 characters."
答案 1 :(得分:0)
有些时候,java脚本可能不是最好的解决方案,因为有些用户会禁用它。所以也要考虑一下。
在向数据库插入数据时,我们应该考虑'sql injection','加密方法'等。
我认为你应该在互联网上阅读和研究更多文章。试试这些链接。
http://www.sitepoint.com/form-validation-with-php/
http://www.w3resource.com/javascript/form/javascript-sample-registration-form-validation.php
http://www.hscripts.com/scripts/JavaScript/form-validation.php
答案 2 :(得分:0)
我用这个
$var = filter_var($_POST['xxx'], FILTER_SANITIZE_STRING);
If (preg_match('/^([a-zA-Z]){4,15}$/', $var)){ // must character lower or upper, minimal 4 and max 15 characters
$con1 = true;
} else{
$con2 = false;
}
// $cond2
// $cond3
// $cond4
// CHECK ALL CONDITION BEFORE INSERT
If (($cond1 && $cond2 && $cond3 && $cond4) == true){
(with prepared statement)
$sql = Insert into;
} else{
Header("Location: "); // if you redirect user to another page
And echo the errors with ajax
}
希望您可以考虑这个