PHP按提交后重复代码

时间:2019-07-10 10:01:56

标签: 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>

2 个答案:

答案 0 :(得分:0)

每次加载页面时都会执行

$addr = shell_exec("python test.py $email $code ");代码。您可以在此代码之前添加条件。

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

答案 1 :(得分:0)

尝试后,我已经找到了解决方案,只需把这行放在

$addr = shell_exec("python test.py $email $code ");

在此表单之前的页面上,这将仅执行一次代码