我做了一些圖像驗證。用戶必須從動態生成的圖像中輸入代碼。PHP:以任何方式來保護會話變量/ captcha?
此代碼是在主PHP文件正在使用:
@session_start();
if (isset($_POST['session_pw'])&&$_SESSION['result']==$_POST['security_im']) {
$_SESSION['pw']=$_POST['session_pw'];
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
}
if ($_SESSION['pw']=='mypassword'&&$_SESSION['ip'] == $_SERVER['REMOTE_ADDR']){
// OK we are in.
// Otherwise display a login form:
}else echo <<<EOD
<form method="POST">
<font color="blue"> Authorization is required:</font><br>
<input type="text" name="security_im" style="text-align:right;padding-right:1px;background-repeat:no-repeat;background-image:url(../imagecheck.php)"><br>
<input type="password" name="session_pw" style="text-align:right;padding-right:1px"><br>
<input type="submit" style="margin:0px">
</form>
EOD;
注意這一點:
background-image:url(../imagecheck.php)
這是我如何調用生成的圖像。這是它的內容:
<?php
session_start();
create_image();
exit();
function create_image() {
$md5 = "";
while (strlen($md5) == 0) {
$md5 = md5(mt_rand());
$md5 = preg_replace('/[^0-9]/', '', $md5);
}
$pass = substr($md5, 10, 5);
$_SESSION['result'] = $pass;
$width = 60;
$height = 20;
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$clr[1] = ImageColorAllocate($image, 204, 0, 204);
$clr[2] = ImageColorAllocate($image, 0, 204, 204);
$clr[3] = ImageColorAllocate($image, 204, 204, 0);
$R = rand(1, 3);
$black = ImageColorAllocate($image, 255, 255, 255);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
//Add randomly generated string in white to the image
ImageString($image, 5, 7, 2, $pass, $clr[$R]);
//Throw in some lines to make it a little bit harder for any bots to break
imageline($image, 0, 0, $width - 2, $height - 2, $grey);
imageline($image, 0, $height - 2, $width - 2, 0, $grey);
ImageRectangle($image, 0, 0, $width - 1, $height - 1, $grey);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image, null, 80);
//Free up resources
ImageDestroy($image);
}
?>
注:
$_SESSION['result'] = $pass;
這是我如何存儲在會話密鑰。
現在,問題是,圖像可能被阻止,並且會話密鑰不會從上次存儲的結果更改。這意味着巨大的威脅。我想知道是否有任何保護或解決方法?
如果你瞭解:-)
驗證碼不是「安全」功能。他們是行爲或權限邏輯。 – mario
您是否在每一個網頁上使用captcha?如果圖片被阻止,他們將無法驗證驗證碼。如果會話密鑰在提供圖像後相同,則知道它已被阻止。如果他們關閉了圖像呢?或有人竊取圖像? – Anthony