1

我有以下 PHP 脚本,但它似乎总是在检查会话,因此回显“请在用户输入代码之前输入正确的代码。

我想在代码实际触发之前点击事件并检查会话和表单的发布。如何在 PHP 中做到这一点?

这是我的代码:

<?php

session_start();
    if (md5($_POST['norobot']) == $_SESSION['randomnr2'])   
    { 
        // here you  place code to be executed if the captcha test passes
            echo "Code correct - email should send now";

}   

else {  
        // here you  place code to be executed if the captcha test fails
            echo "Code incorect please try again";
    }

?> 

我的提交按钮如下所示:

<input type="submit" id="submit_btn" class="button" value="Submit" />
4

3 回答 3

2

如果要检查会话变量是否已设置并且具有非空白值,则可以执行以下操作:

if(isset($_SESSION['THECODE']) && $_SESSION['THECODE']!='')
{
  // THECODE is set to something
}

=============

更新,当您添加代码时:

<?php

session_start();
if(isset($_POST['norobot']) && isset($_SESSION['randonnr2']))
{
  if (md5($_POST['norobot']) == $_SESSION['randomnr2'])   
  { 
    echo "Code correct - email should send now";
  }
  else
  {  
    echo "Code incorect please try again";
  }
}

?> 
于 2012-05-18T09:56:29.970 回答
2
<?php
    session_start();
    if (isset($_POST['norobot'])) {
        if (md5($_POST['norobot']) == $_SESSION['randomnr2']) {
            // here you  place code to be executed if the captcha test passes
            echo "Code correct - email should send now";
        }
        else {
            // here you  place code to be executed if the captcha test fails
            echo "Code incorect please try again";
        }
    }
?> 
于 2012-05-18T09:57:28.883 回答
1

为什么不做这样的事情?检查用户是否提交了任何内容,如果没有,则不执行代码。

<?php

session_start();

if(isset($_POST))
    if (md5($_POST['norobot']) == $_SESSION['randomnr2'])   
    { 
        // here you  place code to be executed if the captcha test passes
            echo "Code correct - email should send now";

}   

else {  
        // here you  place code to be executed if the captcha test fails
            echo "Code incorect please try again";
    }
}

?> 
于 2012-05-18T09:57:11.017 回答