浏览代码

单账户允许同时多处登录(原来只允许同一时间单机登录)

star7th 9 年之前
父节点
当前提交
32c3a3c947

+ 25 - 1
Application/Common/Common/function.php

@@ -119,4 +119,28 @@ function clear_runtime($path = RUNTIME_PATH){
     //关闭目录句柄,否则出Permission denied  
     //关闭目录句柄,否则出Permission denied  
     closedir($fh);    
     closedir($fh);    
     return true;  
     return true;  
-}  
+}
+
+//获取ip
+function getIPaddress(){
+    $IPaddress='';
+    if (isset($_SERVER)){
+        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
+            $IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
+        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
+            $IPaddress = $_SERVER["HTTP_CLIENT_IP"];
+        } else {
+            $IPaddress = $_SERVER["REMOTE_ADDR"];
+        }
+    } else {
+        if (getenv("HTTP_X_FORWARDED_FOR")){
+            $IPaddress = getenv("HTTP_X_FORWARDED_FOR");
+        } else if (getenv("HTTP_CLIENT_IP")) {
+            $IPaddress = getenv("HTTP_CLIENT_IP");
+        } else {
+            $IPaddress = getenv("REMOTE_ADDR");
+        }
+    }
+    return $IPaddress;
+
+}

+ 13 - 1
Application/Common/Conf/config.php

@@ -25,5 +25,17 @@ return array(
     'URL_CASE_INSENSITIVE'=>true,
     'URL_CASE_INSENSITIVE'=>true,
     'SHOW_ERROR_MSG'        =>  true,    // 显示错误信息,这样在部署模式下也能显示错误
     'SHOW_ERROR_MSG'        =>  true,    // 显示错误信息,这样在部署模式下也能显示错误
     'STATS_CODE' =>'',  //可选,统计代码
     'STATS_CODE' =>'',  //可选,统计代码
-
+    //上传文件到七牛的配置
+    'UPLOAD_SITEIMG_QINIU' => array(
+                    'maxSize' => 5 * 1024 * 1024,//文件大小
+                    'rootPath' => './',
+                    'saveName' => array ('uniqid', ''),
+                    'driver' => 'Qiniu',
+                    'driverConfig' => array (
+                            'secrectKey' => '', 
+                            'accessKey' => '',
+                            'domain' => '',
+                            'bucket' => '', 
+                        )
+                    ),
 );
 );

+ 1 - 1
Application/Home/Conf/config.php

@@ -2,7 +2,7 @@
 return array(
 return array(
 	//'配置项'=>'配置值'
 	//'配置项'=>'配置值'
     'LANG_SWITCH_ON' => true,   // 开启语言包功能
     'LANG_SWITCH_ON' => true,   // 开启语言包功能
-    'LANG_AUTO_DETECT' => true, // 自动侦测语言 开启多语言功能后有效
+    'LANG_AUTO_DETECT' => false, // 自动侦测语言 开启多语言功能后有效
     'DEFAULT_LANG' => 'zh-cn', // 默认语言
     'DEFAULT_LANG' => 'zh-cn', // 默认语言
     'LANG_LIST'        => 'zh-cn,en-us', // 允许切换的语言列表 用逗号分隔
     'LANG_LIST'        => 'zh-cn,en-us', // 允许切换的语言列表 用逗号分隔
     'VAR_LANGUAGE'     => 'l', // 默认语言切换变量
     'VAR_LANGUAGE'     => 'l', // 默认语言切换变量

+ 4 - 4
Application/Home/Controller/BaseController.class.php

@@ -13,12 +13,12 @@ class BaseController extends Controller {
 		if ( ! session("login_user")) {
 		if ( ! session("login_user")) {
 			$cookie_token = cookie('cookie_token');
 			$cookie_token = cookie('cookie_token');
 			if ($cookie_token) {
 			if ($cookie_token) {
-				$ret = D("User")->where("cookie_token = '%s' ",array($cookie_token))->find();
-				if ($ret && $ret['cookie_token_expire'] > time() ) {
-					$login_user = $ret ;
+				$ret = D("UserToken")->getToken($cookie_token);
+				if ($ret && $ret['token_expire'] > time() ) {
+					$login_user = D("User")->where("uid = $ret[uid]")->find();
+					unset($ret['password']);
 					session("login_user" , $login_user);
 					session("login_user" , $login_user);
 					return $login_user ;
 					return $login_user ;
-
 				}
 				}
 			}
 			}
 			if ($redirect) {
 			if ($redirect) {

+ 23 - 1
Application/Home/Controller/UpdateController.class.php

@@ -11,7 +11,7 @@ class UpdateController extends BaseController {
         elseif (strtolower(C("DB_TYPE")) == 'sqlite' ) {
         elseif (strtolower(C("DB_TYPE")) == 'sqlite' ) {
             $this->sqlite();
             $this->sqlite();
         }
         }
-    	
+    	clear_runtime();
     }
     }
     //升级mysql数据库  
     //升级mysql数据库  
     public function mysql(){
     public function mysql(){
@@ -184,6 +184,18 @@ class UpdateController extends BaseController {
             }
             }
         }
         }
 
 
+        $sql = "CREATE TABLE IF NOT EXISTS `user_token` (
+        `id` int(10) NOT NULL AUTO_INCREMENT,
+        `uid` int(10) NOT NULL DEFAULT '0',
+        `token` varchar(200) NOT NULL DEFAULT '',
+        `token_expire` int(11) NOT NULL DEFAULT '0' ,
+        `ip` varchar(200) NOT NULL DEFAULT '',
+        `addtime` int(11) NOT NULL DEFAULT '0',
+        PRIMARY KEY (`id`),
+        KEY `token` (`token`)
+        ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1 ";
+        D("User")->execute($sql);
+
         echo "OK!";
         echo "OK!";
     }
     }
 
 
@@ -232,6 +244,16 @@ class UpdateController extends BaseController {
                 D("item")->execute($sql);
                 D("item")->execute($sql);
             }
             }
         }
         }
