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

    PHP4, PHP5 e session_set_save_handler()

    sto impazzendo a costruire un gestore di sessioni personalizzato, ma se con php5 funziona benissimo, con php4 va a metà e non capisco cosa sia sbagliato...

    questa è la versione MOLTO tartassata della classe che stavo facendo.

    VA DETTO però, che la uso "dentro" un'altra classe figlia, da cui chiamo il metodo start...

    è forse una diversità tra i 2 php??? in php4 il metodo write non funziona, quindi non scrive le sessioni!...

    sapete aiutarmi?

    Codice PHP:
    <?php


        
    class session
        
    {
        
    #    var $id; #id di sessione #private (non serve, c'è già session_id)
        #    var $info = ''; #dati di sessione,togli! #private
            
    var $path ''#dir dei dati #protected
            
    var $name 'userid'#nome sessione ALFANUMERICO!(al posto di PHPSESSID) #private const (es. self::name)
            
    var $life '1 day'#massima inattività (gc) #private const (es. self::life)
            
    var $srcfile#handle per fopen o db sqlite (usare id???)
            
            // prototipo di file sessione: 569s7dd4567gd3f5g6ga725.userid
            
            
    function session() # __construct
            
    {
        
    #        ini_set('session.use_cookies', '1'); // ???
        #        ini_set('session.save_handler', 'files');
        #        ini_set('session.gc_maxlifetime', strtotime($this->life, 0));
                
            #    session_name($this->name); # parametro usato dalle funzioni
            #    session_save_path(''); # parametro usato dalle funzioni
                
                
            
    }
            
            function 
    start() #protected
            
    {
            
    #    ini_set('session.use_cookies', '1'); // ???
            #    ini_set('session.save_handler', 'files');
                
    ini_set('session.gc_maxlifetime'strtotime($this->life0));
                
                
    session_name($this->name); # parametro usato dalle funzioni
                
    session_save_path($this->path); # parametro usato dalle funzioni
                
                
    session_set_save_handler
                
    (    array(&$this'open')
                ,    array(&
    $this'close')
                ,    array(&
    $this'read')
                ,    array(&
    $this'write')
                ,    array(&
    $this'destroy')
                ,    array(&
    $this'gc')
                );
                
                
    register_shutdown_function('session_write_close');
                
                
    session_start();
            }
            
            
    #----- "Safe mode" for (un)registering session variables ------#
            
    function set($s_var$value null#public final
            
    {
                if (
    $value !== null)
                    
                    
    $_SESSION[$s_var] = $value;
                
                else unset(
    $_SESSION[$s_var]);
                
                return 
    true;
            }
            
            
    #----- "Safe mode" for reading session variables ------#
            
    function get($s_var ''#public final
            
    {
                if (!
    $s_var) return empty($_SESSION);
                
                return 
    array_key_exists($s_var$_SESSION) ? $_SESSION[$s_var] : null;
            }
            
            
            
            function 
    open($s_path$s_name#private final
            
    {
            
    #    $this->id = session_id();
                
    $this->srcfile sprintf('%s/%s.%s'$s_pathsession_id(), $s_name);
                echo 
    'FILE = '.$this->srcfile;
                
    # CREARE FILE SE NON ESISTE!
                // trova il più veloce: file_exists, file, is_file.
                
    if(!file_exists($this->srcfile))
                {
                    
    $f fopen($this->srcfile'w');
                    
    fclose($f);
                }
                
                
    # @return: TRUE | FALSE
                
    return true;
            }
            
            function 
    close() #private final
            
    {
                return 
    true;
                
                
    # @return: TRUE | FALSE
                # chiama gc()
            
    }
            
            function 
    read($s_id#private final
            
    {
                
    $s_data file_get_contents($this->srcfile);
                echo 
    'dati letti = '.$s_data;
                return 
    '';#($s_data) ? $s_data : '';
                
                # @return: datastring oppure ''
            
    }
            
            function 
    write($s_id$s_data#private final
            
    {
            
    #    echo 'dati scritti = '.$s_data;
                
                
    $handle fopen($this->srcfile'wb');
                
    fseek($handle0);
                
    fwrite($handle$s_data);
                
    fclose($handle);
                
            
    #    return file_put_contents($this->srcfile, $s_data) ? true : false;
                
                //scrive $data
                # @return: TRUE | FALSE
            
    }
            
            function 
    destroy($s_id#private final
            
    {
                return 
    unlink($this->srcfile);
                
                
    # @return: TRUE | FALSE
            
    }
            
            function 
    gc($s_maxlifetime#private final
            
    {/*
                foreach (glob($this->path.'*.'.$this->name) as $file)
                    
                    if (time()-fileatime($file) > $s_maxlifetime)
                        
                        if (!unlink($file)) { break; return false; }
                
                clearstatcache();
                */
                
    return true;
                
                
    # @return: TRUE | FALSE
            
    }
        }
    #echo 'ciao'.time().'
    ';
    $sess = &new session;

    $sess->start();
    #session_destroy();
    if(!empty($_GET['
    sess'])) $sess->set($_GET['sess'], $_GET['sessv']);
    #echo $sess->set('
    false', 'ciao');

    print_r($_SESSION);

    ?>

  2. #2
    il codice da provare (in php4, poi php5)

    Codice PHP:
    <?php

        
    class session
        
    {
            var 
    $path '';
            var 
    $name 'userid';
            var 
    $life '1 day';
            var 
    $srcfile;
            
            function 
    session()
            {
                
            }
            
            function 
    start()
            {
                
    ini_set('session.use_cookies''1');
                
    ini_set('session.gc_maxlifetime'strtotime($this->life0));
                
                
    ini_set('session.save_handler''user');
                
                
    session_name($this->name);
                
    session_save_path($this->path);
                
                
    session_set_save_handler
                
    (    array(&$this'open')
                ,    array(&
    $this'close')
                ,    array(&
    $this'read')
                ,    array(&
    $this'write')
                ,    array(&
    $this'destroy')
                ,    array(&
    $this'gc')
                );
                
                
    register_shutdown_function('session_write_close');
                
                
    session_start();
            }
            
            function 
    set($s_var$value null)
            {
                if (
    $value !== null)
                    
                    
    $_SESSION[$s_var] = $value;
                
                else unset(
    $_SESSION[$s_var]);
                
                return 
    true;
            }
            
            function 
    get($s_var '')
            {
                if (!
    $s_var) return empty($_SESSION);
                
                return 
    array_key_exists($s_var$_SESSION) ? $_SESSION[$s_var] : null;
            }
            
            function 
    open($s_path$s_name)
            {
                
    $this->srcfile sprintf('%s%s.%s'$s_pathsession_id(), $s_name);
                echo 
    'FILE = '.$this->srcfile;
                
                if(!
    file_exists($this->srcfile))
                {
                    
    $f fopen($this->srcfile'w');
                    
    fclose($f);
                }
                return 
    true;
            }
            
            function 
    close()
            {
                return 
    true;
            }
            
            function 
    read($s_id)
            {
                
    $s_data file_get_contents($this->srcfile);
                return 
    $s_data;
            }
            
            function 
    write($s_id$s_data)
            {
                
    $handle fopen($this->srcfile'ab');
                
    fwrite($handle$s_data);
                
    fclose($handle);
                return 
    true;
            }
            
            function 
    destroy($s_id)
            {
                @
    unlink($this->srcfile);
                return 
    true;
            }
            
            function 
    gc($s_maxlifetime)
            {
                foreach (
    glob($this->path.'*.'.$this->name) as $file)
                    
                    if (
    time()-fileatime($file) > $s_maxlifetime)
                        
                        if (!
    unlink($file)) { break; return false; }
                
                
    clearstatcache();
                
                return 
    true;
            }
        }

    $sess = &new session;

    $sess->start();
    #session_destroy();
    if(!empty($_GET['sess'])) $sess->set($_GET['sess'], $_GET['sessv']);
    #echo $sess->set('false', 'ciao');

    print_r($_SESSION);

    ?>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title></title>
    </head>
    <body>
    <form name="mioform" action="" method="get" accept-charset="utf-8" enctype="multipart/form-data">
    <input type="text" name="sess" value="">
    <input type="text" name="sessv" value=""><input type="submit" name="go"><input type="button" onclick="self.location.href = 'sestest.php'" value="altrapag">
    </form>
    </body>
    </html>

  3. #3

  4. #4
    nessuno lo sa?

  5. #5

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 © 2024 vBulletin Solutions, Inc. All rights reserved.