2012-05-18 72 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"; 
    } 
} 

?>