2012-03-05 4 views
2

나는 이상한 문제가 있습니다. 방문자가 사이트에 처음 방문했을 때 세션을 설정하면 세션이 고정되지 않습니다. 두 번째 및 다음 번에 내가 찌르는 것을 설정하려고합니다. 처음 시도한 후에 나는 세션을 파괴하고 무언가를 설정할 수 있습니다. 그저 초기 무언가를 저장하려는 시도는 실패합니다. $_SESSION['uid'] = $row["Id"];으로 세션에 뭔가를 저장하려고합니다. $row["Id"]이 유효하고 데이터를 보유하고 있음을 알고 있습니다.PHP 세션이 처음으로 설정되지 않았습니다.

표준 세션을 사용하지 않습니다. 세션을 데이터베이스에 저장하고 있습니다. 세션 클래스가 아래에 있습니다. 이 행동을 설명하기 위해 내가 누락되었거나 잘못하고있는 것이 있습니까?

업데이트 :

이 잘 나는 자신의 세션 클래스를 테스트하고 그것은 작동하는 것 같군 : - /하지만 __destruct하지만, 내 큰 응용 프로그램 호출되지 없구요 _write에서 사용할 때 호출되는 않습니다. 그게 어떨까요?

<?php 

include_once('db.php'); 

class PDOSession 
{ 
    protected $pdo; 
    protected $table = 'SessionData'; 

    public function __construct() 
    { 
     // Get a database connection 
     $db = new PDOConnectionFactory(); 
     $this->pdo = $db->getConnection(true); 

     // Start session 
     session_set_save_handler(array($this, '_open'), 
           array($this, '_close'), 
           array($this, '_read'), 
           array($this, '_write'), 
           array($this, '_destroy'), 
           array($this, '_gc')); 
     session_start(); 
    } 

    public function __destruct() 
    { 
     session_write_close(); 
    } 

    protected function fetchSession($id) 
    { 
     $stmt = $this->pdo->prepare('SELECT id, data FROM '.$this->table.' WHERE id = :id AND unixtime > :unixtime'); 
     $stmt->execute(array(':id' => $id, ':unixtime' => (time() - (int)ini_get('session.gc_maxlifetime')))); 
     $sessions = $stmt->fetchAll(); 

     return empty($sessions) ? false : $sessions[0]; 
    } 

    public function _open($savePath, $sessionName) 
    { 
     return true; 
    } 

    public function _close() 
    { 
     return true; 
    } 

    public function _read($id) 
    { 
     $session = $this->fetchSession($id); 
     return ($session === false) ? false : $session['data']; 
    } 

    public function _write($id, $sessionData) 
    { 
     $session = $this->fetchSession($id); 
     if($session === false) { 
      $stmt = $this->pdo->prepare('INSERT INTO '.$this->table.' (id, data, unixtime) VALUES (:id, :data, :time)'); 
     } else { 
      $stmt = $this->pdo->prepare('UPDATE '.$this->table.' SET data = :data, unixtime = :time WHERE id = :id'); 
     } 
     $stmt->execute(array(
         ':id' => $id, 
         ':data' => $sessionData, 
         ':time' => time() 
         )); 
    } 

    public function _destroy($id) 
    { 
     $stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE id = :id'); 
     $stmt->execute(array(':id' => $id)); 
    } 

    public function _gc($maxlifetime) 
    { 
     $stmt = $this->pdo->prepare('DELETE FROM '.$this->table.' WHERE unixtime < :time'); 
     $stmt->execute(array(':time' => (time() - (int) $maxlifetime))); 
    } 
} 
$newPDOSessionStartHere = new PDOSession(); 

답변

1

나는 바보 같아. session_unset()이 아닌 session_destroy()을 호출하여 인증 스크립트 맨 위의 내용을 지 웁니다. 수업은 잘 작동합니다.

0

클래스를 정의한 후에 세션을 시작해야한다고 생각합니다. 안쪽에.

+0

이것은 차이가없는 것처럼 보입니다. – Justin808

관련 문제