仰望星辰工作室

fix: add unified CSRF protection

X xiaole 提交于 2026-07-31 16:27 · d2c3ed5 ·父提交 b66110c
fix: add unified CSRF protection
13 个文件变更 +181 -27 1181469655@qq.com
•MPHX/common.php +10 -0
•MPHX/function.php +112 -0
•MPHX/member.php +1 -1
•MPHX/plugin.php +21 -7
•MPHX/theme.php +16 -4
•admin/ajax.php +1 -0
•admin/api/login.php +3 -2
•app_plugins/alipay_official/bootstrap.php +3 -3
•app_plugins/epay/bootstrap.php +4 -4
•app_plugins/user_info/lib/auth.php +3 -2
•user/ajax.php +1 -0
•user/api/login.php +3 -2
•user/idcdl.php +3 -2
变更内容
diff --git a/MPHX/common.php b/MPHX/common.php
index b892b6b..372c760 100644
--- a/MPHX/common.php
+++ b/MPHX/common.php
@@ -22,6 +22,16 @@ define('SYS_KEY', 'MNBT');
 define('CC_Defender', 1); //防CC攻击开关(1为session模式)
 date_default_timezone_set("PRC");
 $date = date("Y-m-d H:i:s");
+include_once(SYSTEM_ROOT."function.php");
+$sessionSecure = mnbt_request_is_secure();
+@ini_set('session.use_strict_mode', '1');
+@ini_set('session.cookie_httponly', '1');
+@ini_set('session.cookie_samesite', 'Lax');
+if (PHP_VERSION_ID >= 70300) {
+	session_set_cookie_params(['lifetime' => 0, 'path' => '/', 'secure' => $sessionSecure, 'httponly' => true, 'samesite' => 'Lax']);
+} else {
+	session_set_cookie_params(0, '/; samesite=Lax', '', $sessionSecure, true);
+}
 session_start();
 $scriptpath=str_replace('\\','/',$_SERVER['SCRIPT_NAME']);
 $sitepath = substr($scriptpath, 0, strrpos($scriptpath, '/'));
diff --git a/MPHX/function.php b/MPHX/function.php
index caec6be..471ffa0 100644
--- a/MPHX/function.php
+++ b/MPHX/function.php
@@ -1,4 +1,116 @@
 <?php
+function mnbt_ip_in_cidr($ip, $cidr)
+{
+	if (strpos($cidr, '/') === false) return hash_equals($cidr, $ip);
+	list($network, $prefix) = explode('/', $cidr, 2);
+	$ipBin = @inet_pton($ip);
+	$networkBin = @inet_pton($network);
+	if ($ipBin === false || $networkBin === false || strlen($ipBin) !== strlen($networkBin)) return false;
+	$prefix = (int)$prefix;
+	$max = strlen($ipBin) * 8;
+	if ($prefix < 0 || $prefix > $max) return false;
+	$bytes = intdiv($prefix, 8);
+	$bits = $prefix % 8;
+	if ($bytes > 0 && substr($ipBin, 0, $bytes) !== substr($networkBin, 0, $bytes)) return false;
+	if ($bits === 0) return true;
+	$mask = (0xFF << (8 - $bits)) & 0xFF;
+	return (ord($ipBin[$bytes]) & $mask) === (ord($networkBin[$bytes]) & $mask);
+}
+function mnbt_request_from_trusted_proxy()
+{
+	$remote = isset($_SERVER['REMOTE_ADDR']) ? trim((string)$_SERVER['REMOTE_ADDR']) : '';
+	$trusted = getenv('MNBT_TRUSTED_PROXIES');
+	if ($remote === '' || $trusted === false || trim($trusted) === '') return false;
+	foreach (explode(',', $trusted) as $proxy) {
+		$proxy = trim($proxy);
+		if ($proxy !== '' && mnbt_ip_in_cidr($remote, $proxy)) return true;
+	}
+	return false;
+}
+function mnbt_request_is_secure()
+{
+	if (!empty($_SERVER['HTTPS']) && strtolower((string)$_SERVER['HTTPS']) !== 'off') return true;
+	if (isset($_SERVER['SERVER_PORT']) && (int)$_SERVER['SERVER_PORT'] === 443) return true;
+	if (!mnbt_request_from_trusted_proxy()) return false;
+	$proto = isset($_SERVER['HTTP_X_FORWARDED_PROTO']) ? strtolower(trim(explode(',', (string)$_SERVER['HTTP_X_FORWARDED_PROTO'])[0])) : '';
+	return $proto === 'https';
+}
+function mnbt_set_cookie($name, $value, $expires = 0, $httpOnly = true, $sameSite = 'Lax', $path = '/')
+{
+	$options = ['expires' => (int)$expires, 'path' => $path, 'secure' => mnbt_request_is_secure(), 'httponly' => (bool)$httpOnly, 'samesite' => $sameSite];
+	if (PHP_VERSION_ID >= 70300) return setcookie($name, $value, $options);
+	return setcookie($name, $value, (int)$expires, $path . '; samesite=' . $sameSite, '', $options['secure'], $options['httponly']);
+}
+function mnbt_set_auth_cookie($name, $value, $expires)
+{
+	$deleteAt = time() - 604800;
+	foreach (['/admin', '/user', '/app_plugins/user_info', '/app_plugins/user_info/lib'] as $legacyPath) mnbt_set_cookie($name, '', $deleteAt, true, 'Lax', $legacyPath);
+	return mnbt_set_cookie($name, $value, $expires, true, 'Lax', '/');
+}
+function mnbt_rotate_login_session()
+{
+	if (session_status() === PHP_SESSION_ACTIVE) session_regenerate_id(true);
+	mnbt_csrf_token(true);
+}
+function mnbt_csrf_token($rotate = false)
+{
+	if ($rotate || empty($_SESSION['mnbt_csrf_token']) || !is_string($_SESSION['mnbt_csrf_token'])) {
+		try { $_SESSION['mnbt_csrf_token'] = bin2hex(random_bytes(32)); } catch (Throwable $e) { $_SESSION['mnbt_csrf_token'] = hash('sha256', uniqid((string)mt_rand(), true) . session_id()); }
+	}
+	$token = $_SESSION['mnbt_csrf_token'];
+	if (!headers_sent() && (!isset($_COOKIE['MNBT_CSRF_TOKEN']) || !hash_equals((string)$_COOKIE['MNBT_CSRF_TOKEN'], $token))) {
+		mnbt_set_cookie('MNBT_CSRF_TOKEN', $token, 0, false, 'Lax');
+		$_COOKIE['MNBT_CSRF_TOKEN'] = $token;
+	}
+	return $token;
+}
+function mnbt_csrf_request_token()
+{
+	foreach (['HTTP_X_CSRF_TOKEN', 'HTTP_X_XSRF_TOKEN'] as $header) if (!empty($_SERVER[$header])) return (string)$_SERVER[$header];
+	foreach (['_csrf', 'csrf_token', '_token'] as $key) if (isset($_POST[$key]) && is_string($_POST[$key])) return $_POST[$key];
+	return '';
+}
+function mnbt_csrf_verify($token = null)
+{
+	$expected = isset($_SESSION['mnbt_csrf_token']) && is_string($_SESSION['mnbt_csrf_token']) ? $_SESSION['mnbt_csrf_token'] : '';
+	$actual = $token === null ? mnbt_csrf_request_token() : (string)$token;
+	return $expected !== '' && $actual !== '' && hash_equals($expected, $actual);
+}
+function mnbt_csrf_is_safe_method($method = null)
+{
+	$method = strtoupper($method === null ? ($_SERVER['REQUEST_METHOD'] ?? 'GET') : (string)$method);
+	return in_array($method, ['GET', 'HEAD', 'OPTIONS'], true);
+}
+function mnbt_csrf_fail()
+{
+	http_response_code(419);
+	@header('Content-Type: application/json; charset=UTF-8');
+	if (function_exists('json_exit')) json_exit('CSRF 验证失败,请刷新页面后重试');
+	exit('{"code":"CSRF 验证失败,请刷新页面后重试"}');
+}
+function mnbt_csrf_validate_request($exempt = false)
+{
+	if ($exempt || mnbt_csrf_is_safe_method()) return true;
+	if (!mnbt_csrf_verify()) mnbt_csrf_fail();
+	return true;
+}
+function mnbt_csrf_field()
+{
+	return '<input type="hidden" name="_csrf" value="' . htmlspecialchars(mnbt_csrf_token(), ENT_QUOTES, 'UTF-8') . '">';
+}
+function mnbt_csrf_head()
+{
+	$token = htmlspecialchars(mnbt_csrf_token(), ENT_QUOTES, 'UTF-8');
+	return '<meta name="csrf-token" content="' . $token . '"><script>(function(){var m=document.querySelector(\'meta[name="csrf-token"]\');if(!m)return;var t=m.content,s=function(u){try{return new URL(u,location.href).origin===location.origin}catch(e){return false}},v=function(x){return !/^(GET|HEAD|OPTIONS)$/i.test(x||"GET")};document.addEventListener("submit",function(e){var f=e.target;if(f&&v(f.method)&&s(f.action||location.href)&&!f.querySelector(\'input[name="_csrf"]\')){var i=document.createElement("input");i.type="hidden";i.name="_csrf";i.value=t;f.appendChild(i)}},true);var o=XMLHttpRequest.prototype.open,a=XMLHttpRequest.prototype.send;XMLHttpRequest.prototype.open=function(method,url){this.__mnbtCsrf=v(method)&&s(url);return o.apply(this,arguments)};XMLHttpRequest.prototype.send=function(){if(this.__mnbtCsrf)this.setRequestHeader("X-CSRF-Token",t);return a.apply(this,arguments)};if(window.fetch){var f=window.fetch;window.fetch=function(input,init){init=init||{};var u=typeof input==="string"?input:input.url,method=init.method||(typeof input!=="string"&&input.method)||"GET";if(v(method)&&s(u)){init.headers=new Headers(init.headers||(typeof input!=="string"?input.headers:void 0));init.headers.set("X-CSRF-Token",t)}return f.call(this,input,init)}}if(window.jQuery)jQuery.ajaxPrefilter(function(options,original,xhr){if(v(options.type)&&s(options.url))xhr.setRequestHeader("X-CSRF-Token",t)})})();</script>';
+}
+function mnbt_csrf_inject_html($html)
+{
+	if (!is_string($html) || $html === '') return $html;
+	if (!preg_match('/\A\s*(?:<!doctype\s+html\b[^>]*>\s*)?<html\b[^>]*>/i', $html)) return $html;
+	if (!preg_match('/<head\b[^>]*>[\s\S]*<\/head\s*>/i', $html)) return $html;
+	if (preg_match('/<meta\b[^>]*\bname\s*=\s*(["\'])csrf-token\1/i', $html)) return $html;
+	return preg_replace('/<\/head\s*>/i', mnbt_csrf_head() . '</head>', $html, 1);
+}
 function curl_get($url)
 {
 $ch=curl_init($url);
diff --git a/MPHX/member.php b/MPHX/member.php
index 9226f4f..a35d20c 100644
--- a/MPHX/member.php
+++ b/MPHX/member.php
@@ -28,7 +28,7 @@ if($conf['kzmbqk']=='false'){sysmsg('控制面板已经被关闭详细请联系
 		$zjid=$yhc['btid'];
 		$ssbt=$yhc['ssbt'];                     
 		if($conf['kzmbqk']=='false'){sysmsg('控制面板已经关闭!详细请联系站长QQ'.$conf['qqh']);}
-		if(strtotime($date)-strtotime($yhc['datae'])>0 && $yhc['datae']!='0000-00-00'){setcookie("user_token", "", time() - 604800);sysmsg('您已到期!已经帮您自动退出登陆!刷新即可重新登录');}
+		if(strtotime($date)-strtotime($yhc['datae'])>0 && $yhc['datae']!='0000-00-00'){mnbt_set_auth_cookie("user_token", "", time() - 604800);sysmsg('您已到期!已经帮您自动退出登陆!刷新即可重新登录');}
 		if($yhc['qk']=='false'){
 			sysmsg('您已被禁止登陆!详细请联系站长QQ'.$conf['qqh']);
 		}
diff --git a/MPHX/plugin.php b/MPHX/plugin.php
index 5354cdf..37ff704 100644
--- a/MPHX/plugin.php
+++ b/MPHX/plugin.php
@@ -722,6 +722,7 @@ function mnbt_plugin_dispatch_ajax($side, $egn)
 	if (!mnbt_plugin_auth_check($item['auth'] ?? null)) {
 		mnbt_plugin_auth_fail($item['auth'] ?? null);
 	}
+	mnbt_csrf_validate_request();
 	$prev = $GLOBALS['mnbt_plugin_current'];
 	$GLOBALS['mnbt_plugin_current'] = $item['plugin'];
 	try {
@@ -1040,6 +1041,7 @@ function mnbt_plugin_find_page($side, $plugin, $page)
 
 function mnbt_plugin_render_page($side, $plugin, $page)
 {
+	mnbt_csrf_validate_request();
 	$side = $side === 'admin' ? 'admin' : 'user';
 	$info = mnbt_plugin_find_page($side, $plugin, $page);
 	if (!$info) {
@@ -1062,8 +1064,14 @@ function mnbt_plugin_render_page($side, $plugin, $page)
 	$GLOBALS['mnbt_plugin_current'] = $plugin;
 	extract($GLOBALS, EXTR_SKIP);
 	$title = $info['title'] ?: ($plugin . ' / ' . $page);
-	include $realFile;
-	$GLOBALS['mnbt_plugin_current'] = $prev;
+	$bufferLevel = ob_get_level();
+	ob_start('mnbt_csrf_inject_html');
+	try {
+		include $realFile;
+	} finally {
+		while (ob_get_level() > $bufferLevel) ob_end_flush();
+		$GLOBALS['mnbt_plugin_current'] = $prev;
+	}
 	return true;
 }
 
@@ -1259,7 +1267,7 @@ function mnbt_plugin_dispatch_home()
  * @param string|callable|null $auth  鉴权要求:null/'none'=无验证, 'admin'=管理员, 'user'=用户, 回调函数=自定义验证
  * @return bool
  */
-function mnbt_register_route($method, $path, $callback, $priority = 10, $auth = null)
+function mnbt_register_route($method, $path, $callback, $priority = 10, $auth = null, $csrfExempt = false)
 {
 	if (!is_callable($callback)) {
 		return false;
@@ -1290,6 +1298,7 @@ function mnbt_register_route($method, $path, $callback, $priority = 10, $auth =
 		'cb' => $callback,
 		'plugin' => $GLOBALS['mnbt_plugin_current'],
 		'auth' => $auth,
+		'csrf_exempt' => (bool)$csrfExempt,
 	];
 	return true;
 }
@@ -1324,6 +1333,7 @@ function mnbt_plugin_dispatch_route()
 			if (!mnbt_plugin_auth_check($item['auth'] ?? null)) {
 				mnbt_plugin_auth_fail($item['auth'] ?? null);
 			}
+			mnbt_csrf_validate_request($item['csrf_exempt'] ?? false);
 			$prev = $GLOBALS['mnbt_plugin_current'];
 			$GLOBALS['mnbt_plugin_current'] = $item['plugin'];
 			$ctx = [
@@ -1333,9 +1343,17 @@ function mnbt_plugin_dispatch_route()
 				'plugin' => $item['plugin'],
 				'route' => $item['path'],
 			];
+			$bufferLevel = ob_get_level();
+			ob_start('mnbt_csrf_inject_html');
 			try {
 				$result = call_user_func($item['cb'], $params, $ctx);
+				if (is_string($result) && $result !== '') {
+					if (!headers_sent()) header('Content-Type: text/html; charset=UTF-8');
+					echo $result;
+				}
+				while (ob_get_level() > $bufferLevel) ob_end_flush();
 			} catch (Throwable $e) {
+				while (ob_get_level() > $bufferLevel) ob_end_clean();
 				error_log('[MNBT plugin] route ' . $item['method'] . ' ' . $item['path'] . ' @' . ($item['plugin'] ?? '?') . ': ' . $e->getMessage());
 				$GLOBALS['mnbt_plugin_current'] = $prev;
 				continue;
@@ -1344,10 +1362,6 @@ function mnbt_plugin_dispatch_route()
 			if ($result === false) {
 				continue;
 			}
-			if (is_string($result) && $result !== '' && !headers_sent()) {
-				header('Content-Type: text/html; charset=UTF-8');
-				echo $result;
-			}
 			exit;
 		}
 	}
diff --git a/MPHX/theme.php b/MPHX/theme.php
index f241eb6..ce8d084 100644
--- a/MPHX/theme.php
+++ b/MPHX/theme.php
@@ -259,7 +259,7 @@ function mnbt_theme_include($view, array $vars = [], $scope = 'user')
 
 	// 模式 1:完全接管
 	if (is_string($override)) {
-		echo $override;
+		echo mnbt_csrf_inject_html($override);
 		return true;
 	}
 
@@ -291,7 +291,13 @@ function mnbt_theme_include($view, array $vars = [], $scope = 'user')
 	if ($vars) {
 		extract($vars, EXTR_OVERWRITE);
 	}
-	include $path;
+	$bufferLevel = ob_get_level();
+	ob_start('mnbt_csrf_inject_html');
+	try {
+		include $path;
+	} finally {
+		while (ob_get_level() > $bufferLevel) ob_end_flush();
+	}
 
 	// 模式 2:包裹模式 - 输出 after
 	if ($after !== '') {
@@ -327,7 +333,7 @@ function mnbt_render($view, array $vars = [], $exit = true, $scope = 'user')
 
 	// 模式 1:完全接管
 	if (is_string($override)) {
-		echo $override;
+		echo mnbt_csrf_inject_html($override);
 		if ($exit) {
 			exit;
 		}
@@ -362,7 +368,13 @@ function mnbt_render($view, array $vars = [], $exit = true, $scope = 'user')
 	if ($vars) {
 		extract($vars, EXTR_OVERWRITE);
 	}
-	include $path;
+	$bufferLevel = ob_get_level();
+	ob_start('mnbt_csrf_inject_html');
+	try {
+		include $path;
+	} finally {
+		while (ob_get_level() > $bufferLevel) ob_end_flush();
+	}
 
 	// 模式 2:after
 	if ($after !== '') {
diff --git a/admin/ajax.php b/admin/ajax.php
index 6e7a68b..bcbe482 100644
--- a/admin/ajax.php
+++ b/admin/ajax.php
@@ -1,6 +1,7 @@
 <?php
 include("../MPHX/common.php");
 @header('Content-Type: text/html; charset=UTF-8');
+mnbt_csrf_validate_request();
 $egn=$_POST['gn'];
 	if($islogin==1 || $egn=='login') {
 } else json_exit('请登陆');
diff --git a/admin/api/login.php b/admin/api/login.php
index 6a2f17e..2009bd0 100644
--- a/admin/api/login.php
+++ b/admin/api/login.php
@@ -12,7 +12,8 @@ if($egn=='login') {
 			unset($_SESSION['authcode']);
 			$session=md5($user.$pass.$password_hash);
 			$token=authcode("{$user}\t{$session}", 'ENCODE', SYS_KEY);
-			setcookie("admin_token", $token, time() + 604800);
+			mnbt_rotate_login_session();
+			mnbt_set_auth_cookie("admin_token", $token, time() + 604800);
 			@header('Content-Type: text/html; charset=UTF-8');
 			json_exit('登陆成功');
 		} else {
@@ -21,7 +22,7 @@ if($egn=='login') {
 			json_exit('用户名或密码错误');
 		}
 	} elseif(isset($_POST['logout'])) {
-		setcookie("admin_token", "", time() - 604800);
+		mnbt_set_auth_cookie("admin_token", "", time() - 604800);
 		@header('Content-Type: text/html; charset=UTF-8');
 		json_exit('您已成功注销本次登陆');
 	}
diff --git a/app_plugins/alipay_official/bootstrap.php b/app_plugins/alipay_official/bootstrap.php
index db65a4e..ce5634f 100644
--- a/app_plugins/alipay_official/bootstrap.php
+++ b/app_plugins/alipay_official/bootstrap.php
@@ -197,7 +197,7 @@ HTML;
 // ============================================================
 //  回调路由:/pay/alipay_official/notify(异步通知)
 // ============================================================
-mnbt_register_route('*', '/pay/alipay_official/notify', function ($params, $ctx) {
+mnbt_register_route('POST', '/pay/alipay_official/notify', function ($params, $ctx) {
 	@header('Content-Type: text/plain; charset=UTF-8');
 	$svc = alipay_official_get_service();
 	if (!$svc) {
@@ -228,12 +228,12 @@ mnbt_register_route('*', '/pay/alipay_official/notify', function ($params, $ctx)
 	} else {
 		echo 'fail';
 	}
-});
+}, 10, null, true);
 
 // ============================================================
 //  回调路由:/pay/alipay_official/return(同步返回)
 // ============================================================
-mnbt_register_route('*', '/pay/alipay_official/return', function ($params, $ctx) {
+mnbt_register_route('GET', '/pay/alipay_official/return', function ($params, $ctx) {
 	$base = isset($ctx['base']) ? $ctx['base'] : '';
 	// 同步返回仅作展示,订单最终状态以异步通知为准
 	@header('Location: ' . $base . '/user');
diff --git a/app_plugins/epay/bootstrap.php b/app_plugins/epay/bootstrap.php
index 09b9645..dfa2120 100644
--- a/app_plugins/epay/bootstrap.php
+++ b/app_plugins/epay/bootstrap.php
@@ -108,14 +108,14 @@ mnbt_register_payment('epay', [
 // ============================================================
 //  注册回调路由:/pay/epay/notify(异步通知)
 // ============================================================
-mnbt_register_route('*', '/pay/epay/notify', function ($params, $ctx) {
+mnbt_register_route('POST', '/pay/epay/notify', function ($params, $ctx) {
 	@header('Content-Type: text/plain; charset=UTF-8');
 	if (!epay_is_configured()) {
 		echo 'fail';
 		return;
 	}
 	$c = epay_get_config();
-	$data = $_SERVER['REQUEST_METHOD'] === 'POST' ? $_POST : $_GET;
+	$data = $_POST;
 	if (empty($data) || empty($data['sign'])) {
 		mnbt_pay_log('易支付异步通知无数据或缺少 sign', '回调异常', $data['out_trade_no'] ?? '');
 		echo 'fail';
@@ -140,12 +140,12 @@ mnbt_register_route('*', '/pay/epay/notify', function ($params, $ctx) {
 	} else {
 		echo 'fail';
 	}
-});
+}, 10, null, true);
 
 // ============================================================
 //  注册回调路由:/pay/epay/return(同步返回)
 // ============================================================
-mnbt_register_route('*', '/pay/epay/return', function ($params, $ctx) {
+mnbt_register_route('GET', '/pay/epay/return', function ($params, $ctx) {
 	if (!epay_is_configured()) {
 		@header('Location: ./');
 		return;
diff --git a/app_plugins/user_info/lib/auth.php b/app_plugins/user_info/lib/auth.php
index 34314a9..7a3f50c 100644
--- a/app_plugins/user_info/lib/auth.php
+++ b/app_plugins/user_info/lib/auth.php
@@ -51,7 +51,8 @@ function user_info_auth_login($user_id, $password_hash)
 {
 	$session_hash = md5($user_id . $password_hash . SYS_KEY);
 	$token = authcode($user_id . "\t" . $session_hash, 'ENCODE', SYS_KEY);
-	setcookie('account_token', $token, time() + 604800, '/'); // 7 天
+	mnbt_rotate_login_session();
+	mnbt_set_auth_cookie('account_token', $token, time() + 604800);
 }
 
 /**
@@ -59,7 +60,7 @@ function user_info_auth_login($user_id, $password_hash)
  */
 function user_info_auth_logout()
 {
-	setcookie('account_token', '', time() - 604800, '/');
+	mnbt_set_auth_cookie('account_token', '', time() - 604800);
 }
 
 /**
diff --git a/user/ajax.php b/user/ajax.php
index 9d36b1a..370a98e 100644
--- a/user/ajax.php
+++ b/user/ajax.php
@@ -5,6 +5,7 @@
 */
 include("../MPHX/common.php");
 @header('Content-Type: text/html; charset=UTF-8');
+mnbt_csrf_validate_request();
 $egn=$_POST['gn'] ?? '';
 if($islogins==1 || $egn=='login') {
 } else exit('{"code":"请登陆"}');
diff --git a/user/api/login.php b/user/api/login.php
index ef1dfc2..f296bcb 100644
--- a/user/api/login.php
+++ b/user/api/login.php
@@ -14,7 +14,8 @@ if($egn=='login') {
 			unset($_SESSION['authcode']);
 			$session=md5($user.$pass.$password_hash);
 			$token=authcode("{$user}\t{$session}", 'ENCODE', SYS_KEY);
-			setcookie("user_token", $token, time() + 604800);
+			mnbt_rotate_login_session();
+			mnbt_set_auth_cookie("user_token", $token, time() + 604800);
 			@header('Content-Type: text/html; charset=UTF-8');
 			json_exit('登陆成功');
 		} else {
@@ -22,7 +23,7 @@ if($egn=='login') {
 			exit('{"code":"用户不存在或密码错误!"}');
 		}
 	} elseif(isset($_POST['logout'])) {
-		setcookie("user_token", "", time() - 604800);
+		mnbt_set_auth_cookie("user_token", "", time() - 604800);
 		@header('Content-Type: text/html; charset=UTF-8');
 		exit('{"code":"您已成功注销本次登陆!"}');
 	}
diff --git a/user/idcdl.php b/user/idcdl.php
index 3622364..0be29d4 100644
--- a/user/idcdl.php
+++ b/user/idcdl.php
@@ -1,6 +1,6 @@
 <?php
-setcookie("user_token", "", time() - 604800);		//先注销一次上次的登陆	
 include("../MPHX/common.php");
+mnbt_set_auth_cookie("user_token", "", time() - 604800);
 @header('Content-Type: text/html; charset=UTF-8');
 $egn=$_REQUEST['gn'];
 if($conf['yzme']=='true')exit("<script language='javascript'>alert('后台已经开启控制面板验证码登陆无法进行一键登录!');window.location.href='./login.php';</script>");
@@ -16,7 +16,8 @@ if(isset($_REQUEST['username']) && isset($_REQUEST['password'])){
 		unset($_SESSION['authcode']);
 		$session=md5($user.$pass.$password_hash);
 		$token=authcode("{$user}\t{$session}", 'ENCODE', SYS_KEY);
-		setcookie("user_token", $token, time() + 604800);
+		mnbt_rotate_login_session();
+		mnbt_set_auth_cookie("user_token", $token, time() + 604800);
 		@header('Content-Type: text/html; charset=UTF-8');
         header("Location:index.php");
 }else{