我做了一些图像验证。用户必须从动态生成的图像中输入代码。
此代码正在主 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;
这就是我在会话中存储密钥的方式。
现在,问题是,图像可能会被阻止,并且会话密钥不会从上次存储的结果中改变。这意味着巨大的威胁。我想知道是否有任何保护或解决方法?
请仅在您了解问题和安全性的情况下回答:-)