SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: [OOP + session_set_save_handler() ] Works on PHP5, not on PHP4!!!

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Bologna (Italy)
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation [OOP + session_set_save_handler() ] Works on PHP5, not on PHP4!!!

    I read everything about this topic (aslo on forum), but...

    The code below drives me crazy!!! It seems to work fine on php5, but on php4 it does not write session data (but seems to read).

    It's a custom session handling class.
    (This is the simplest version of it, made just to find the problem. Inside my application I use it throu a child class with some more custom methods...)

    WHAT'S WRONG??? Did I forget to set something? Try yourself first in php5, then in php4.

    Please give me a hand.

    PHP Code:
    <?php

        
    class session
        
    {
            var 
    $path '_temp/';
            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'wb');
                
    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>
    ciao,
    jack.
    Last edited by mdsjack; Feb 9, 2005 at 05:47.

  2. #2
    SitePoint Enthusiast sinatosk's Avatar
    Join Date
    Jul 2003
    Location
    PHP World
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    might be wrong but by the looks of your code your not defining a correct path

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Bologna (Italy)
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I edited, I forgot the path in that sample code, but it points to a subfolder of the script.

    What's wrong now?

  4. #4
    SitePoint Enthusiast sinatosk's Avatar
    Join Date
    Jul 2003
    Location
    PHP World
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't know sorry. I've always found PHP built in session handler to be damn fussy at times.... so I just programme my own custom session handler from scratch...

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Bologna (Italy)
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I prefer this method because using php session cookie to pass values is easier than writing a SID on each link...

    If you know different ways, tell... :|

  6. #6
    SitePoint Member
    Join Date
    Mar 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb RE: Open original session file

    I have fighting with the session_set_save_handler function (I was died)

    I found an bug (or not bug), my problem it was that never save the session file in my folder sesion location.

    You must open and read the original session file, and now you can open other session file and write in this.

    This is my code:

    PHP Code:
    <?php
    class Sesion {
        var 
    $tmp;
        var 
    $real;
        var 
    $fp false;
        var 
    $fpr false;
        var 
    $tempo 0;

        function 
    Sesion () {
            
    $this->tempo 60*60;

            
    ini_set('session.use_cookies''1');
            
    ini_set('session.gc_maxlifetime'$this->tempo);
            
    ini_set('session.save_handler''user');
        }

        function 
    inicia () {
            
    session_set_save_handler(
                array(&
    $this"abrir"),
                array(&
    $this"pechar"),
                array(&
    $this"ler"),
                array(&
    $this"escribir"),
                array(&
    $this"destruir"),
                array(&
    $this"caducar")
            );
        }

        function 
    abrir ($save_path$session_name) {
            global 
    $paths;

            
    $this->tmp $save_path."/sess_".session_id();
            
    $this->real $paths["session"].base64_encode(session_id());

            return (
    true);
        }

        function 
    pechar () {
            
    flock($this->fprLOCK_UN);
            
    fclose($this->fpr);

            
    flock($this->fpLOCK_UN);
            
    fclose($this->fp);

            return (
    true);
        }

        function 
    ler ($id) {
            if (
    $this->fpr = @fopen($this->real"r")) {
                
    flock($this->fprLOCK_EX);
                
    $datos = @fread($this->fpr, @filesize($this->real));
            }

            if (
    $this->fp = @fopen($this->tmp"r")) {
                
    flock($this->fpLOCK_EX);
                
    $sess_data = @fread($this->fp, @filesize($this->tmp));
                return 
    $datos;
            } else {
                return (
    ""); // Must return "" here.
            
    }
        }

        function 
    escribir ($id$sess_data) {
            if (
    $this->fpr = @fopen($this->real"w")) {
                
    flock($this->fprLOCK_EX);
                @
    fwrite($this->fpr$sess_data);
            }

            if (
    $this->fp = @fopen($this->tmp"w")) {
                
    flock($this->fpLOCK_EX);
                return (@
    fwrite($this->fp"1"));
            } else {
                return (
    false);
            }
        }

        function 
    destruir ($id) {
            
    unlink($this->real);
            return 
    unlink($this->tmp);
        }

        function 
    caducar ($maxlifetime) {
            
    $dir dirname($this->real);
            
    $dp opendir($dir);

            while (
    $cada readdir($dp)) {
                if (
    $cada != "."
                 
    && $cada != ".."
                
    && time()-fileatime($dir."/".$cada) > $maxlifetime) {
                    @
    unlink($dir."/".$cada);
                }
            }

            
    closedir($dp);

            
    $dir dirname($this->tmp);
            
    $dp opendir($dir);

            while (
    $cada readdir($dp)) {
                    echo 
    time()."-".fileatime($dir."/".$cada)." > $maxlifetime == ".(time()-fileatime($dir."/".$cada))."\n<br>\n";
                if (
    $cada != "."
                 
    && $cada != ".."
                
    && ereg("^sess_"$cada)
                && 
    time()-fileatime($dir."/".$cada) > $maxlifetime) {
                    echo 
    time()."-".fileatime($dir."/".$cada)." > $maxlifetime";
                    @
    unlink($dir.$cada);
                }
            }

            
    closedir($dp);

            return 
    true;
        }
    }

    $sesion = &new Sesion();
    $sesion->inicia();
    ?>
    And now It's work. I save a false data in the PHP session and a real data in my file session.

    if I not open the original file session or change the path or name, it not work.

    This code it's a piece of a real and more extended code.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •