세션 테이블을 뒤집어 엎는 중.

session 디렉토리 제거. 서버의 자체 session으로 관리함. 필요하다면 자동로그인을 추가함.

login_process.php를 이용하지 않고 자체적으로 세션을 관리하도록 처리 중.
This commit is contained in:
2018-02-03 04:39:22 +09:00
parent e536e2d4ca
commit ecac4b6edb
15 changed files with 96 additions and 78 deletions
+1 -21
View File
@@ -3,11 +3,7 @@ require(__dir__.'/../vendor/autoload.php');
class _Session {
public function __construct() {
$sessionPath = ROOT.W.D_SESSION;
session_save_path($sessionPath);
session_cache_limiter('nocache, must_revalidate');
session_cache_expire(10080); // 60*24*7분
session_set_cookie_params(604800, '/');
// 세션 변수의 등록
session_start();
@@ -20,21 +16,6 @@ class _Session {
}
}
public static function TrashSession() {
$sessionPath = ROOT.W.D_SESSION;
if($dir = @opendir($sessionPath)) {
while($file = @readdir($dir)) {
if(!strstr($file, 'sess_')) continue;
if(strpos($file, 'sess_') != 0) continue;
if(!$atime = @fileatime("{$sessionPath}/{$file}")) continue;
if(time() > $atime+604800) { // 3600*24*7초
@unlink("{$sessionPath}/{$file}");
}
}
closedir($dir);
}
}
public function Set($key, $val) {
$_SESSION[$key] = $val;
}
@@ -50,8 +31,7 @@ class _Session {
}
public function Logout() {
$_SESSION['noMember'] = 0;
session_destroy();
unset($_SESSION['noMember']);
}
public function IsLoggedIn() {
+1 -1
View File
@@ -84,5 +84,5 @@ function Entrance_AdminClosedLogin(serverDir) {
}
function Entrance_AdminOpen119(serverDir) {
Open(serverDir+W+'119'+PHP);
Open(serverDir+W+'_119'+PHP);
}
+2 -2
View File
@@ -43,7 +43,7 @@ for($i=0; $i < $_serverCount; $i++) {
else { $state = "&lt;{$nation['CNT']}국 경쟁중&gt;"; }
$state = "<font size=2>{$state}</font>";
if($me) { $site = "login_process.php"; }
if($me) { $site = "index.php"; }
elseif($game['NPCMODE'] == 1) { $site = "selection.php"; }
else { $site = "join.php"; }
@@ -76,7 +76,7 @@ for($i=0; $i < $_serverCount; $i++) {
$character = "
<div class='Entrance_ServerListCharacter'>{$picture}</div>
<div class='Entrance_ServerListName'>{$me['NAME']}</div>
<input class='Entrance_ServerListLogin' type='button' value='입장' onclick='Entrance_Enter(\"{$_serverDirs[$i]}/login_process.php\")'>
<input class='Entrance_ServerListLogin' type='button' value='입장' onclick='Entrance_Enter(\"{$_serverDirs[$i]}/index.php\")'>
";
} elseif($gen['CNT'] >= $game['MAXGENERAL']) {
$character = "<div class='Entrance_ServerListBlock'>- 장수 등록 마감 -</div>";
+2 -2
View File
@@ -13,13 +13,13 @@ $id = $_POST['id'];
$pw = $_POST['pw'];
$conmsg = $_POST['conmsg'];
$_SESSION['conmsg'] = $conmsg;//XXX: conmsg를 처리할 더 적절한 장소는?
$id = _String::NoSpecialCharacter($id);
$pw = substr($pw, 0, 32);
$response['result'] = 'FAIL';
_Session::TrashSession();
$SESSION = new _Session();
$rs = $DB->Select('NO, GRADE, QUIT', 'MEMBER', "ID='{$id}' AND PW='{$pw}'");
+54 -11
View File
@@ -46,27 +46,37 @@ function getUserID($forceExit=false){
* @return int|null
*/
function getGeneralID($forceExit=false){
$username = getUserID();
if(!$username){ //유저명 없으면 어차피 의미 없음.
$userID = getUserID();
if(!$userID){ //유저명 없으면 어차피 의미 없음.
return null;
}
$id_key = getServPrefix().'p_no';
$generalID = util::array_get($_SESSION[$id_key], null);
$idKey = getServPrefix().'p_no';
$generalID = util::array_get($_SESSION[$idKey], null);
if($generalID){
return $generalID;
}
$db = getDB();
//흠?
$generalID = getDB()->queryFirstField('select no from general where user_id = %s', $username);
$generalID = $db->queryFirstField('select no from general where user_id = %s', $userID);
if(!$generalID && $forceExit){
header('Location:..');
die();
}
if($generalIDid){
$_SESSION[$id_key] = $generalID;
if($generalID){
//로그인으로 처리
//XXX: 'get' 함수인데 update가 들어가있다.
//TODO: 조금더 적절한 형태의 로그인 카운트를 생각해볼 것
$query=$db->query("update general set logcnt=logcnt+1 ,ip = %s_ip,lastconnect=%s_lastConnect,conmsg=%s_conmsg where user_id= %s_userID",[
'ip' => getenv("REMOTE_ADDR"),
'lastConnect' => date('Y-m-d H:i:s'),
'conmsg' => util::array_get($_SESSION['conmsg'], ''),
'userID' => $userID
]);
$_SESSION[$idKey] = $generalID;
}
return $generalID;
@@ -89,8 +99,8 @@ function getGeneralName($forceExit=false)
return null;
}
$id_key = getServPrefix().'p_name';
$generalName = util::array_get($_SESSION[$id_key], null);
$nameKey = getServPrefix().'p_name';
$generalName = util::array_get($_SESSION[$nameKey], null);
if($generalName){
return $generalName;
@@ -98,6 +108,32 @@ function getGeneralName($forceExit=false)
//흠?
$generalName = getDB()->queryFirstField('select name from general where no = %i', $generalID);
if(!$generalName){
//이게 말이 돼?
resetSessionGeneralValues();
if($forceExit){
header('Location:..');
die();
}
}
if($generalName){
$_SESSION[$nameKey] = $generalName;
}
return $generalName;
}
/**
* Session에 보관된 장수 정보를 제거함.
* _prefix_p_no, _prefix_p_name 두 값임
*/
function resetSessionGeneralValues(){
$idKey = getServPrefix().'p_no';
$nameKey = getServPrefix().'p_name';
unset($_SESSION[$idKey]);
unset($_SESSION[$nameKey]);
}
function GetImageURL($imgsvr) {
@@ -112,16 +148,23 @@ function GetImageURL($imgsvr) {
/**
* generalID를 이용해 각 서버 등록 여부를 확인함
*
* FIXME: 현재의 구현으로는 session에 p_no가 남아있지만 general 테이블에서는 삭제되어있는 진풍경(!)이 펼쳐질 수도 있음.
* 장수가 사망했을 때에 바로 테이블에서 삭제하는 것이 아니라 삭제 플래그를 설정하는 것이 나을 것임.
* (퀘 섭등 NPC가 빙의할 경우에도 마찬가지!)
*
* 세팅된 플래그는 새롭게 장수를 생성하거나, 새로 빙의할 때 초기화하는 것이 적절한 해결책일 것.
*
* @return bool
*/
function isSigned(){
$p_id = getGeneralID();
if(!$_pid){
$generalID = getGeneralID();
if(!$generalID){
return false;
}
return true;
}
function checkLimit($userlevel, $con, $conlimit) {
//TODO: 접속 제한의 기준을 새로 세울 것.
//운영자
+1 -5
View File
@@ -3,14 +3,10 @@
function CheckLogin($type=0) {
if(!isset($_SESSION['p_id'])) {
if($type == 0) {
header('Location: start.php');
//echo "<script>location.replace('start.php');</script>";
//echo 'start.php';//TODO:debug all and replace
header('Location: ../');
}
else {
header('Location: main.php');
//echo 'main.php';//TODO:debug all and replace
//echo "<script>window.top.main.location.replace('main.php');</script>";
}
exit();
}
+2 -3
View File
@@ -65,18 +65,17 @@ if($img < 1) { $picture = 'default.jpg'; };
$connect
) or Error(__LINE__.MYDB_error($connect),"");
$pw = md5('12qw!@QW12qw!@QW');
$picture = 'pic_2.jpg';
if($img < 1) { $picture = 'default.jpg'; };
//부운영자는 비밀번호를 지정하지 않아 로그인할수 없도록 처리한다.
@MYDB_query("
insert into general (
user_id, password, connect, name, picture, nation, city, troop, makelimit,
leader, power, intel, experience, dedication, gold, rice, crew, train, atmos,
weap, book, level, turntime, killturn, lastconnect, userlevel
) values (
'viceadmin', '$pw', '0', '부운영자', '$picture', '0', '3', '0', '0',
'viceadmin', 'a', '0', '부운영자', '$picture', '0', '3', '0', '0',
'50', '50', '50', '0', '0', '10000', '10000', '0', '0', '0',
'0', '0', '0', '$turntime', '80', '$lastconnect', '5'
)",
-1
View File
@@ -24,7 +24,6 @@ if(MYDB_error($connect)) Error(__LINE__.MYDB_error($connect),"");
MYDB_select_db($dbname, $connect) or Error("MySQL-DB Select<br>Error!!!","");
delInDir("logs");
delInDir("data/session");
@unlink("data/connected.php");
// 관리자 테이블 삭제
+9
View File
@@ -51,6 +51,15 @@ $conlimit = $db->queryFirstField('select conlimit from game where no=1');
$me = $db->queryFirstRow('select `no`,`name`,`nation`,`level`,`msgindex`,`userlevel`,`con`,`picture`,`imgsvr` from `general` where `user_id` = %s_p_id',
array('p_id'=>$_SESSION['p_id']));
if(!$me){
resetSessionGeneralValues();
returnJson([
'result' => false,
'reason' => '로그인되지 않았습니다.',
'redirect' => NULL
]);
}
$con = checkLimit($me['userlevel'], $me['con'], $conlimit);
if($con >= 2) {
returnJson([
+1 -14
View File
@@ -14,7 +14,6 @@ use utilphp\util as util;
define('__OLINE__',__LINE__);
define('__LINE__',__FILE__." ".__FUNCTION__." ".__LINE__." : ");
ini_set("session.cache_expire", 10080); // minutes
ini_set("session.gc_maxlifetime", 604800); // seconds
ob_start();
@@ -73,22 +72,11 @@ unset($setup);
// data폴더가 없으면 data/session까지 생성
if(is_dir("data")){
if(!is_writable("data")) Error("Data 디렉토리의 쓰기 권한이 없습니다!");
if(is_dir("data/session")){
if(!is_writable("data/session")) Error("세션 디렉토리 data/session의 쓰기 권한이 없습니다!");
}else{
mkdir("data/session");
}
}else{
mkdir("data");
mkdir("data/session");
}
session_save_path('data/session');
session_cache_limiter('nocache, must_revalidate');//NOTE: 캐시가 가능할 수도 있음. 주의!
session_set_cookie_params(0, '/');
session_cache_expire(60); // 60분
session_cache_limiter('nocache, must_revalidate');//NOTE: 캐시가 가능하도록 설정해야 할 수도 있음. 주의!
// 세션 변수의 등록
//NOTE: ajax등의 경우에는 session_write_close로 빠르게 끝낼 수 있어야한다.
@@ -106,7 +94,6 @@ if($_SESSION['p_time']+3600 < time()) {
unset($_SESSION['p_id']);
unset($_SESSION[getServPrefix().'p_name']);
$_SESSION['p_time'] = time();
session_destroy();
} else {
$_SESSION['p_time'] = time();
}
+1
View File
@@ -62,6 +62,7 @@ $_SESSION['p_time'] = time();
$date = date('Y-m-d H:i:s');
//
$query="update general set logcnt=logcnt+1,ip='{$_SESSION['p_ip']}',lastconnect='$date',conmsg='$conmsg' where user_id='{$_SESSION['p_id']}'";
MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
+1 -3
View File
@@ -8,6 +8,4 @@ unset($_SESSION['p_ip']);
unset($_SESSION[getServPrefix().'p_no']);
unset($_SESSION[getServPrefix().'p_name']);
session_destroy();
header('Location:start.php');
header('Location:../');
+19 -13
View File
@@ -1,27 +1,33 @@
<?php
include "lib.php";
include "func.php";
//로그인 검사
CheckLogin();
$connect = dbConn();
increaseRefresh($connect, "메인", 2);
checkTurn($connect);
if(!isset($_SESSION['p_id'])){
echo "<script>location.replace('start.php');</script>";
exit(0);
$db = getDB();
//로그인 검사
if(!isSigned()){
header('Location:../');
die();
}
$query = "select no,skin,userlevel,con,turntime,newmsg,newvote,map from general where user_id='{$_SESSION['p_id']}'";
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");
$me = MYDB_fetch_array($result);
$userID = getUserID();
if(!$userID){
header('Location:../');
die();
}
$me = $db->queryFirstRow('select no,skin,userlevel,con,turntime,newmsg,newvote,map from general where user_id = %s', $userID);
//그새 사망이면
if($me['no'] == 0) {
//echo "a";
header('Location: start.php');
//echo "<script>location.replace('start.php');</script>";
exit(0);
if($me === null) {
resetSessionGeneralValues();
header('Location: ../');
die();
}
if($me['newmsg'] == 1 && $me['newvote'] == 1) {
+2
View File
@@ -2,6 +2,8 @@
include "lib.php";
include "func.php";
//NOTE:관리자의 경우 NPC로그인을 user_id는 유지하되 no값만 바꾸는 식으로 가능하지 않을까?
$connect=dbConn();
?>
-2
View File
@@ -7,8 +7,6 @@ $id = $_POST['id'];
$pw = $_POST['pw'];
$pw = md5($pw.$pw);
DeleteSession();
//회원 테이블에서 정보확인
$query="select no from general where user_id='$id'";
$result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),"");