session + cookie 实现登录功能
前端部分代码实现
<div id="mainCp">
<div class="postForm">
<p>
<label>用户账号:</label>
<input name="oldpwd" type="text" id="username" class="intxt" />
</p>
<p class="cellBg">
<label>用户密码:</label>
<input name="userpwd" type="password" id="userpwd" class="intxt" />
<span id="_userpwdok"></span>
</p>
<p class="cellBg">
<label>重复密码:</label>
<input name="userpwd" type="password" id="reuserpwd" class="intxt" />
<span id="_userpwdok"></span>
</p> <p class="cellBg">
<label>验证码号:</label>
<input name="vdcode" type="text" id="vdcode" class="intxt" />
<img id="code" src="" align="absmiddle" alt="看不清?点击更换" style="cursor:pointer;" onclick="getCode()">
</p> <p>
<button class="button2" type="button" id="login">登录账号</button>
<button class="button2 ml10" type="reset">重新填写</button>
</p>
</div>
</div>
<style>
#out{
position: fixed;
top:0;
left:0;
z-index: 10000;
background: rgba(0,0,0,0.7);
display:flex;
align-items: center;
justify-content: center;
overflow: hidden;
width:0px;
height:0px;
}
#in{
margin: 200px auto;
width:300px;
height:100px;
display:flex;
align-items: center;
justify-content: center;
background:#fff;
border-radius: 14px;
font-size: 16px;
font-weight: bold;
}
</style>
<div id="out">
<div id="in"></div>
</div>
<script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>
<script src="/public/static/js/StrPlay.class.js"></script>
<script src="/public/static/js/Ele.class.js"></script>
<script src="/public/static/js/loginCheck.js"></script>
<script>
// ------ 获取验证码
function getCode() {
var imgCode = find('#code');
imgCode.src = 'http://0609.cn/Config/image/imgcode.php?num=6&y=34&x=150' + "&time=" + new Date().getTime();
}
// ------ 初始化
function init() {
loop("input",function(item) {
item.value = "";
})
}
// ------ 遍历输入框
function loop(str,fn) {
find(str).forEach((item,index)=> {
fn(item);
});
}
// ------ 代码运行时长
function runLen(fn) {
window.addEventListener('load',function(){
console.group("程序运行时长:");
console.time();
fn();
console.timeEnd();
console.groupEnd();
});
}
// ------ 输入检测
function inputCheck(e) {
e.username('username');
e.password('userpwd');
e.repassword('reuserpwd','userpwd');
}
// ------ 点击登录,获取表单信息,检查数据是否合格,请求服务端检测程序
function goLogin() {
show("正在处理,请稍后 . . . .");
var param = {};
param.username = find('#username').value;
param.password = find('#userpwd').value;
param.code = find('#vdcode').value;
$.post('http://0609.cn/public/index.php/check',param,function(data) {
data = data.split(",").map(item => {
return item.split("=>");
})
let mdata = new Map(data);
find('#in').innerHTML = mdata.get('login');
});
}
// ------ DOM效果展示
function show(tip) {
var pageWidth = window.innerWidth;
var pageHeight = window.innerHeight;
if (typeof pageWidth != "number"){
if (document.compatMode == "CSS1Compat"){
pageWidth = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientHeight;
} else {
pageWidth = document.body.clientWidth;
pageHeight = document.body.clientHeight;
}
}
find('#out').style.cssText = "display:block;width:" + pageWidth + "px;height:" + pageHeight +"px";
find('#in').innerHTML = tip;
find('#out').addEventListener("click",function(){
find('#out').style.cssText = "display:none;";
},true);
}
// ------ 通过选择器,获取DOM元素
function find(str) {
return str.charAt(0) === "#" ?
document.querySelector(str) : document.querySelectorAll(str);
}
// ------ 错误异常检测
function errorCheck() {
window.addEventListener('error',function(error){
if(error.filename){return false;}else{
console.log(error.target+"资源加载失败"+":");
console.log(error.target);}
},true);
window.onerror = function(message, source, lineno, colno, error) {
console.log("程序运行出错:"+message+" at "+lineno+":"+colno);
return true;
}
window.addEventListener('unhandledrejection',function(event){
event && event.preventDefault();
console.log("数据接口请求失败");
},true);
} // ------ 开始运行程序
runLen(function() {
var e = new Check();
init();
inputCheck(e);
getCode();
loop("input",function(item){
item.addEventListener('blur',function() {
if(!Object.values(e.fit).includes(false)){
find('#login').addEventListener('click',goLogin,true);
}else{
find('#login').removeEventListener('click',goLogin,true);
}
},true);
});
errorCheck();
}); </script>
服务端代码实现
namespace app\index\controller;
use app\index\model\Data as MD;
class Checklogin
{
public function __construct()
{
// parent::__construct();
}
public function check()
{
header('Content-type:text/html;charset=utf-8;');
$code=strtolower($this->checkcode());
$username=$this->userlist()->username;
$password=$this->userlist()->password;
sleep(3);
if($_REQUEST['code'] != $code)
{
echo "login=>验证码错误";
exit;
}
if($_REQUEST['username'] != $username)
{
echo "login=>用户名或密码错误";
exit;
}
if($_REQUEST['password'] != $password)
{
echo "login=>用户名或密码错误";
exit;
}
$str="";
foreach($this->userlist() as $k=>$v){
$str .= $k."=>".$v.",";
}
echo $str."login=>登录成功";
exit;
}
public function checkcode(){
session_start();
return $_SESSION['code'];
}
public function userlist(){
$user = new \stdClass();
$user->username = "username";
$user->password = "123456";
$user->name = "Tom json";
$user->age = "50";
$user->sex = "man";
$user->vip = "是";
return $user;
}
}

