php 중복 로그인 방지

php 중복 로그인 방지


몇년전 중복로그인 방지를 해야하는일이 있어 만들었던 소스입니다. 정확히는 A-pc에서 로그인했을때 B-pc에서 로그인이 되지 않는게 아니라, A-pc에서 로그인했을때 B-pc에서 로그인하면 2군데 모두 세션이 없어지고 로그아웃 처리되는 개념입니다. 스쿨에 괴수분들이 많아서 팁아닌 팁 올리는게 부끄럽네요;




 

<?php
/**

CREATE TABLE `avoid_duplication` (
  `idx`         int(11)         NOT NULL AUTO_INCREMENT,
  `mem_idx`     int(11)         DEFAULT NULL, # 회원의 index
  `sess_id`     varchar(100)    DEFAULT NULL, # 세션아이디 (회원의 아이디 아님.)
  `wrt_date`    int(11)         DEFAULT NULL, # 등록일 time()
  `ip`          varchar(255)    DEFAULT NULL, # 아이피.
  PRIMARY KEY (`idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 
 
CREATE TABLE `member_block` (
  `mb_idx`          int(11)         NOT NULL AUTO_INCREMENT,
  `mem_idx`         int(11)         DEFAULT NULL, # 회원의 index
  `mem_wrt_date`    int(11)         DEFAULT NULL, # 차단된 날짜 time()
  `mb_code`         varchar(255)    DEFAULT NULL, # 차단해제 신청시 인증번호 체크
  PRIMARY KEY (`mb_idx`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
$sess_uidx : 회원의 index를 세션값으로..
 * */
Class AvoidDuplication extends DB
{
    var $TBL = "avoid_duplication";
    
    function __construct()
    {
        $this->ezSQL_mysql(
            DBUSER,
            DBPW,
            DBNAME,
            DBHOST
        );
    }

    function bloackChk($sess_uidx)
    {
        
        $sql = "select count(*) as cnt from member_block where mem_idx = '{$sess_uidx}'";
        $cnt = $this->get_row($sql)->cnt;
        if ($cnt!=0)
        {
            //alert("중복로그인으로 인해 블럭처리된 아이디입니다.\\n관리자에게 문의주시거나, \'중복로그인해지신청\'을 해주세요.");
			//location.href = 'logout.php';
        }
    }

    function processing()
    {
        global $sess_uidx;
        $nowTime    = time();
        $IP         = $_SERVER["REMOTE_ADDR"];
        
        $this->bloackChk($sess_uidx);
        
        $sql = "SELECT * FROM {$this->TBL} WHERE mem_idx = '{$sess_uidx}'";
        $row = $this->get_row($sql);
        
        $resTime    = $nowTime - $row->wrt_date;
        $time       = 60*10;
        
        if ($sess_uidx)
        {
            if ($row->mem_idx=="")
            {
                $sql = "INSERT INTO {$this->TBL} (mem_idx, ip, wrt_date) VALUES ('{$sess_uidx}','{$IP}','{$nowTime}')";
                $this->query($sql);
            }
            else
            {
                if ($row->ip!=$IP)
                {
                    if ($resTime < $time+1)
                    {
                        // 블럭처리
                        $sql ="INSERT INTO member_block (mem_idx, mem_wrt_date) VALUES ('{$sess_uidx}','$nowTime')";
                        $this->query($sql);
                    }
                    else
                    {
                        $sql = "UPDATE {$this->TBL} SET wrt_date = '{$nowTime}',ip = '{$IP}' WHERE mem_idx = '{$sess_uidx}'";
                        $this->query($sql);
                    }
                }
                else
                {
                    // do noting
                }
            }
        }
        else
        {
            // do noting
        }
    }

    function processingStop()
    {
        global $sess_uidx;
        $sql = "delete from {$this->TBL} where mem_idx = '{$sess_uidx}'";
        $this->query($sql);
        
    }
    
    // 인증번호 생성하여 발송하고 '차단'테이블에 인증번호 입력.
    function duplication($mem_id,$mem_pw,$mem_ssn1,$mem_ssn2,$mem_mail1,$mem_mail2)
    {
        global $sess_uidx;
        $sql = "
        SELECT *, count(*) AS cnt FROM member WHERE
        mem_id = '$mem_id' AND
        mem_pw = '$mem_pw' AND
        mem_ssn1 = '$mem_ssn1' AND
        mem_ssn2 = '$mem_ssn2' AND
        mem_mail1 = '$mem_mail1' AND
        mem_mail2 = '$mem_mail2' 
        ";
        $row = $this->get_row($sql);
        if ($row->cnt!=0)
        {
            $key = rand(0,9999);
            $sql = "UPDATE member_block SET mb_code = '$key' WHERE mem_idx = '$row->mem_idx'";
            $res = $this->query($sql);
            
            if ($res)
            {
                // 이곳에서 인증번호를 메일이나 문자로 발송. 인증번호는 $key 변수.
                echo true;
            }
        }
    }

    // 차단 해제.
    function duplicationDel($mem_id, $mb_code)
    {
        global $sess_uidx;
        $sql = "SELECT * FROM member WHERE mem_id = '{$mem_id}'";
        $row = $this->get_row($sql);
        $row->mem_idx;
        
        $sql = "SELECT count(*) AS cnt FROM member_block WHERE mem_idx = '$row->mem_idx'
        AND mb_code = '$mb_code'";
        $cnt = $this->get_row($sql)->cnt;
        
        if ($cnt!=0)
        {
            $sql = "DELETE FROM member_block WHERE mem_idx = '$row->mem_idx'
            AND mb_code = '$mb_code'";
            $res = $this->query($sql);
            echo true;
        }
        else {
            echo false;
        }
    }
}

$aDupli = new AvoidDuplication();
$aDupli->processing();

// 사용시
//$aDupli->bloackChk($sess_idx);
?>