PHP实现验证码登录

1.登录页面

<!DOCUMENT html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http
://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
	<title>验证码使用案例</title>
	<style type="text/css">
		ul li{margin:0;padding:0;}
		form{margin:40px 30px 0;}
		form li{list-style:none;padding:5px 0;}
		form li label{float:left;width:70px;text-align:right}
		form li a{font-size:12px;color:#999;text-decoration:none}
		.login_btn{border:none;background:#01A4F1;color:#fff;font-size:14px;font-weight:bold;height:28px;line-height:28px;padding:0 10px;cursor:pointer;}
		form li img{vertical-align:top}
	</style>

</head>
<body>
<form action="checkcode.php" method="POST">
实现验证码登录的基本步骤如下: 1. 生成验证码图片:使用PHP的GD库生成验证码图片,并将验证码文本保存到session中。 2. 显示验证码图片:将生成的验证码图片显示到登录界面上,让用户输入验证码。 3. 验证验证码:当用户提交登录表单时,获取用户输入的验证码文本,并与session中保存的验证码文本进行比较。 4. 验证用户信息:如果验证码正确,则使用用户输入的用户名和密码验证用户信息是否正确。 5. 登录成功:如果用户名和密码都正确,则将用户信息保存到session中,并跳转到登录后的页面。 以下是一个简单的验证码登录的示例代码: ```php <?php // 生成验证码 session_start(); $code = rand(1000, 9999); $_SESSION['code'] = $code; $im = imagecreate(60, 20); $bg = imagecolorallocate($im, 0, 0, 0); $fg = imagecolorallocate($im, 255, 255, 255); imagestring($im, 5, 5, 2, $code, $fg); header('Content-type: image/png'); imagepng($im); imagedestroy($im); // 处理登录 if ($_SERVER['REQUEST_METHOD'] == 'POST') { $username = $_POST['username']; $password = $_POST['password']; $captcha = $_POST['captcha']; if ($captcha != $_SESSION['code']) { echo '验证码错误'; } else if ($username == 'admin' && $password == '123456') { session_start(); $_SESSION['username'] = $username; header('Location: index.php'); } else { echo '用户名或密码错误'; } } ?> <form method="post"> <label>用户名:<input type="text" name="username"></label><br> <label>密码:<input type="password" name="password"></label><br> <label>验证码:<input type="text" name="captcha"></label><br> <img src="captcha.php"><br> <button type="submit">登录</button> </form> ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值