首页 > 解决方案 > 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>

标签: php

解决方案


$addr = shell_exec("python test.py $email $code ");每次加载页面时都会执行代码。您可以在此代码之前添加条件。

if (!isset($_POST['submit2'])){
  $addr = shell_exec("python test.py $email $code ");
}

推荐阅读