thinkphp通行证服务,验证登录,注销登录
<?php /** * 通行证服务 */ class PassportService extends Service { /** * 验证用户或者管理员是否已登录 * @return boolean */ public function isLogged($admin=false){ //$_cookie_user=cookie('LOGGED_USER'); $_session_username = $_SESSION[C('COOKIE_PREFIX').'username']; $_session_uid = $_SESSION[C('COOKIE_PREFIX').'uid']; $_session_isadmin = intval($_SESSION[C('COOKIE_PREFIX').'isadmin']); // 验证本地系统登录 if ($_session_user || $_session_uid){ if(!$admin){ return true; } elseif($admin&&$_session_isadmin==1){ return true; } else{ return false; } }else{ return false; } }
//验证登录提交参数 public function doLogin($username,$password){ if(empty($username) || empty($password)){ return false; } //$username=addcslashes($username); $password=md5($password); $map=array(); $map['username']=$username; $map['password']=$password; $uid=M("user")->where($map)->getField('uid'); // $password=md5($password); //$uid=M("user")->where("username='{$username}' and password='{$password}'")->getField('uid'); if(!$uid){ return false; } else{ return $this->online($uid); } }
//写入cookie和session public function online($uid){ $uid=intval($uid); if($uid<=0){ return false; } else{ $user=M("user")->where("uid={$uid}")->field('uid,username,isadmin')->find(); if(!$user){ return false; }else{ header("P3P: CP=."); cookie('LOGGED_USER',$user['username']); $_SESSION[C('COOKIE_PREFIX').'username'] = $user['username']; $_SESSION[C('COOKIE_PREFIX').'uid'] = $user['uid']; $_SESSION[C('COOKIE_PREFIX').'isadmin'] = $user['isadmin']; $this->recordLogin($user['uid']); return true; } } } /** * 注销本地登录 * @return void */ public function logoutLocal() { //注销session unset($_SESSION[C('COOKIE_PREFIX').'username']); unset($_SESSION[C('COOKIE_PREFIX').'uid']); unset($_SESSION[C('COOKIE_PREFIX').'isadmin']); //注销cookie cookie('LOGGED_USER',NULL); } /** * 登录记录 * * @param int $uid 用户ID */ public function recordLogin($uid) { $login['uid'] = $uid; $login['ip'] = get_client_ip(); $login['place'] = convert_ip($login['ip']); $login['ctime'] = time(); M('login_record')->add($login); } /* 后台管理相关方法 */ // 运行服务,系统服务自动运行 public function run(){ return; } //启动服务,未编码 public function _start(){ return true; } //停止服务,未编码 public function _stop(){ return true; } //卸载服务,未编码 public function _install(){ return true; } //卸载服务,未编码 public function _uninstall(){ return true; } } ?>
posted on 2014-12-25 09:05 walter371 阅读(1177) 评论(0) 编辑 收藏 举报