PHP登录界面加图片中文验证码验证登录

版权声明: https://blog.csdn.net/l269798518/article/details/80490340

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>验证码表单</title>
</head>
<body>
	<fieldset style="width: 500px; height: 400px;">
		<legend>验证码</legend>
		<form action="login.php" method="post">
			<label>用户名:</label>
			<input type="text" name="username"><br><br>
			<label>密码:</label>
			<input type="password" name="password"><br><br>
			<label>验证码:</label>
			<input type="text" name="captcha"><br>
			<img src="exercise06.php" onclick="this.src='exercise06.php?name='+Math.random()">
			<p>点击图片更换验证码</p>
			<input type="submit" name="sub">
		</form>
	</fieldset>
</body>
</html>

login.php

<?php 
	header('Content-type:text/html; charset=utf-8');
	// 开启Session
	session_start();
	// 获取用户输入的验证码
	$captcha = trim($_POST['captcha']);
	if ($captcha == $_SESSION['char']) {
		echo "验证码正确".'<br>';
		// 获取输入的用户名和密码
		$username = $_POST['username'];
		$password = $_POST['password'];
		if ($username == "admin" && $password == "123") {
			echo "欢迎你,".$username;
		} else {
			echo "用户名或密码错误!<a href='login.html'>重新登录</a>";
		}
	} else {
		echo "验证码错误!<a href='login.html'>重新登录</a>";
	}
 ?>
captcha.php(构造验证码):
<?php 
	header('Content-type:text/html; charset=utf-8');
	// 验证码案例
	// 创建图片资源
	$img = imagecreatetruecolor(200,50);

	// 加背景色
	$bg_color = imagecolorallocate($img, 150, 150, 150);
	imagefill($img, 0, 0, $bg_color);

	// 构造随机文字
	// 定义一个字符串集
	$str = '放假啊吧经费为哦范文芳分别为我分为氛围为为额而炯日俄公文佛网为为而后哦气哦额我问哦亲俄';

	// 获取字符串长度
	$len = strlen($str);  //是字节数,utf-8下每个汉字占3个字节
	$c_len = $len / 3;  //汉字数

	// 随机从上述字符串集中取出字符
	$rand1 = mt_rand(0, $c_len - 1);   //随即汉字的索引
	$rand2 = mt_rand(0, $c_len - 1);   //随即汉字的索引
	$char1 = substr($str, $rand1 * 3, 3);
	$char2 = substr($str, $rand2 * 3, 3); //取两个随机汉字

	// 将得到的两个汉字拼成一个字符串,保存到Session中以便服务器做验证
	$char = $char1.$char2;
	// 开启Session
	session_start();
	$_SESSION['char'] = $char;

	// 将取到的汉字放入图片中
	// 写入文字
	$str_color1 = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
	$str_color2 = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
	imagettftext($img, mt_rand(20,30), mt_rand(10,45), 50, 40, $str_color1, 'simsun.ttc', $char1);
	imagettftext($img, mt_rand(20,30), mt_rand(-45,-10), 100, 35, $str_color2, 'simsun.ttc', $char2);

	// 做干扰噪点或直线可以调整干扰到文字的下层,移动代码
	for ($i=0; $i < 50; $i++) { 
		// 点:写字符
		$dots_color = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
		imagestring($img, mt_rand(1, 5), mt_rand(0, 200), mt_rand(0, 50), ".", $dots_color);
	}

	// 增加干扰线
	for ($i=0; $i < 10; $i++) { 
		// 线:画直线
		$line_color = imagecolorallocate($img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
		imageline($img, mt_rand(0, 200), mt_rand(0, 50), mt_rand(0, 200), mt_rand(0, 50), $line_color);
	}

	// 指定输出格式
	header('Content-type:image/png');
	// 输出验证码
	imagepng($img);
	// 销毁图片资源
	imagedestroy($img);
 ?>
这个案例涉及到了PHP中的表单传值、Session会话技术、PHP图像处理技术等知识,适合向我这样的初学者练习。

猜你喜欢

转载自blog.csdn.net/l269798518/article/details/80490340