我正在制作一个比较两个变量
的验证页面第一个是一个随机代码,该代码先前是从python脚本生成的,并以输出的名称$代码示例(8063D0A7(将结果带到了PHP变量,这是数字和字母的8个字符
第二个是用户输入($ verf(
当用户单击提交($代码(和($ verf(时,如果为true,则将转移到其他页面,如果没有,请再次尝试
我尝试了很多方法,但是无论如何,它总是显示错误,任何输入
<?php
session_start(); ///starts a session and getting the variables from another page
echo "E-mail has been sent to " ;
echo $_SESSION['email']; ///gets $email from another page
echo $email , " ";
echo $_SESSION['code']; ///gets the $code from another page
$email = escapeshellarg($_SESSION['email']); ///make an arg to put in bash script
$code = escapeshellarg($_SESSION['code']);
$addr = shell_exec("./test.sh $email $code"); ///execute bash script to send $code to $email
?>
<!DOCTYPE HTML>
<html>
<body>
<h2>E-mail Verfication</h2>
<form method="post" action="">
Name: <input type="string" name="verf" value="">
<br><br>
<input type="submit" name="submit2" value="Submit">
</form>
<?php
if (isset($_POST['submit2'])) {
$verf = $_POST['verf'];
if ($verf == $code) {
echo "Correct!";
header('Location: 12.php');
} else {
echo "Wrong!";
}
} else {
echo "please fill the verification";
}
echo $verf;
echo $code;
?>
</body>
</html>
我认为识别变量的问题是一个问题,例如以$代码为字符串,将$ verf作为其他类型的输入,所以它永远是错误的,我不知道我是新手PHP help plz ..:D
问题很简单 - 这是因为您使用escapeshellarg()
,它在字符串周围添加单引号并引用/逃脱任何现有的单个引号(检查手册:PHP Escapeshellarg
因此,在您的情况下:
// lets say:
$_SESSION['code']="abc";
// then you do:
$code = escapeshellarg($_SESSION['code']);
// this means that now, $code is actually "'abc'" instead of "abc"
echo $code;
// so, if
$verf = "abc";
// then of course, $code is NOT the same with $verf;
echo $code == $verf ? "correct" : "incorrect";
因此,在您的情况下,您应该更改此行:
//$verf = $_POST['verf'];
$verf = escapeshellarg($_POST['verf']);
下次,尝试通过回声进行调试:$ verf vs $ code。
编辑。对评论的回应:
要删除数据中可以使用的空空间:trim((
$code = " A1B3 ";
$code = trim($code);
echo $code;
//A1B3
或,要删除所有不需要的炭(Ex。Chars不是A-Z或0-9(,您可以使用:Preg_replace((
$code = " A1B3?!#@! ";
$code = preg_replace("/[^A-Z0-9]/", "", $code);
echo $code;
//A1B3