SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member codergurl's Avatar
    Join Date
    Jun 2003
    Location
    Houston, Tx, USA
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    custom session problem

    I wrote a custom session class for a weblog script I'm writing. I thought everything was going fine until I realized that when a I registered a session variable, it would unset right after I left the page. Or maybe I'm just loosing it? o_O

    class.session.php
    PHP Code:
    <?php

    class Session {
        var 
    $db;
        var 
    $timeout;

        function 
    Session(&$db) {
            
    $this->db $db;
            
    $this->timeout time() + get_cfg_var('session.gc_maxlifetime');
            
    session_set_save_handler(
            array(&
    $this,'_open'),
            array(&
    $this,'_close'),
            array(&
    $this,'_read'),
            array(&
    $this,'_write'),
            array(&
    $this,'_destroy'),
            array(&
    $this,'_gc'));
            
    session_start();
        }

        function 
    _open($path$name) {
            return 
    true;
        }

        function 
    _close() {
            return 
    true;
        }

        function 
    _read($id) {
            return 
    $this->db->get_results('SELECT * FROM '.$this->db->table_sessions.' WHERE ID = \''.$id.'\'');
        }

        function 
    _write($id$data) {
            
    $expire $this->timeout;
            
    $data $this->db->escape_string($data);
            
    $count =$this->db->get_one_result("SELECT COUNT(*) FROM ".$this->db->table_sessions." WHERE ID='$id'");
            if (
    $count == 1) {
                
    $this->db->query('UPDATE '.$this->db->table_sessions." SET expire='$expire', data='$data' WHERE ID='$id'");
            } else {
                
    $this->db->query('INSERT INTO '.$this->db->table_sessions." SET ID='$id', expire='$expire', data='$data'");
            }
            return 
    true;
        }

        function 
    _destroy($id) {
            
    $this->db->query('DELETE FROM '.$this->db->table_sessions.' WHERE ID = \''.$id.'\'');
            return 
    true;
        }

        function 
    _gc() {
            
    $this->db->query('DELETE FROM '.$this->db->table_sessions.' WHERE expire < '.$this->timeout);
            return 
    true;
        }

    }

    ?>
    login.php
    PHP Code:
    <?php
    require_once('minimal/blog.header.php');

    /* $lang['invalid_user_password'] = 'Invalid user/password. Try again.'; */

    if (isset($HTTP_POST_VARS['submit'])) {
        unset(
    $HTTP_SESSION_VARS['m_authordata']);
        unset(
    $HTTP_COOKIE_VARS['m_user']);
        unset(
    $HTTP_COOKIE_VARS['m_password']);

        
    /* extract variables */
        
    extract($HTTP_POST_VARS);

        if (isset(
    $rememberme)) {
            
    /* remember user login information for one week */
            
    setcookie('m_user'$usertime()+(60*60*24*7));
            
    setcookie('m_password'$passwordtime()+(60*60*24*7));
        }

        if (!empty(
    $user) && !empty($password)) {
            
    $user $db->escape_string($user);
            
    $password md5($db->escape_string($password));
            
    $authordata $db->get_results('SELECT * FROM '.$db->table_authors.' WHERE password=\''.$password.'\' AND user=\''.$user.'\'');

            if (
    $db->num_rows() == 1) {
                
    $HTTP_SESSION_VARS['m_authordata'] = $authordata;
                 echo 
    $HTTP_SESSION_VARS['m_authordata'];
            } else {
                
    $tpl->assign('error_message'$lang['invalid_user_password']);
                
    $tpl->display('admin/error_page.html');
            }
        } else {
            
    $tpl->assign('error_message'$lang['invalid_user_password']);
            
    $tpl->display('admin/error_page.html');
        }
    } else {
        if (isset(
    $HTTP_SESSION_VARS['m_authordata']) && is_array($HTTP_SESSION_VARS['m_authordata'])) {
            
    header('Location: '.$cpurl);
            exit;
        }
        
    $tpl->assign('m_user'$HTTP_COOKIE_VARS['m_user']);
        
    $tpl->assign('m_password'$HTTP_COOKIE_VARS['m_password']);
        
    $tpl->display('admin/login_form.html');
    }

    $db->debug();

    ?>
    PHP is almost as sexy as me!!

  2. #2
    SitePoint Member codergurl's Avatar
    Join Date
    Jun 2003
    Location
    Houston, Tx, USA
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed it myself. Someone can delete this.

    The _read function should have been:
    PHP Code:
        function _read($id) {
            return 
    $this->db->get_one_result('SELECT data FROM '.$this->db->table_sessions.' WHERE ID = \''.$id.'\'');
        } 
    PHP is almost as sexy as me!!


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
  •