带验证码的登录页面
HTML页面中用表单标签的post方式提交到相应的php后台文件
<html lang="en">
<head><meta charset="UTF-8"><title>登录页面</title>
</head>
<body>
<form method="post" action="./server/dologin.php"><input type="text" placeholder="用户名" name="username"><br><br><input type="password" placeholder="密码" name="password"><br><br><input type="text" placeholder="验证码" name="verifycode" class="captcha"><br><br><img id="captcha_img" src="./server/captcha.php?r=<?php echo rand();?>" alt="验证码"><label><a href="javascript:void(0)" rel="external nofollow" onclick="document.getElementById('captcha_img').src='./server/captcha.php?r='+Math.random()">换一个</a> </label><br><label><input type="checkbox" name="autologin[]" value="1"/>自动登录</label><br><button type="submit">登录</button><button type="button"><a href="register.php">注册</a></button>
</form>
</body>
</html>`
PHP文件中引入pdo的连接文件,并接受来自HTMl页面中输入框的值
用if语句去分别判断账号密码是否为空,验证码是否相同并且查询数据库中是否有对应的数据,
如果if判断不成功则用js的弹出框弹出相应的错误提示信息。
判断验证码是否正确
function checkVerifycode($verifycode,$code){if($verifycode==$code){return true;}else{echo '<html><head><Script Language="JavaScript">alert("验证码错误");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=../login.php\">";}
}
判断用户是否在数据库中(PDO方式)
function checkUser($username,$password){include 'mysql.php';$sql="select * from user where username=:username and password=:password";$sm = $conn->prepare($sql);$sm->bindParam(':username',$username);$sm->bindParam(':password',$password);if($sm->execute()){return true;}else{echo '<html><head><Script Language="JavaScript">alert("用户不存在");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=../login.php\">";}}
登录页面展示: