Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Incomplete Object per oggetti in $_SESSION

    Salve, sto cercando di salvare un oggetto in una variabile di sessione $_SESSION per poterlo poi riutilizzare successivamente.
    Riporto qui sotto il codice in questione:
    Codice PHP:
    require_once('VFSclient.class.php');
    session_start();

    if (isset(
    $_POST['submit'])) {
        switch (
    $_POST['submit']){
            case 
    'Entra':
                
    //LOGIN
                
    if(doLogin($_POST['username'], $_POST['password'])){
                    
    //LOGIN OK
                    
    echo "Login OK - USER = ".$_POST['username']." - PASS = ".$_POST['password']. " - TKEY = ".$_SESSION['vfs']->tkey;
                    
    //header("Location: main.php");
                
    } else {
                    
    //LOGIN ERROR
                    
    $_POST['errmsg'] = $_SESSION['vfs']->getErrorMessage();
                    include(
    'login.php');
                }
                break;
            case 
    'Registrati':
                
    //VISUALIZZA PAGINA DI REGISTRAZIONE
                
    include('register.php');
                break;
            case 
    'Procedi':
                
    //EFFETTUA REGISTRAZIONE
                
    if(doRegister($_POST['username'], $_POST['password'], $_POST['email'])){
                    
    // USERADD OK - NOW LOGIN
                    
    include('login.php');
                } else {
                    
    //FALLISCE LA USERADD
                
    }
                break;
        }
    } else {
        
    //PRIMA VOLTA CHE ACCEDO - CREO L'OGGETTO VFSClient()
        
    $_SESSION['vfs'] = new VFSClient();
        include(
    'login.php');
    }

    function 
    doLogin($username$password){
        return 
    $_SESSION['vfs']->login($username,$password);
    }

    function 
    doRegister($username$password$email){
        return 
    $_SESSION['vfs']->userAdd($username,$password,$email);

    Questo codice però contiene qualcosa che non va, infatti, ogni volta che tento di eseguire le operazioni effettuate dalle funzioni doLogin e doRegister php risponde con:

    Fatal error: VFSClient::login() [function.VFSClient-login]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "WebService_VFSiService_VFService" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in C:\Programmi\Apache Software Foundation\Apache2.2\htdocs\VFSclient\VFSclient.cl ass.php on line 49

    Qualcuno saprebbe dirmi cosa sto sbagliando?

    Ciao,
    Samuele.

  2. #2
    Scusa ma $_SESSION è un Array, perchè lo usi come oggetto?
    Per forza ti dà quell'errore...
    Michele Castellucci
    Sviluppatore Web del Consorzio CottonBit
    Consorzio Cottonbit
    Risorse per la programmazione
    Dire Fare Programmare!

  3. #3
    Non capisco bene cosa intendi dire...
    a quanto avevo capito $_SESSION è un array associativo, ovvero dentro ogni elemento di $_SESSION io posso metterci "quello che voglio" nel senso che posso fare:
    Codice PHP:
    $_SESSION['stringa'] = "stringa";
    $_SESSION['numero'] = 3;
    $_SESSION['oggetto'] = new ObjClass(); 
    quindi, a quanto pensavo, credevo che avrei potuto richiamare i metodi dell'oggetto memorizzato in $_SESSION['oggetto'] semplicemente con:
    Codice PHP:
    $_SESSION['oggetto']->metodo1();
    $_SESSION['oggetto']->metodo2(); 
    dove metodo1() e metodo2() sono metodi della classe ObjClass.

    Se non è possibile fare ciò, come posso allora memorizzare un oggetto per una sessione?

    In caso fosse utile riporto anche il contenuto della classe VFSclient:
    Codice PHP:
    require('SOAP/Client.php');

    /* METODI DEL WEBSERVICE
     * -- addResource(String TKEY, String url, String username, String pass)
     * -- synchronizeFolders(String first, String second, String TKEY)
     * -- upload(String encodedFile, String fileName, String To, String TKEY)
     * -- Rename(String Path, String oldName, String newName, String TKEY)
     * -- getAllResource(String TKEY)
     * -- getAllFile(String Resource, String TKEY)
     * -- login(String User, String Pass)
     * -- getInfo(String Resource, String TKEY)
     * -- logout(String TKEY)
     * -- addUser(String nick,String pass,String email)
     * -- deleteUser(String TKEY)
     * -- changeUserPwd(String TKEY, String newpass)
     * -- deleteResource(String TKEY, String url, String username, String pass)
     * -- changeResource(String TKEY, String url, String username, String pass, String new_url, String new_username, String new_pass)
     * -- copy(String From, String To, String TKEY)
     * -- delete(String Resource, String TKEY)
     * -- move(String From, String To, String TKEY)
     * -- download(String From, String TKEY)
     */

    class VFSClient{
        
        private 
    $WSDL_URL 'VFService.wsdl';
        private 
    $WSDL;
        private 
    $client;
        public  
    $tkey;
        public  
    $err_msg;
        public  
    $res_arr = array();
        public  
    $myVSF   = array();
        
        public function 
    __construct(){
            
    $this->WSDL    = new SOAP_WSDL($this->WSDL_URL); 
            
    $this->client  $this->WSDL->getProxy();
        }
        
        public function 
    getErrorMessage(){
            return 
    $this->err_msg;
        }
        
        public function 
    getTKEY(){
            return 
    $this->tkey;
        }
        
        public function 
    login($username$password){
            
    $ret $this->client->login($username,$password);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // LOGIN SUCCESSFUL
                
    $this->tkey $arr[1];
                return 
    true;
            } else {
                
    // LOGIN FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    logout(){
            
    $ret $this->client->logout($this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // LOGOUT SUCCESSFUL
                
    return true;
            } else {
                
    // LOGOUT FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    userAdd($username$password$email){
            
    $ret $this->client->addUser($username$password$email);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // USERADD SUCCESSFUL
                
    return true;
            } else {
                
    // USERADD FAILED - USER NOT ADDED IN DATABASE
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    userDel(){
            
    $ret $this->client->deleteUser($this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // USERDEL SUCCESSFUL
                
    return true;
            } else {
                
    // USERDEL FAILED - USER IS STILL IN DATABASE
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    userMod($newpassword){
            
    $ret $this->client->changeUsrPwd($this->tkey$newpassword);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS - PASSWORD CHANGED TO NEWPASSWORD
                
    return true;
            } else {
                
    // FAILED - PASSWORD HAS NOT BEEN CHANGED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    resAdd($url$username$password){
            
    $ret $this->client->addResource($this->tkey$url$username$password);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS - NEW RESOURCE ADDED IN DATABASE
                
    return true;
            } else {
                
    // FAILED - RESOURCE HAS NOT BEEN ADDED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    resDel($url$username$password){
            
    $ret $this->client->changeResource($this->tkey$url$username$password);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS - RESOURCE DELETED
                
    return true;
            } else {
                
    // FAILED - RESOURCE HAS NOT BEEN DELETED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public  function 
    resMod($old_url$old_user$old_password$new_url$new_user$new_password){
            
    $ret $this->client->changeResource($this->tkey$old_url$old_user$old_password$new_url$new_user$new_password);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS - RESOURCE HAS BEEN CHANGED
                
    return true;
            } else {
                
    // FAILED - RESOURCE HAS NOT BEEN CHANGED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    getRes(){
            
    $ret $this->client->getAllResource($this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS
                
    for($i=1i<count($arr); $i++){
                    
    // MEMORIZZO IL VETTORE DELLE RISORSE DISPONIBILI
                    
    $res[$i-1] = $arr[$i];
                }
                return 
    $res;
            } else {
                
    // FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    getFiles($res){
            
    $ret $this->client->getAllFiles($res$this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS
                
    for($i=1i<count($arr); $i++){
                    
    /* Struttura del vettore $files:
                     *  - $files[nomeFile][tipoFile]
                     * Scorrere il vettore:
                     *   foreach(array_keys($files) as $key){
                     *       $nomeFile = $key;
                     *       $tipoFile = $files[$key]
                     *   }
                     */
                    
    $files[substr($arr[$i],2)] = $arr[$i]{0};
                }
                return 
    $files;
            } else {
                
    // FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    getFileInfo($file){
            
    $ret $this->client->getInfo($file$this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS
                
    for($i=1i<count($arr); $i++){
                    
    $tmp explode("$"$arr[$i]);
                    
    /* Struttura del vettore $fileInfo:
                     *  - $fileInfo[tipoInfo][valoreInfo]
                     * Scorrere il vettore:
                     *   foreach(array_keys($fileInfo) as $key){
                     *       $tipoInfo = $key;
                     *       $valoreInfo = $fileInfo[$key]
                     *   }
                     */
                    
    $fileInfo[$tmp[0]] = $tmp[1];
                }
                return 
    $fileInfo;
            } else {
                
    // FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    copy($src$dst){
            
    $ret $this->client->copy($src$dst$this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS - COPY DONE
                
    return true;
            } else {
                
    // FAILED - COPY FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    move($src$dst){
            
    $ret $this->client->move($src$dst$this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS - MOVE DONE
                
    return true;
            } else {
                
    // FAILED - MOVE FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    remove($file){
            
    $ret $this->client->delete($file$this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS - REMOVE DONE
                
    return true;
            } else {
                
    // FAILED - REMOVE FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        public function 
    rename($path$oldfilename$newfilename){
            
    $ret $this->client->rename($path$oldfilename$newfilename$this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS - RENAME DONE
                
    return true;
            } else {
                
    // FAILED - RENAME FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        
    // Si deve specificare come secondo parametro il path di destinazione del file scaricato
        
    public function download($src$dst){
            
    $ret $this->client->download($src$this->tkey);
            
    $arr explode("|"$ret);
            if (
    $arr[0]==1) {
                
    // SUCCESS
                /* La funzione download ritorna il nome del file 
                 * ed il file codificato base64 sottoforma di stringa.
                 */
                
    $filename $arr[1];
                
    $data     base64_decode($arr[2]);
                
    $handle   fopen($dst.'/'.$filename'w');
                if(
    $handle != false){
                    if(
    fputs($handle$datastrlen($data)) != false){
                        return 
    true;
                    } else {
                        
    // fputs FAILED
                        
    return false;
                    }
                } else {
                    
    // fopen FAILED
                    
    return false;
                }
            } else {
                
    // FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        
        
    // $file Ã¨ il path completo del file da uploadare
        
    public function upload($file$res){
            
    $handle fopen($file'r');
            if(
    $handle != false){
                
    $data fread($handlefilesize($file));
                if(
    $data != false){
                    
    $base64 base64_encode($data);
                    
    $ret $this->client->upload($base64basename($file), $res$this->tkey);
                    
    $arr explode("|"$ret);
                    if(
    $arr[0]==1){
                        
    //SUCCESS - FILE UPLOADED
                        
    return true;
                    } else {
                        
    //FAILED
                        
    $this->err_msg $arr[1];
                        return 
    false;
                    }
                } else {
                    
    // fread FAILED
                    
    return false;
                }
            } else {
                
    //fopen FAILED
                
    return false;
            }
        }
        
        public function 
    synchronize($dir1$dir2){
            
    $ret $this->client->synchronizeFolders($dir1$dir2$this->tkey);
            
    $arr explode("|"$ret);
            if(
    $arr[0]==1){
                
    //SUCCESS - FILE UPLOADED
                
    return true;
            } else {
                
    //FAILED
                
    $this->err_msg $arr[1];
                return 
    false;
            }
        }
        

    Dal messaggio di errore:
    Please ensure that the class definition "WebService_VFSiService_VFService" of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in C:\Programmi\Apache Software Foundation\Apache2.2\htdocs\VFSclient\VFSclient.cl ass.php on line 49
    mi sembra di capire che l'errore non stia nel richiamare il metodo login della classe VFSclient(), ma piuttosto si verifichi quando viene eseguita l'istruzione:
    Codice PHP:
    $ret $this->client->login($username,$password); 
    che si trova all'interno del metodo login della classe VFSclient.

    Non riesco però a capire cosa fare per risolvere questa situazione...

  4. #4
    Non credo che $_SESSION sia abilitata a contenere oggetti.

    Credo tu debba utilizzare serializa(), unserialize per memorizzare in $_SESSION degli oggetti
    Tali metodi convertono un oggetto in stringa e viceversa.

    Codice PHP:

    $obj 
    = new ObjClass();
    $_SESSION['oggetto'] = serialize($obj); 
    per compiere l'operazione invers:

    Codice PHP:

    $obj 
    unserialize($_SESSION['oggetto']);
    $obj->metodo1(); 
    Michele Castellucci
    Sviluppatore Web del Consorzio CottonBit
    Consorzio Cottonbit
    Risorse per la programmazione
    Dire Fare Programmare!

  5. #5
    Ho visto che sulla pagina di documentazione delle sessioni su php.net ci sono opinioni discordanti a riguardo della memorizzazione di oggetti in $_SESSION.
    La pagina a cui mi sto riferendo è http://www.php.net/manual/en/functio...n-register.php
    Secondo alcuni non c'è bisogno di serializzare, secondo altri invece sì...

    Fatto sta che ho provato a cambiare il mio codice serializzando e deserializzando esplicitamente l'oggetto che vado a memorizzare in $_SESSION:
    Codice PHP:
    require_once('VFSclient.class.php');
    session_start();

    if (isset(
    $_POST['submit'])) {
        switch (
    $_POST['submit']){
            case 
    'Entra':
                
    //LOGIN
                
    if(doLogin($_POST['username'], $_POST['password'])){
                    
    //LOGIN OK
                    //echo "Login OK - USER = ".$_POST['username']." - PASS = ".$_POST['password']. " - TKEY = ".$_SESSION['vfs']->tkey;
                    
    header("Location: main.php");
                } else {
                    
    //LOGIN ERROR
                    
    $_POST['errmsg'] = $_SESSION['vfs']->getErrorMessage();
                    include(
    'login.php');
                }
                break;
            case 
    'Registrati':
                
    //NUOVA REGISTRAZIONE
                
    include('register.php');
                break;
            case 
    'Procedi':
                if(
    doRegister($_POST['username'], $_POST['password'], $_POST['email'])){
                    
    // USERADD OK - NOW LOGIN
                    
    include('login.php');
                } else {
                    
    //FALLISCE LA USERADD
                
    }
                break;
        }
    } else {
        
    //PRIMA VOLTA CHE ACCEDO
        
    $vfs = new VFSClient();
        
    $svfs serialize($vfs);
        
    $_SESSION['vfs'] = $svfs;
        include(
    'login.php');
    }

    function 
    doLogin($username$password){
        
    $uvfs unserialize($_SESSION['vfs']);
        return 
    $uvfs->login($username,$password);
    }

    function 
    doRegister($username$password$email){
        
    $uvfs unserialize($_SESSION['vfs']);
        return 
    $uvfs->userAdd($username,$password,$email);

    Non so se ho tralasciato qualcosa, ma questo codice produce lo stesso risultato di quello precedente:
    Fatal error: VFSClient::login() [function.VFSClient-login]: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition &quot;WebService_VFSiService_VFService&quot; of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition in C:\Programmi\Apache Software Foundation\Apache2.2\htdocs\VFSclient\VFSclient.cl ass.php on line 49

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.