0
我確定這個問題很多,但每個人的代碼都不一樣,但我在驗證註冊表單時遇到了問題。爲什麼我的表單被錯誤驗證?
我會在代碼片段下面發佈我的代碼。
現在我究竟做錯了什麼?我想確保填寫每個字段,但即使我使用下面的代碼,或者添加了帶有錯誤echo的die()函數,其餘代碼仍然會執行並且表單將被髮送。
下面的代碼片段是我一直在嘗試的東西。
if(isset($_POST["submit"])){
if(empty($firstname) || empty($lastname) || empty($username) || empty($password) ||
empty($passwordconf)
{
echo "You did not fill out the required fields.";
die();
}
$user=$_POST['user'];
$pass=$_POST['pass'];
這是我現在的代碼。
if(isset($_POST["submit"])){
$user=$_POST['user'];
$pass=$_POST['pass'];
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$username = "DBname";
$password = "DBpass";
$hostname = "DBhost";
$md5pass = md5($pass);
if(empty($email)){ //this is what I've tried
$errors[] = "email cannot be left empty";
}
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to server");
$con=mysql_connect($hostname, $username, $password) or die(mysql_error());
$select=mysql_select_db("Database", $con) or die("cannot select DB");
$query=mysql_query("SELECT * FROM login WHERE username='".$user."'");
$numrows=mysql_num_rows($query);
if($numrows==0)
{
$sql="INSERT INTO login(username,password,firstname,lastname,email)
VALUES('$user','$md5pass','$fname','$lname','$email')";
$result=mysql_query($sql);
if($result){
echo "Account Successfully Created, Please login to continue";
} else {
echo "Failure!";
}
} else {
echo "That username already exists! Please try again with another.";
}
}
你的數據庫是注射 –
**非常脆弱的危險**:你是混合[**的過時的**數據庫API](http://stackoverflow.com/q/12859942/19068)與現代的(這將無法正常工作),只能使用[現代替代](http://php.net/手動/ EN/mysqlinfo.api.choosing.php)。你也**易受[SQL注入攻擊](http://bobby-tables.com/)**,現代的API會使[防禦]更容易(http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php)自己從。 – Quentin
您正在使用[不適合的哈希算法](http://php.net/manual/en/faq.passwords.php),需要[更好地照顧](https://www.owasp.org/index。 php/Password_Storage_Cheat_Sheet)的用戶密碼。 – Quentin