我认为这篇文章似乎可能是其他人的“关闭”话题。但是,如果你帮我解决这个问题,那将是非常感谢。
我在网上找到了一些电子邮件验证码。不知何故,我一开始觉得很困惑,但是当我开始理解它并把它放在我的代码上时。有一个错误,我不知道如何。
问题:
代码:
<?php
if(isset($_POST['submit']))
{
$a = $_POST['username'];
$b = $_POST['password'];
$c = $_POST['firstname'];
$d = $_POST['lastname'];
$e = $_POST['month'];
$f = $_POST['day'];
$g = $_POST['year'];
$h = $_POST['contact'];
$i = $_POST['email'];
$j = $_POST['confirm'];
$code = md5(uniqid(rand()));
include("dbconnect.php");
$query = "SELECT * FROM `users`.`info` WHERE `username`='".$a."' AND `email_address`='".$i."'";
$queryQuery=$con->query($query);
$checker = mysqli_num_rows($queryQuery);
if (($a && $b && $c && $d && $h && $i && $j) == "")
{
print "<script type=text/javascript>
alert('All fields are required');
</script>";
}
else
{
if ($checker == 0 && $b != $j)
{
print "<script type=text/javascript>
alert('Password Mismatch');
</script>";
}
else if($checker == 0)
{
//print $a,$b,$c,$d,$e,$f,$g,$h,$i;
$insertQuery="INSERT INTO `users`.`info` (`username`,`password`,`firstname`,`lastname`,`month`,`day`,`year`,`contact_number`,`email_address`,`confirm_code`) VALUES ('$a','$b','$c','$d','$e','$f','$g','$h','$i','$code')";
$insertQueryResult=$con->query($insertQuery);
if ($insertQueryResult)
{
// send e-mail to ...
$to=$i;
// Your subject
$subject="Your confirmation link here";
// From
$header="From Admins of Publisita.com";
// Your message
$message="Your Comfirmation link \r\n";
$message.="Click on this link to activate your account \r\n";
$message.="http://www.gmail.com/confirmation.php?passkey=$code";
// send email
$sentmail = mail($to,$subject,$message,$header);
}
// if not found
else
{
print "<script type=text/javascript>
alert('Not found your email in our database')
</script>";
}
// if your email succesfully sent
if($sentmail)
{
print "<scrpit type=text/javascript>
alert('Your Confirmation link Has Been Sent To Your Email Address')
</script>";
}
else
{
print "<script type=text/javascript>
alert('Cannot send Confirmation link to your e-mail address')
</script>";
}
}
print "<script type=text/javascript>
alert('Successfully Registered');
</script>";
}
else
{
print "<script type=text/javascript>
alert('Information are already been used');
</script>";
}
}
}
?>
如果有人帮助我,这将是一件好事
答案 0 :(得分:2)
这一行:
if (($a && $b && $c && $d && $h && $i && $j) == "")
是无效的语法。如果你想确保这些值不是空的,你需要单独检查它们(你也想使用或(||
),因为只有一个必须是空的,你才能显示错误。你当前的代码会要求所有这些都是空的):
if ($a == "" || $b == "" ....) // or if (empty($a) || empty($b) ....)
或者提出一种更简洁的方法来做到这一点:
$fields = array($a, $b, $c, $d, $h, $i, $j);
if (count(array_filter($fields)) !== count($fields))
上面的代码将所有要检查的值放入一个数组中。然后它调用array_filter()
来删除任何错误的值(空字符串的类型是juggled为Boolean false)。如果剩余元素的数量不等于元素的起始数量,则一个或多个元素为空,您需要显示错误。
正如@NicolasDefranoux所指出,你对SQL injections持开放态度。确保在发布代码之前关闭该漏洞。