我正在創建一個帶有驗證碼的表單,並且我將這些代碼存儲在隱藏的輸入框中,但輸入框存儲以前的會話代碼。我正在猜測它與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);
?>
我使用Javascript驗證和比較兩個字段,它仍然有可能這樣嗎? – horHAY
更好地通過與服務器的AJAX呼叫比較驗證碼。將用戶輸入發送到服務器,PHP會對其進行比較,向客戶端發送響應,並根據PHP的結果最終向用戶提供反饋。 –
我必須使用JS(不要問),據我所知這是最安全的選擇(我知道安全風險),雖然對於人類驗證它應該沒問題? – horHAY