Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problem z destruktorem
keyzen
post
Post #1





Grupa: Zarejestrowani
Postów: 6
Pomógł: 0
Dołączył: 15.02.2008

Ostrzeżenie: (0%)
-----


Problem jest taki że destruktor nie działa jak powinien, metoda która ma taki sam kod działa.

Wyskakuje błąd:
Warning: file_put_contents(./sessions/033b5e522aec54902e89542f0fbe68203285a9c0) [function.file-put-contents]: failed to open stream: No such file or directory in F:\htdocs\sessions\index.php on line 31
A plik istnieje. Metoda normalnie zapisuje plik.

Destruktor i metoda:
Kod
public function __destruct()
{
    file_put_contents($this->sessionsDir . $this->sessionID, base64_encode(serialize($this->sessionVars)));
    
    return;
}

public function A_TA_METODA_DZIALA()
{
    file_put_contents($this->sessionsDir . $this->sessionID, base64_encode(serialize($this->sessionVars)));
    
    return;
}


i cały kod:
Kod
<?php

class Session
{
    protected $sessionsDir = './sessions/';
    protected $sessionName = 'sid';
    protected $sessionID;
    protected $sessionVars;
    
    public function __construct($sessionID = null)
    {
        if ($sessionID === null)
        {
            $sessionID = $this->checkCookie();
            
            if ($sessionID === null)
            {
                $sessionID = $this->createNewSession();
            }
        }
        
        $this->sessionID = $sessionID;
        
        $this->assignSessionVars();
        
        return;
    }
    
    public function __destruct()
    {
        file_put_contents($this->sessionsDir . $this->sessionID, base64_encode(serialize($this->sessionVars)));
        
        return;
    }
    
    public function A_TA_METODA_DZIALA()
    {
        file_put_contents($this->sessionsDir . $this->sessionID, base64_encode(serialize($this->sessionVars)));
        
        return;
    }
    
    public function __get($name)
    {
        return $this->sessionVars[$name];
    }
    
    public function __set($name, $value)
    {
        $this->sessionVars[$name] = $value;
        
        return;
    }
    
    protected function checkCookie()
    {
        if (isset($_COOKIE[$this->sessionName]))
        {
            if (preg_match('/^[a-z0-9]+$/i', $_COOKIE[$this->sessionName]) && file_exists($this->sessionsDir . $_COOKIE[$this->sessionName]))
            {
                return $_COOKIE[$this->sessionName];
            }
        }
        
        return null;
    }
    
    protected function createNewSession()
    {
        do
        {
            $sessionID = sha1((string) microtime(1));
        }
        while (file_exists($this->sessionsDir . $sessionID));
        
        file_put_contents($this->sessionsDir . $sessionID, null);
        
        setcookie($this->sessionName, $sessionID);
        
        return $sessionID;
    }
    
    protected function assignSessionVars()
    {
        $this->sessionVars = unserialize(base64_decode(file_get_contents($this->sessionsDir . $this->sessionID)));
        
        return;
    }
    
    public function getSessionName()
    {
        return $this->sessionName;
    }
    
    public function getSessionID()
    {
        return $this->sessionID;
    }
}

$u = new Session();

if ($u->counter === null)
{
    $u->counter = 0;
}

echo ++$u->counter;

$u->A_TA_METODA_DZIALA();

//Ale destruktor juz nie :(
?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 04:38