session + cookie 实现登录功能的更多相关文章
- Django框架之cookie和session及开发登录功能
1.cookie是什么? Web应用程序是使用HTTP协议传输数据的.HTTP协议是无状态的协议.一旦数据交换完毕,客户端与服务器端的连接就会关闭,再次交换数据需要建立新的连接.这就意味着服务器无法从 ...
- 步步为营-76-用户登录(Session+Cookie)
说明:cookie是保存在浏览器的.Session是存储在服务器的 1 同样UI页面还是web前端提供 1.1 首先,经过验证码校验:将系统产生的验证码放入到Session中,然后取Session值注 ...
- session、cookie 记住登录状态的实现
Cookie的机制 Cookie是浏览器(User Agent)访问一些网站后,这些网站存放在客户端的一组数据,用于使网站等跟踪用户,实现用户自定义功能. Cookie的Domain和Path属性标识 ...
- 自己Cookie写的自动登录功能 包含BASE64 和MD5的使用
sql表 username password字段 User类 有 id username password等字段 Service有一函数 @Override public User findUser ...
- cookie理解与实践【实现简单登录以及自动登录功能】
cookie理解 Cookie是由W3C组织提出,最早由netscape社区发展的一种机制 http是无状态协议.当某次连接中数据提交完,连接会关闭,再次访问时,浏览器与服务器需要重新建立新的连接: ...
- 在基于vue的webpack脚手架开发中使用了代理转发,结果浏览器发出的请求中不带cookie导致登录时总是session失效怎么办?
环境: 有2个业务接口需要转发到82的服务器上: ../user/getCode.do ../user/doLogin.do 现象: 使用上述的 ...
- 使用COOKIE实现登录 VS 使用SESSION实现登录
注:本文使用的代码基于PHP,其他语言逻辑同理. 一:使用COOKIE实现登录验证 使用cookie实现登录的方式,主要通过一些单向的加密信息进行验证.比如admin用户登录了之后,服务端生成一个co ...
- 完成登录功能,用session记住用户名
登录功能完成: js:设置return html:设置 form input py: @app.route设置methods GET POST 读取表单数据 查询数据库 用户名密码对: 记住用户名 跳 ...
- 通过jquery.cookie.js实现记住用户名、密码登录功能
Cookies 定义:让网站服务器把少量数据储存到客户端的硬盘或内存,从客户端的硬盘读取数据的一种技术: 下载与引入:jquery.cookie.js基于jquery:先引入jquery,再引入:jq ...
随机推荐
- 【大厂面试06期】谈一谈你对Redis持久化的理解?
Redis持久化是面试中经常会问到的问题,这里主要通过对以下几个问题进行分析,帮助大家了解Redis持久化的实现原理. 1.Redis持久化是什么? 2.Redis持久化有哪些策略?各自的实现原理是怎 ...
- Crypto++ AES 加密解密流程
// aesdemo.cpp : 定义控制台应用程序的入口点. // #include <stdio.h>#include <tchar.h>#include <iost ...
- Python 图像处理 OpenCV (10):图像处理形态学之顶帽运算与黑帽运算
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- centos7 hadoop 单机模式安装配置
前言 由于现在要用spark,而学习spark会和hdfs和hive打交道,之前在公司服务器配的分布式集群,离开公司之后,自己就不能用了,后来用ambari搭的三台虚拟机的集群太卡了,所以就上网查了一 ...
- JSONobject按照put顺序存储和读取
new的时候加true即可: JSONObject jsonObject = new JSONObject(true);
- vc6.0转vs2012的一些错误与解决方法
1>------ 已启动生成: 项目: NMW210, 配置: Debug Win32 ------ abs_position = fabs((float)posiTemp1 - (float) ...
- program files (x86)\microsoft visual studio 14.0\vc\include\xtree,如果没有找到,下标溢出了,就报错咯
---------------------------Microsoft Visual C++ Runtime Library---------------------------Debug Asse ...
- XP系统无法进入界面 不断的反复重启-解决方法
XP系统无法进入界面 不断的反复重启-解决方法 XP系统无法进入界面 不断的反复重启-解决方法 一般都是非正常关机导致磁盘受到损坏.需要修复磁盘. 1.插入带PE的u盘,进入PE系统后 2.win+R ...
- 关于margin外边距合并的问题
一 .兄弟元素margin外边距合并演示 当两个垂直方向相邻的兄弟元素都为常规流块盒,他们之间垂直方向的外边距不是两者之和,而是取两者中的最大值.这种现象被称为相邻的兄弟元素垂直方向外边距合并. ...
- cron计划任务
格式 crontab -e [-u 用户名] ##编辑:注意,每项工作都是一行. crontab -l [-u 用户名] ##查看 crontab -r [-u 用户名] #清除 分 时 日 月 周 ...