2012-05-18 69 views
1

我有以下的PHP脚本,但它似乎为会话始终检查,因此回声出“请输入正确的密码之前,用户甚至已经进入了一个码。的onclick提交检查PHP

我想。在之前的代码实际上火灾确实为会话和形式的岗位检查click事件怎么可以这样在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" /> 
+0

你似乎没有会话值被设置为任何事情的任何... –

回答

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"; 
     } 
    } 
?> 
+0

优秀这完美的作品。所以'if(isset($ _ POST ['norobot'])){'是点击处理程序? –

+0

@ sp-1986 no。检查$ _POST设置确保表单已提交。它与点击无关。 – psynnott

+0

不是点击处理程序。这将验证是否提交了名称为“norobot”的字段。谢谢。 – Dev

2

如果你想检查会话变量设置,并具有非空值,你可以这样做:

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

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

UPDATE,因为您添加代码:

<?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"; 
    } 
} 

?> 
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"; 
    } 
} 

?>