[英]How can I make my CAPTCHA script have a refresh link?
我有一個驗證碼腳本,它運作良好。 但是,我不知道如何刷新它。
這是verificationimage.php
:
<?php
header('Content-type: image/jpeg');
$width = 50;
$height = 24;
$my_image = imagecreatetruecolor($width, $height);
imagefill($my_image, 0, 0, 0xFFFFFF);
// add noise
for ($c = 0; $c < 40; $c++){
$x = rand(0,$width-1);
$y = rand(0,$height-1);
imagesetpixel($my_image, $x, $y, 0x000000);
}
$x = rand(1,10);
$y = rand(1,10);
$rand_string = rand(1000,9999);
imagestring($my_image, 5, $x, $y, $rand_string, 0x000000);
setcookie('tntcon',(md5($rand_string).'a4xn'));
imagejpeg($my_image);
imagedestroy($my_image);
?>
並且,具有以下形式的頁面:
<label for="verif_box" style="margin-top:10px;">Image Verification:</label>
<input name="verif_box" type="text" id="verif_box" autocomplete="off" minlength="4" maxlength="5" class="text" />
<img id="captcha" src="verificationimage.php?<?php echo rand(0,9999);?>" alt="This form can't be submitted because images aren't displayed" title="Verification image, type it in the box" width="50" height="24" align="absbottom" onclick="$('#verif_box').focus();" style="cursor:pointer;" /> (<a href="#captcha" class="fancybox">what's this?</a>)
<br />
<br />
<?php
//if the variable "wrong_code" is sent from previous page then display the error field
if(isset($_GET['wrong_code'])){?>
<div style="border:1px solid #990000; background-color:#D70000; color:#FFFFFF; padding:4px; padding-left:6px;width:295px;">The code you entered does not match the image. Please try typing what you see in the image to the right again.</div><br />
<?php ;} ?>
以下是mailer.php
發生的情況:
// check to see if verificaton code was correct
if(md5($verif_box).'a4xn' == $_COOKIE['tntcon']){
// if verification code was correct send the message and show this page
mail("email@domain.com", 'New WeeBuild Support Ticket: '.$subject, $emailContent, $headers);
mail($email, 'Thank you for contacting WeeBuild Support! (#'.$ticket.')', $emailContents, $headers2);
// add to database
$today = getdate();
$theDate = $today["weekday"].", ".$today["month"]." ".$today["mday"].", ".$today["year"];
mysql_select_db("database_name", $con);
$sql="INSERT INTO table_name (name, email, subject, message, ticket, ip_address, created)
VALUES ('$_POST[name]','$_POST[email]','$_POST[subject]','$_POST[message]','$ticket','$_SERVER[REMOTE_ADDR]','$theDate')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
//close the database connection
mysql_close($con);
// delete the cookie so it cannot sent again by refreshing this page
setcookie('tntcon','');
} else if(isset($message) and $message!=""){
// if verification code was incorrect then return to contact page and show error
header("Location: index.php?name=$name&subject=$subject&email=".urlencode($email)."&message=".urlencode($message)."&wrong_code=true");
exit;
}
這一切目前都很好,但我怎樣才能刷新驗證碼呢? 我試過這樣做:
<script>
$(document).ready(function() {
$('#refresh').click(function() {
$('img#captcha').attr('src','verificationimage.php?<?php echo rand(0,9999);?>');
});
});
</script>
<a href="#" id="refresh">refresh</a>
但是,它不起作用。
順便說一句,我不想使用reCaptcha,我只是這樣做,因為對我來說,reCaptcha看起來很難用我的東西來設置。
我的回答類似於評論中的答案,可能是在刷新時使用了緩存副本,但使用正確的方法確保瀏覽器不緩存該圖像而不是隨機字符串會更清晰。 在圖像生成頁面上,添加以下內容:
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");
由於圖像標記具有id,因此這是選擇器唯一需要的東西。
我認為問題來自瀏覽器緩存:因為隨機字符串是由PHP生成的,刷新按鈕只能工作一次(因為下一次調用將使用相同的字符串,瀏覽器將使用緩存版本)。
這應該是技巧,每次按下按鈕時都會生成一個新的隨機字符串:
$(function() {
$('#refresh').click(function() {
$('#captcha').attr('src', 'verificationimage.php?' + new Date().getTime());
return false;
});
});
除了使用cookie之外,還有更好,更安全的方式來發送CAPTCHA信息以進行驗證。 至少,我建議使用會話變量,但使用命名會話和IP驗證,以防止會話劫持。 為您的CAPTCHA圖像生成腳本添加熱鏈接防護也是一個非常好的主意,這樣有人就不能只在瀏覽器中提取CAPTCHA圖像。 只是一個建議。 :)
要使用熱鏈接防護,只需將以下代碼添加到映像創建腳本的頂部:
if (!isset($_SERVER['HTTP_REFERER']) or checkValidReferer() === false) die();
function checkValidReferer() {
$out = false;
$ref = $_SERVER['HTTP_REFERER'];
$lh = $_SERVER['HTTP_HOST'];
if (stripos($ref,$lh) !== false) $out = true;
return $out;
}
如果引用者的基本URL都存在,則上述代碼輸出良好的圖像,並且與圖像腳本的基本URL相同。 這可以防止圖像被用於除應該的位置之外的某個地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.