Uchome的登录验证机制
生活随笔
收集整理的这篇文章主要介绍了
Uchome的登录验证机制
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
2019独角兽企业重金招聘Python工程师标准>>>
登录:成功后设置cookie
//设置cookie ssetcookie('auth', authcode("$setarr[password]\t$setarr[uid]", 'ENCODE'), $cookietime); ssetcookie('loginuser', $passport['username'], 31536000); ssetcookie('_refer', '');其中我用到最重要的是:api/uc.php文件中
function synlogin($get, $post) {global $_SGLOBAL;if(!API_SYNLOGIN) {return API_RETURN_FORBIDDEN;}//note 同步登录 API 接口obclean();header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');$cookietime = 31536000;$uid = intval($get['uid']);$query = $_SGLOBAL['db']->query("SELECT uid, username, password FROM ".tname('member')." WHERE uid='$uid'");if($member = $_SGLOBAL['db']->fetch_array($query)) {include_once S_ROOT.'./source/function_space.php';$member = saddslashes($member);$space = insertsession($member);//设置cookiessetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);}ssetcookie('loginuser', $get['username'], $cookietime);} if($member = $_SGLOBAL['db']->fetch_array($query)) {
include_once S_ROOT.'./source/function_space.php';
$member = saddslashes($member);
$space = insertsession($member);
//设置cookie
ssetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
}
ssetcookie('loginuser', $get['username'], $cookietime);
验证:
//判断当前用户登录状态 function checkauth() {global $_SGLOBAL, $_SC, $_SCONFIG, $_SCOOKIE, $_SN;if($_SGLOBAL['mobile'] && $_GET['m_auth']) $_SCOOKIE['auth'] = $_GET['m_auth'];if($_SCOOKIE['auth']) {@list($password, $uid) = explode("\t", authcode($_SCOOKIE['auth'], 'DECODE'));$_SGLOBAL['supe_uid'] = intval($uid);if($password && $_SGLOBAL['supe_uid']) {// 查内存表 session表$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('session')." WHERE uid='$_SGLOBAL[supe_uid]'");if($member = $_SGLOBAL['db']->fetch_array($query)) {if($member['password'] == $password) {$_SGLOBAL['supe_username'] = addslashes($member['username']);$_SGLOBAL['session'] = $member;} else {$_SGLOBAL['supe_uid'] = 0;}} else {$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('member')." WHERE uid='$_SGLOBAL[supe_uid]'");if($member = $_SGLOBAL['db']->fetch_array($query)) {if($member['password'] == $password) {$_SGLOBAL['supe_username'] = addslashes($member['username']);$session = array('uid' => $_SGLOBAL['supe_uid'], 'username' => $_SGLOBAL['supe_username'], 'password' => $password);include_once(S_ROOT.'./source/function_space.php');insertsession($session);//登录} else {$_SGLOBAL['supe_uid'] = 0;}} else {$_SGLOBAL['supe_uid'] = 0;}}}}if(empty($_SGLOBAL['supe_uid'])) {clearcookie();} else {$_SGLOBAL['username'] = $member['username'];} } uchome2.0的登录验证机制越来越像Ucenter的了Uchome2.0采用cookie+ 数据库 的方式来进行用户登录验证的
1 :登录
A:用户填好登录表单之后 数据 被提交给sourcedo_login.php 处理
B:在do_login.php中下面这些 语句 接收传递来的用户名密码与cookie生效 时间
C:然后验证用户提交来的用户名以及密码的正确性,不正确则跳转并提示登录失败
注意:这里验证用户名与密码的正确性是通过uc_client在Ucenter用户中心数据库中 查询 的
D:若验证通过之后,再将获取到得用户账户信息赋给setarr 变量 数组
E:查询uchome的数据库看该用户信息是否存在于Uchome数据库中,不存在的话,则将从Ucenter中查询到的用户数据写入到uchome的member表中,存在则将member中的密码替换掉从Ucenter中查询出来的密码,存入setarr变量数组中。
F:将用户登录信息写入到Uchome的 session 表中
Session表的数据如下
Insertsession 函数 在sourcefunction_space.php中定义
其主要 功能 为a:清除session表中的某个用户的记录b:获得用户的IP以及是否使用隐身道具
c: 将setarr变量数组中的数据插入到session表中。d:更新 统计 数数据等
G:将用户名与密码加密写入cookie中
2 :验证
判断当前用户登录状态是通过sourcefunction_common.php中的checkauth函数实现的
下面就来分析这个函数
A:判断$_cookie[auth]是否存在,若不存在则不进行任何处理,并清除所有cookie
若存在则:
B:从cookie中反解出用户名跟密码信息
注意:这里说的密码以及上面说的将密码加密进cookie中的密码并不是用户的真实密码,而是经过md5双重加密并且salt处理后的密码
C:从session数据表中取出用户ID为$_SGLOBAL[supe_uid]的用户信息,若该记录存在则执行下面的操作,否则执行D操作(直接查询用户数据库)
如果取出来的密码与cookie中的密码相等,那么判定为登录成功将数据写入到$_SGLOBAL['session']数组
否则判定为登录失败,清除所有cookie
D:在C步时,若在session表中没有该用户的数据则,在用户数据表member中查询该ID得用户数据,看cookie中的密码是否与数据库中的密码一样,如果一样则登录成功,并将用户数据写入到session表中保持用户的登录状态,否则登录失败
上面无论是用session表来判定登录状态还是用member表来判定登录状态,都会影响到一个变量,那就是$_SGLOBAL['supe_uid'],在 程序 处理的时候,用户登录与否一般要用到得变量就是$_SGLOBAL['supe_uid']。
例如在space.php文件中 有个这样的语句
此语句中的checklogin函数就是检测用户是否需要登录
这个函数就是看$_SGLOBAL['supe_uid']是否存在,存在的话就以这个ID的身份去操作,不存在就说明用户登录不成功 需要用户登录才能操作。
转载于:https://my.oschina.net/mickelfeng/blog/114022
总结
以上是生活随笔为你收集整理的Uchome的登录验证机制的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: mysqldump重叠备份带来的锁表问题
- 下一篇: 【转】关于char * 与 char[]