+        //创建user_token表
+        $sql = "CREATE TABLE IF NOT EXISTS `user_token` (
+        `id`  INTEGER PRIMARY KEY ,
+        `uid` int(10) NOT NULL DEFAULT '0',
+        `token` CHAR(200) NOT NULL DEFAULT '',
+        `token_expire` int(11) NOT NULL DEFAULT '0' ,
+        `ip` CHAR(200) NOT NULL DEFAULT '',
+        `addtime` int(11) NOT NULL DEFAULT '0'
+        )";
+        D("UserToken")->execute($sql);
 
 
         echo 'OK!';
         echo 'OK!';
     }
     }

+ 8 - 12
Application/Home/Controller/UserController.class.php

@@ -48,9 +48,10 @@ class UserController extends BaseController {
 			//如果有cookie记录,则自动登录
 			//如果有cookie记录,则自动登录
 			$cookie_token = cookie('cookie_token');
 			$cookie_token = cookie('cookie_token');
 			if ($cookie_token) {
 			if ($cookie_token) {
-				$ret = D("User")->where("cookie_token = '%s' ",array($cookie_token))->find();
-				if ($ret && $ret['cookie_token_expire'] > time() ) {
-					$login_user = $ret ;
+				$ret = D("UserToken")->getToken($cookie_token);
+				if ($ret && $ret['token_expire'] > time() ) {
+					$login_user = D("User")->where("uid = $ret[uid]")->find();
+					unset($ret['password']);
 					session("login_user" , $login_user);
 					session("login_user" , $login_user);
 					$this->message(L('auto_login_succeeded'),U('Home/Item/index'));
 					$this->message(L('auto_login_succeeded'),U('Home/Item/index'));
 					exit();
 					exit();
@@ -67,12 +68,9 @@ class UserController extends BaseController {
 		  	$ret = D("User")->checkLogin($username,$password);
 		  	$ret = D("User")->checkLogin($username,$password);
 		    if ($ret) {
 		    if ($ret) {
 		      session("login_user" , $ret );
 		      session("login_user" , $ret );
-		      $cookie_token = md5(time().rand().'efeffthdh');
-		      $cookie_token_expire = time() + 60*60*24*90 ;
-	          cookie('cookie_token',$cookie_token,60*60*24*90);
-		      D("User")->where(" uid = '$ret[uid]' ")->save(array("last_login_time"=>time(),"cookie_token"=>$cookie_token,"cookie_token_expire"=>$cookie_token_expire));
+		      $token = D("UserToken")->createToken($ret['uid']);
+	          cookie('cookie_token',$token,60*60*24*90);//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
 		      unset($ret['password']);
 		      unset($ret['password']);
-
 	          $this->message(L('login_succeeded'),U('Home/Item/index'));		        
 	          $this->message(L('login_succeeded'),U('Home/Item/index'));		        
 		    }else{
 		    }else{
 		      $this->message(L('username_or_password_incorrect'));
 		      $this->message(L('username_or_password_incorrect'));
@@ -82,10 +80,8 @@ class UserController extends BaseController {
 			    $ret = D("User")->checkLogin($username,$password);
 			    $ret = D("User")->checkLogin($username,$password);
 			    if ($ret) {
 			    if ($ret) {
 			      session("login_user" , $ret );
 			      session("login_user" , $ret );
-			      $cookie_token = md5(time().rand().'efeffthdh');
-			      $cookie_token_expire = time() + 60*60*24*90 ;
-		          cookie('cookie_token',$cookie_token,60*60*24*90);
-			      D("User")->where(" uid = '$ret[uid]' ")->save(array("last_login_time"=>time(),"cookie_token"=>$cookie_token,"cookie_token_expire"=>$cookie_token_expire));
+		      	  $token = D("UserToken")->createToken($ret['uid']);
+          		  cookie('cookie_token',$token,60*60*24*90);//此处由服务端控制token是否过期,所以cookies过期时间设置多久都无所谓
 			      unset($ret['password']);
 			      unset($ret['password']);
 
 
 		          $this->message(L('login_succeeded'),U('Home/Item/index'));		        
 		          $this->message(L('login_succeeded'),U('Home/Item/index'));		        

+ 30 - 0
Application/Home/Model/UserTokenModel.class.php

@@ -0,0 +1,30 @@
+<?php
+namespace Home\Model;
+use Home\Model\BaseModel;
+/**
+ * 
+ * @author star7th      
+ */
+class UserTokenModel extends BaseModel {
+
+	public function createToken($uid,$token_expire = 0 ){
+		$token_expire = $token_expire > 0  ? (time() + $token_expire ) : (time() + 60*60*24*90 );
+		$token = md5(md5($uid.$token_expire.time().rand()."showdoc")."rdgtrd12367hghf54t");
+		$data['uid'] = $uid ;
+		$data['token'] = $token ;
+		$data['token_expire'] = $token_expire ;
+		$data['ip'] = getIPaddress() ;
+		$data['addtime'] = time() ;
+		$ret = $this->add($data);
+		if ($ret) {
+			//删除过期的token 
+			$this->where( "token_expire < ".time() )->delete();
+			return $token ;
+		}
+		return false ;
+	}
+
+	public function getToken($token){
+		return $this->where("token='$token'")->find();
+	}
+}

二进制
Sqlite/showdoc.db.php


+ 12 - 0
install/ajax.php

@@ -266,6 +266,18 @@ function import_mysql($con){
 	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='页面历史表' AUTO_INCREMENT=1 ";
 	) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='页面历史表' AUTO_INCREMENT=1 ";
 	mysqli_query($con, $sql);
 	mysqli_query($con, $sql);
 
 
+    $sql = "CREATE TABLE IF NOT EXISTS `user_token` (
+    `id` int(10) NOT NULL AUTO_INCREMENT,
+    `uid` int(10) NOT NULL DEFAULT '0',
+    `token` varchar(200) NOT NULL DEFAULT '',
+    `token_expire` int(11) NOT NULL DEFAULT '0' ,
+    `ip` varchar(200) NOT NULL DEFAULT '',
+    `addtime` int(11) NOT NULL DEFAULT '0',
+    PRIMARY KEY (`id`),
+    KEY `token` (`token`)
+    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='' AUTO_INCREMENT=1 ";
+    mysqli_query($con, $sql);
+
 	//创建项目user表
 	//创建项目user表
 	$sql = "CREATE TABLE IF NOT EXISTS `user` (
 	$sql = "CREATE TABLE IF NOT EXISTS `user` (
 	`uid` int(10) NOT NULL AUTO_INCREMENT,
 	`uid` int(10) NOT NULL AUTO_INCREMENT,