카테고리 없음

[PHP] PHP 세션 클래스 및 $ _SESSION 배열

행복을전해요 2021. 1. 10. 23:45

mysqli_stmt::fetch()행을 나타내는 배열을 반환하지 않고 true 또는 false 만 반환합니다. 따라서 귀하의 코드_read()

$allData= $getData->fetch();
$totalData = count($allData);
$hasData=(bool) $totalData >=1;
return $hasData ? $allData['data'] : '';

작동하지 않습니다. $allData어느 것 true또는 false더 배열 요소는 없다 $allData['data'].

http://docs.php.net/mysqli-stmt.fetch 말한다 :

준비된 명령문의 결과를 mysqli_stmt_bind_result ()로 바인딩 된 변수로 가져옵니다 .
  public function _read($id)
  {
      $getData= $this->database->prepare("SELECT data FROM
            Sessions AS Session
                  WHERE Session.id = ?
                      ");
                          if ( false===$getData ) {
                                // now what?
                                    }
                                    
                                        $getData->bind_param('s',$id);
                                            $getData->bind_result($data);
                                                if ( false===$getData->execute() ) {
                                                      // now what?
                                                          }
                                                              return  $getData->fetch() ? $data : '';
                                                                }
                                                                
-------------------

여기에 업데이트 된 코드가 있습니다 !!! :-) 이제 완전히 작동합니다 !!!

<?php
class session {
    private $_session;
        public $maxTime;
            private $db;
                public function __construct() {
                        $this->maxTime['access'] = time();
                                $this->maxTime['gc'] = 21600; //21600 = 6 hours
                                
                                        //it is session handler
                                                session_set_save_handler(array($this,'_open'),
                                                                array($this,'_close'),
                                                                                array($this,'_read'),
                                                                                                array($this,'_write'),
                                                                                                                array($this,'_destroy'),
                                                                                                                                array($this,'_clean')
                                                                                                                                                );
                                                                                                                                                
                                                                                                                                                        register_shutdown_function('session_write_close');
                                                                                                                                                        
                                                                                                                                                                session_start();//SESSION START
                                                                                                                                                                    }
                                                                                                                                                                    
                                                                                                                                                                        private function getDB() {
                                                                                                                                                                                $mysql_host = 'your_host';
                                                                                                                                                                                        $mysql_user = 'user';
                                                                                                                                                                                                $mysql_password = 'pass';
                                                                                                                                                                                                        $mysql_db_name = 'db_name';
                                                                                                                                                                                                        
                                                                                                                                                                                                        
                                                                                                                                                                                                                if (!isset($this->db)) {
                                                                                                                                                                                                                            $this->db = new mysqli($mysql_host, $mysql_user, $mysql_password, $mysql_db_name);
                                                                                                                                                                                                                                        if (mysqli_connect_errno()) {
                                                                                                                                                                                                                                                        printf("Error no connection: <br />%s\n", mysqli_connect_error());
                                                                                                                                                                                                                                                                        exit();
                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                    return $this->db;
                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                            // O_O !!!
                                                                                                                                                                                                                                                                                                                public function _open() {
                                                                                                                                                                                                                                                                                                                        return true;
                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                public function _close() {
                                                                                                                                                                                                                                                                                                                                        $this->_clean($this->maxTime['gc']);
                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                public function _read($id)  {       
                                                                                                                                                                                                                                                                                                                                                        $stmt= $this->getDB()->prepare("SELECT session_variable FROM table_sessions 
                                                                                                                                                                                                                                                                                                                                                                                                    WHERE table_sessions.session_id = ?");
                                                                                                                                                                                                                                                                                                                                                                                                            $stmt->bind_param('s',$id);
                                                                                                                                                                                                                                                                                                                                                                                                                    $stmt->bind_result($data);
                                                                                                                                                                                                                                                                                                                                                                                                                            $stmt->execute();
                                                                                                                                                                                                                                                                                                                                                                                                                                    $ok = $stmt->fetch() ? $data : '';
                                                                                                                                                                                                                                                                                                                                                                                                                                            $stmt->close();
                                                                                                                                                                                                                                                                                                                                                                                                                                                    return $ok;
                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                            public function _write($id, $data) {    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $stmt = $this->getDB()->prepare("REPLACE INTO table_sessions (session_id, session_variable, session_access) VALUES (?, ?, ?)");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $stmt->bind_param('ssi', $id, $data, $this->maxTime['access']);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $ok = $stmt->execute();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $stmt->close();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return $ok;     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            public function _destroy($id) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $stmt=$this->getDB()->prepare("DELETE FROM table_sessions WHERE session_id = ?");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $stmt->bind_param('s', $id);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $ok = $stmt->execute();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $stmt->close();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $ok;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        public function _clean($max) {
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $old=($this->maxTime['access'] - $max);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                $stmt = $this->getDB()->prepare("DELETE FROM table_sessions WHERE session_access < ?");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    $stmt->bind_param('s', $old);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        $ok = $stmt->execute();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            $stmt->close();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                return $ok;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ?>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

다음은 세션 테이블입니다.

CREATE TABLE IF NOT EXISTS `table_sessions` (
  `session_id` varchar(50) NOT NULL,
    `session_variable` text NOT NULL,
      `session_access` decimal(15,0) NOT NULL,
        PRIMARY KEY  (`session_id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
        
-------------------

먼저 세션시작 해야 할까요?

-------------------

세션이 지속되지 않는 것 같은 이전에

페이지를 변경할 때와 수동으로 설정하지 않은 경우 세션 ID가 동일하게 유지되는지 확인할 수 있습니다.

var_dump(session_id());

if (session_id()=="")
{
    if ($_GET["sessionid"])
        {
                session_id($_GET["sessionid"]);
                    }
                        elseif ($_POST["sessionid"])
                            {
                                    session_id($_POST["sessionid"]);
                                        }
                                        
                                            session_start();
                                            }
                                            

이것은 이것이 문제인지 확인하는 테스트에 가깝습니다. 쿼리 문자열에서 세션 ID를 설정하는 것이 보안에 어떤 영향을 미치는지 잘 모르겠지만 좋지 않은 것 같습니다!



출처
https://stackoverflow.com/questions/2006064