2013-12-18 60 views
1

我正在創建一個帶有驗證碼的表單,並且我將這些代碼存儲在隱藏的輸入框中,但輸入框存儲以前的會話代碼。我正在猜測它與session_stars有什麼共同之處,我一直在擺弄它,但無濟於事。會話回顯的前一個變量

請問有人能指點我正確的方向!

php文件:

<body> 
    <?php 
    session_start(); 
    ?> 
    ... 
    <div id="reg-field"> 
     <h1>Human Verification</h1> 
     <div id="captions"> 
      <img src="PHP/captcha.php" style="float: right;" class="cnr-all" /> 
     </div> 
     <form name="Form3" method="post"> 
      <input type="text" id="captcha-bar" class="cnr-all" name="answer" placeholder="Enter Code Here" /> 
      <input type="hidden" name="answer2" value="<?php echo $_SESSION['captcha']; ?>"/> 
      <input type="button" id="captcha-reload" class="cnr-all" onclick="window.location.href = window.location.href"> 
      <span id="captchaConfirm" class="confirmMessage" style="margin: 12.5px 0px"></span>    
     </form> 
    </div>  
    ... 
    </body> 

驗證碼PHP創建者:

<?php 
session_start(); 

header("Expires: Tue, 01 Jan 2013 00:00:00 GMT"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 

$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
$randomString = ''; 

for ($i = 0; $i < 5; $i++) 
{ 
    $randomString .= $chars[rand(0, strlen($chars)-1)]; 
} 

$_SESSION['captcha'] = strtolower($randomString); 


$im = @imagecreatefrompng("captcha_bg.png"); 


imagettftext($im, 30, 0, 10, 38, imagecolorallocate ($im, 0, 0, 0), 'larabiefont.ttf', $randomString); 

header ('Content-type: image/png'); 
imagepng($im, NULL, 0); 
imagedestroy($im); 

?> 

回答

1

第一個文件的PHP將在任何HTML發送到客戶端之前執行。

含義:來自SESSION的代碼將在客戶端請求captcha.php之前輸出,生成一個新的驗證碼。

您可以通過在第一個文件中生成驗證碼來解決此問題。 甚至更​​好,不要將它打印出來HTML(隱藏輸入是一個安全問題),只是比較用戶的答案,無論你在$ _SESSION。 這樣你就可以刷新captcha的所有你喜歡的,直到用戶提交它的代碼。你那麼POST [ '驗證碼']比作SESSION [ '驗證碼']

另外:http://www.w3.org/TR/turingtest/

文件1:

<?php 
    session_start(); 
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    $randomString = ''; 

    for ($i = 0; $i < 5; ++$i) 
    { 
     $randomString .= $chars[rand(0, strlen($chars)-1)]; 
    } 

    $_SESSION['captcha'] = strtolower($randomString); 
?> 
<body> 
... 
<div id="reg-field"> 
    <h1>Human Verification</h1> 
    <div id="captions"> 
     <img src="PHP/captcha.php" style="float: right;" class="cnr-all" /> 
    </div> 
    <form name="Form3" method="post"> 
     <input type="text" id="captcha-bar" class="cnr-all" name="answer" placeholder="Enter Code Here" /> 
     <input type="hidden" name="answer2" value="<?php echo $_SESSION['captcha']; ?>"/> 
     <input type="button" id="captcha-reload" class="cnr-all" onclick="window.location.href = window.location.href"> 
     <span id="captchaConfirm" class="confirmMessage" style="margin: 12.5px 0px"></span>    
    </form> 
</div>  
... 
</body> 

文件2:

<?php 
session_start(); 

header("Expires: Tue, 01 Jan 2013 00:00:00 GMT"); 
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
header("Cache-Control: no-store, no-cache, must-revalidate"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 

$im = @imagecreatefrompng("captcha_bg.png"); 


imagettftext($im, 30, 0, 10, 38, imagecolorallocate ($im, 0, 0, 0), 'larabiefont.ttf', $_SESSION['captcha']); 

header ('Content-type: image/png'); 
imagepng($im, NULL, 0); 
imagedestroy($im); 

?> 
+0

我使用Javascript驗證和比較兩個字段,它仍然有可能這樣嗎? – horHAY

+0

更好地通過與服務器的AJAX呼叫比較驗證碼。將用戶輸入發送到服務器,PHP會對其進行比較,向客戶端發送響應,並根據PHP的結果最終向用戶提供反饋。 –

+0

我必須使用JS(不要問),據我所知這是最安全的選擇(我知道安全風險),雖然對於人類驗證它應該沒問題? – horHAY

1

session_start()必須在任何輸出之前。把它作爲第一行代碼是最安全的。您之前有一個<body>標籤,因此它會失敗。

+0

複製在頁面頂部的PHP代碼,並刪除正文標籤session_start ... – Kishore