2013-09-25 6 views
1

이 헬퍼 클래스를 작성하여 데이터베이스에 세션을 저장했지만 전혀 작동하지 않는 것 같습니다. 나는 session_set_save_handler의 리턴 값을 검사했고, 항상 false 값을 반환하는 것으로 보입니다. 이것은 처음에 핸들러 함수를 설정할 수 없다는 것을 의미합니다. 그런 다음 session.auto_start = 0 및 session.save_handler = 'user'설정을 시도했지만 아무 것도 변경하지 않는 것 같습니다. 이 문제를 해결하기 위해 PHP.ini에서 변경할 수있는 것이 있습니까? 아니면 문제가 자체 클래스에 있습니까?데이터베이스에 세션 저장 중

CREATE TABLE `sessions` (
    `id` char(32) NOT NULL, 
    `data` text NOT NULL, 
    `access` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 
+1

클래스 다음

class Session { private $db; public function __construct() { //Instantiate new Database object $this->db = new Database(); // Set handler to overide SESSION $return = session_set_save_handler( array($this, "open"), array($this, "close"), array($this, "read"), array($this, "write"), array($this, "destroy"), array($this, "gc") ); var_dump ($return); register_shutdown_function ('session_write_close') ; session_start(); } /** * Open function * * @param none * @return bool */ public function open() { if ($this->db) { return true; } return false; } /** * Close function * * @param none * @return bool */ public function close() { if($this->db->close()) { return true; } return false; } /** * Read function * * @param string $id * @return mixed */ public function read ($id) { $this->db->query('SELECT data FROM sessions WHERE id = :id'); $this->db->bind(':id', $id); if ($this->db->execute()) { $row = $this->db->single(); return $row['data']; } return ''; } /** * Write function * * @param string $id * @param string $data * @return bool */ public function write ($id, $data) { $access = time(); $this->db->query('REPLACE INTO sessions VALUES (:id, :access, :data)'); $this->db->bind(':id', $id); $this->db->bind(':access', $access); $this->db->bind(':data', $data); if ($this->db->execute()){ return true; } return false; } /** * Destroy function * * @param string $id * @return bool */ public function destroy ($id) { $this->db->query('DELETE FROM sessions WHERE id = :id'); $this->db->bind(':id', $id); if ($this->db->execute()) { return true; } return false; } /** * Garbage collector function * * @param int $maxLifeTime * @return bool */ public function gc ($maxLifeTime){ $old = time() - $maxLifeTime; // Delete expired sessions from the database $this->db->query('DELETE * FROM sessions WHERE access < :old'); $this->db->bind(':old', $old); if($this->db->execute()) { return true; } return false; } } 

내가 세션을 저장하는 테이블에 대해 사용하고 데이터베이스 구조의 'close' 메서드가 누락 된 것 같습니다 – rsanchez

+0

가까이있는 메서드를 추가했지만 여전히 작동하지 않습니다 ... – Ashesh

답변

관련 문제