这是我计划在我的网站的注册表单中使用的一些代码
<?php
session_start();
include ("includes/config.php");
if ($Authenticated == False) {
?>
<div id="register">
<form action="index.php?page=register" method="post">
<lable id="registerStuff"> Name: </>
<input id="registerIn" type='text' name='name' maxlength="20" />
<br/>
<lable id="registerStuff"> Surname: </>
<input id="registerIn" type='text' name='surname' maxlength="20" />
<br/>
<lable id="registerStuff"> Username: </>
<input id="registerIn" type='text' name='username' maxlength="20" />
<br/>
<lable id="registerStuff"> Password: </>
<input id="registerIn" type='password' name='password' maxlength="20" />
<br/>
<lable id="registerStuff"> Retype Password: </>
<input id="registerIn" type='password' name='passwordcheck' maxlength="20" />
<br/>
<input type='submit' value='Register'>
<br/>
</form>
</div>
<?php
$name = mysql_real_escape_string($_POST['name']);
$surname = mysql_real_escape_string($_POST['surname']);
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string(md5($_POST['password']));
$passwordcheck = mysql_real_escape_string(md5($_POST['passwordcheck']));
if ($username OR $password OR $passwordcheck != null) {
if ($password == $passwordcheck) {
$query = "INSERT INTO users (name, surname, username, password) VALUES ('" . $name . "', '" . $surname . "', '" . $username . "', '" . $password . "')";
$result = mysql_query($query);
if (!$result) {
echo "Username wrong";
}
} else {
echo "passwords didnt match";
}
}
}
if ($Authenticated == True) {
header("Location: index.php");
}
?>
我似乎无法获得正确的错误消息,我需要检查输入的2个密码是否相同,我还需要确保不能在DB中输入空格。如果有人可以帮助我,我将不胜感激
答案 0 :(得分:2)
我猜你犯了一个很常见的错误。大多数初学者似乎都希望尽可能少编写代码。但是,在这种情况下,这是行不通的。你必须检查每个变量是否空虚。
更改if构造时,您的代码是正确的:
if ($username != "" AND $password != "" AND $passwordcheck != "") {
}
但是,为了使代码看起来更轻微,最好只使用返回 TRUE 的函数empty()
,如果变量为空:
if (!empty($username) AND !empty($password) AND !empty($passwordcheck)) {
}
答案 1 :(得分:1)
你有非常有趣的代码。我建议,在此之前要求人们写一半以上的脚本之前,谷歌“PHP登录脚本” - this
是第一个结果和问题的答案。
$sql="SELECT * FROM $tbl_name
WHERE username='$myusername' AND password='$mypassword'";
$result=mysql_query($sql);
话虽如此,这是不正确的做法,你应该研究准备好的陈述。自从你开始以来,你可能应该从安全思维开始。