php - PHP在按下提交后重复代码
问题描述
我正在尝试制作一个验证页面,在用户键入验证码后,使用 python 脚本向用户电子邮件发送随机生成的代码,它将与从脚本生成的代码进行比较,如果正确,则重定向到另一个页面不是那么代码将打印错误,用户应该再试一次....问题是每次用户按下提交时,代码都是通过电子邮件发送的,这意味着当我按下提交时,这行将再次执行
$addr = shell_exec("python test.py $email $code ");
尝试使用exit()和die()并没有发生任何事情
<?php
session_start();
echo "E-mail has been sent to " ;
echo $_SESSION['email'];
echo $email , " ";
$_SESSION["code"];
$email = escapeshellarg($_SESSION['email']);
$code = escapeshellarg($_SESSION['code']);
$code = preg_replace("/[^A-Z0-9]/", "", $code);
$addr = shell_exec("python test.py $email $code ");
?>
<!DOCTYPE HTML>
<html>
<body>
<h2>E-mail Verfication</h2>
<form method="post" action="">
Name: <input type="text" name="verf" value="">
<br><br>
<input type="submit" name="submit2" value="Submit">
</form>
<?php
if (isset($_POST['submit2'])) {
$verf1 = escapeshellarg($_POST['verf']);
$verf2 = preg_replace("/[^A-Z0-9]/", "", $verf1);
if ($verf2 == $code) {
echo "Correct!";
echo "<script type='text/javascript'>window.top.location='finish.php';</script>"; exit;
$ip = $_SERVER['REMOTE_ADDR'];
$block = shell_exec("./block.sh $ip ");
} else {
echo "Wrong!";
}
} else {
echo "please fill the verification";
}
?>
</body>
</html>
</body>
</html>
解决方案
$addr = shell_exec("python test.py $email $code ");
每次加载页面时都会执行代码。您可以在此代码之前添加条件。
if (!isset($_POST['submit2'])){
$addr = shell_exec("python test.py $email $code ");
}
推荐阅读
- ios - Firebase facebook 身份验证崩溃
- php - Cakephp 3.6 - 使用数据库函数保存
- r - 使用 purrr 包操作 url 中的两个字符用于抓取目的
- r - R统计:将不规则文件的子集读入数据帧
- android - org.json.JSONException:Java.jang.String 类型的值不成功,无法转换为 JSONObject
- javascript - 启用 tsconfig.json 为 .js 文件 compileOnSave
- xamarin.forms - 全局样式在 iOS 中不起作用
- c# - 是否可以使用面向方面的方法通过 Autofac 和 DynamicProxy 登录 Azure 函数
- c# - 在保持登录状态时使用 C# POST 数据(登录有效但之后不发布......)
- python - 在 Python 3 中从序列中打印多个变量