我正在尝试在包含另一个PHP之前验证输入,以便用户可以在同一页面上获取错误,如果有一个... 4输入是必需的但其他是可选的..我想要做的是如果用户只填写4个必需的输入验证输入,而不是包含另一个PHP文件(而不是包括PHP我改为使用JavaScript来提醒我代码运行良好..)并且如果用户也填写其他可选输入为了验证它们并包含一个PHP文件,我遇到的问题是,即使用户插入无效字符,它仍然会提醒我代码是好的。 如果用户使用允许的字符填充输入,我只希望它处理最后的JavaScript ..
这是PHP:
<?php
// define variables and set to empty values
$titleErr = $MdescripErr = $posterErr = $vcodeErr = $vcode2Err = $vcode3Err = $mlink1Err = $mlink2Err = $mlink3Err = "";
$title = $Mdescrip = $poster = $comment = $vcode = $vcode2 = $vcode3 = $mlink1 = $comment = $mlink2 = $mlink3 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["title"]))
{$titleErr = "title is required";}
else
{
$title = test_input($_POST["title"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[A-Za-z0-9 ]*$/",$title))
{
$titleErr = "Only letters and white space allowed";
}
}
if (empty($_POST["Mdescrip"]))
{$MdescripErr = "Movie Description is required";}
else
{
$Mdescrip = test_input($_POST["Mdescrip"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[A-Za-z0-9 ]*$/",$Mdescrip))
{
$MdescripErr = "Only letters and white space allowed";
}
}
if (empty($_POST["poster"]))
{$posterErr = "Poster Link is required";}
else
{
$poster = test_input($_POST["poster"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$poster))
{
$posterErr = "Invalid URL";
}
}
if (empty($_POST["vcode"]))
{$vcodeErr = "Embed Link is required";}
else
{
$vcode = test_input($_POST["vcode"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$vcode))
{
$vcodeErr = "Invalid URL";
}
}
if (empty($_POST["vcode2"]))
{$vcode2 = "";}
else
{
$vcode2 = test_input($_POST["vcode2"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$vcode2))
{
$vcode2Err = "Invalid URL";
}
}
if (empty($_POST["vcode3"]))
{$vcode3 = "";}
else
{
$vcode3 = test_input($_POST["vcode3"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$vcode3))
{
$vcode3Err = "Invalid URL";
}
}
if (empty($_POST["mlink1"]))
{$mlink1 = "";}
else
{
$mlink1 = test_input($_POST["mlink1"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$mlink1))
{
$mlink1Err = "Invalid URL";
}
}
if (empty($_POST["mlink2"]))
{$mlink2 = "";}
else
{
$mlink2= test_input($_POST["mlink2"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$mlink2))
{
$mlink2Err = "Invalid URL";
}
}
if (empty($_POST["mlink3"]))
{$mlink3 = "";}
else
{
$mlink3 = test_input($_POST["mlink3"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$mlink3))
{
$mlink3Err = "Invalid URL";
}
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($title == NULL || $Mdescrip == NULL || $poster == NULL || $vcode == NULL)
{
}
else if (!preg_match("/^[A-Za-z0-9 ]*$/",$title) ||!preg_match("/^[A-Za-z0-9 ]*$/",$Mdescrip) || !preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$poster) || !preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$vcode))
{
}
else
{
?>
<script>
alert("it went through");
</script>
<?php
}
?>
答案 0 :(得分:0)
您应该尝试拆分网址并删除不必要的项目,例如“https://”,这样您就可以使用“http://www.stackoverflow.com/bla/bla/bla”之类的网址,并将其拆分为[“http”,“”,“www .stackoverflow.com”, “血乳酸”, “血乳酸”, “血乳酸”] 最后检查每个元素是否设置(有效),这将使您的验证非常简单