完全抛入深层次 - PHP的新手并进行简单的表单提交(创建帐户页面)以发送到mySQL数据库,因此对于问题的无聊而道歉。
我不确定在发送数据之前如何正确验证和清理数据。 但是我在插入数据库时使用PDO和占位符,所以我认为我已经覆盖了那一面。
<form action="createaccount.php" name="form" method="post">
<input type="text" name="createUserName" id="createUserName" placeholder="User Name"><br>
<input type="password" name="passWord" id="passWord" placeholder="Password (Min 6 Characters)"><br>
<input type="password" name="confirmPW" id="confirmPW" placeholder="Confirm Password"><br>
<input type="text" name="fName" id="fName" placeholder="First Name"><br>
<input type="text" name="sName" id="sName" placeholder="Surname"><br><br>
<input type="email" name="userEmail" id="userEmail" placeholder="Email Address"><br>
<input type="submit" value="Create Account">
</form>
这被发送到一个名为createaccount.php的独立php文件,它运行用户名检查,如果成功运行一个函数,将字段发送到我的dbHandler类中的insert函数。
<?php
session_start();
include('connect.php');
$username = $_POST['createUserName'];
$password = $_POST['passWord'];
$password_confirm = $_POST['confirmPW'];
$firstname = $_POST['fName'];
$surname = $_POST['sName'];
$email = $_POST['userEmail'];
if ($dbh->checkUserName($username)) {
$_SESSION['message'] = "Username is taken, please try again";
header('Location: createAccount.php');
exit();
} else {
$dbh->createAccount($username, $password, $password_confirm, $firstname, $surname, $email);
header('Location: login.php');
exit();
};
?>
所以我的问题是。我应该使用
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
在我的表格中?如果是这样。我需要在createaccount.php上运行这个函数,因为我无法将它发送到另一个php文件?
我应该使用filter_var吗?和/或我应该使用trim,stripslashes或我的变量中的任何东西我发送到表单?我该怎么做呢?我的理解是
$name = test_input($_POST['createUserName']);
$password = test_input($_POST['passWord']); .. etc
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
另外,这是我的插入功能。这是安全/正确的吗?
<?php
function createAccount($userName, $pw, $confirmPW, $fName, $sName, $email) {
$sql = "INSERT INTO `room_project`.`user` (`userName`, `pass`, `confirmPW`, `fName`, `sName`, `email`) VALUES (?, ?, ?, ?, ?, ?);";
$query = $this->connection->prepare($sql);
$query->execute(Array($userName, $pw, $confirmPW, $fName, $sName, $email));
}
?>
我非常感谢任何建议!再次请原谅这个初学者的性质:)
答案 0 :(得分:4)
您应该使用PHP regex严格验证输入数据。
假设您要验证以下输入字段
然后你会做这样的事情,
if(isset($_POST['submit']) && !empty($_POST['submit'])){
// use a boolean variable to keep track of errors
$error = false;
// Username validation
$username = trim($_POST['username']);
$username_pattern = "/^[a-zA-Z0-9]{3,15}$/"; // username should contain only letters and numbers, and length should be between 3 and 15 characters
if(preg_match($username_pattern, $username)){
// success
}else{
// error
$error = true;
}
// Password validation
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
if(strlen($password) >= 6 && $password===$confirm_password){ // length of the password should be greater than or equal to 6
// success
}else{
// error
$error = true;
}
// First name validation
$first_name = trim($_POST['first_name']);
$first_name_pattern = "/^[a-zA-Z]{2,15}$/";
if(preg_match($first_name_pattern, $first_name)){ // first name should contain only letters and length should be between 2 and 15 characters
// success
}else{
// error
$error = true;
}
// Last name validation
$last_name = trim($_POST['last_name']);
$last_name_pattern = "/^[a-zA-Z]{2,15}$/";
if(preg_match($last_name_pattern, $last_name)){ // last name should contain only letters and length should be between 2 and 15 characters
// success
}else{
// error
$error = true;
}
// Email validation
$email = trim()
$email_pattern = "/^([a-z0-9\._\+\-]{3,30})@([a-z0-9\-]{2,30})((\.([a-z]{2,20})){1,3})$/";
if(preg_match($email_pattern, $email)){ // validate email addresses
// success
}else{
// error
$error = true;
}
if(!$error){
// all fields are validated. Now do your database operations.
}else{
// display error message
}
}
并使用PDO prepare来阻止您的数据库进行任何类型的SQL注入。
来自 PDO :: prepare
为将使用不同参数值多次发出的语句调用PDO :: prepare()和PDOStatement :: execute()通过允许驱动程序协商客户端和/或服务器端缓存来优化应用程序的性能查询计划和元信息,通过消除手动引用参数的需要,有助于防止SQL注入攻击。
答案 1 :(得分:0)
我在发送到服务器之前为您检查和验证了字段。请看一看。
<script>
function checknull(){
if(document.getElementById("createUserName").value == ""){
alert("enter user name");
return false;
}
return true
}
</script>
<form action="createaccount.php" name="form" method="post" onsubmit="return checknull()">
它不是一个完整的例子。只是让你知道如何通过javascript进行空值检查。