SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)

    Session not writting before redirect.

    This is driving me mad. For some reason this session just won't write. I've looked over the this and have tried the solutions presented and its still just not functioning. If you need the entire class I'll post that but this is the snippet I'm dealing with. For some reason if I place it outside my controller without a redirect it writes just fine.

    PHP Code:
    $_SESSION['id'] = $user->id;
    $_SESSION['access'] = $user->access;
    $_SESSION['name'] = $user->name;
    session_write_close();
    header('Location: '.$this->path->makeUrl());
    exit(); 

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Has session_start() been called before this code?

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    lol, of course.

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This should do it according to the user comments for session_write_close() on the manual.

    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['id'] = $user->id;
    $_SESSION['access'] = $user->access;
    $_SESSION['name'] = $user->name;
    session_regenerate_id(true);
    header('Location: '.$this->path->makeUrl());
    exit(); 
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    yep, tried that no luck.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Custom save handler?

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    yes

    I don't think there is anything wrong with it though becasue it was functioning fine before I moved the posted code outside my initiation file and into a class.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post it, it might spark a thought.

    I would modify each method to append a microtime(true) to a log file(absolute path). This might reveal an unexpected call php is performing.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    <?php
    class SessionManager {
        
        const 
    table            =        'sessions';
        
        const 
    id             =         'id';
        const 
    data            =         'data';
        const 
    expiration    =        'expiration';

        protected 
    $_life;
        protected 
    $_db;

        public function 
    __construct(PDO $pDb) {
        
            
    $this->_life get_cfg_var('session.gc_maxlifetime');
            
    $this->_db $pDb;
            
            
    $this->_init();
        
        }
        
        protected function 
    _init() {
        
            
    $this->_register();
        
        }
        
        protected function 
    _register() {
        
            
    session_set_save_handler(
                array(
    $this,'open')
                ,array(
    $this,'close')
                ,array(
    $this,'read')
                ,array(
    $this,'write')
                ,array(
    $this,'destroy')
                ,array(
    $this,'gc')
            );
        
        }
        
        public function 
    open($pSavePath,$pSessionName) {
        
            global 
    $sessSavePath;
            
            
    $sessSavePath $pSavePath;
            
            return 
    true;
            
        }
        
        public function 
    close() {
        
            return 
    true;
        
        }
        
        public function 
    read($pId) {
        
            
    $data '';        
            
    $time time();
            
            
    $sql 'SELECT t0.'.self::id.' AS id, t0.'.self::data.' AS data, t0,'.self::expiration.' AS expiration FROM '.self::table.' AS t0 WHERE t0.'.self::id.'= :id AND t0.'.self::expiration.' > :expiration LIMIT 1';
            
            if(
    $stmt $this->_db->prepare($sql)) {
            
                
    $stmt->bindParam(':id',$pId,PDO::PARAM_STR);
                
    $stmt->bindParam(':expiration',$time,PDO::PARAM_INT);
                
                if(
    $stmt->execute()) {
                
                    if(
    $row $stmt->fetch(PDO::FETCH_ASSOC)) {
                        
                        
    $data $row['data'];
                    
                    }
                
                }
            
            }
            
            return 
    $data;
        
        }
        
        public function 
    write($pId,$pData) {
        
            
    $time time() + $this->_life;
            
            
    $sql 'REPLACE '.self::table.' ('.self::id.','.self::data.','.self::expiration.') VALUES (:id,:data,:expiration)';
            
            if(
    $stmt $this->_db->prepare($sql)) {
            
                
    $stmt->bindParam(':id',$pId,PDO::PARAM_STR);
                
    $stmt->bindParam(':data',$pData,PDO::PARAM_STR);
                
    $stmt->bindParam(':expiration',$time,PDO::PARAM_INT);
                
                if(
    $stmt->execute()) {
                
                    return 
    true;
                
                }
            
            }
        
        }
        
        public function 
    destroy($pId) {
        
            
    $sql 'DELETE FROM '.self::table.' WHERE '.self::id.' = :id LIMIT 1';
            
            if(
    $stmt $this->_db->prepare($sql)) {
            
                
    $stmt->bindParam(':id',$pId,PDO::PARAM_STR);
                
                if(
    $stmt->execute()) {
                    
                    return 
    true;
                
                }
            
            }
        
        }
        
        public function 
    gc() {
        
            
    $sql 'DELETE FROM '.self::table.' WHERE '.self::expiration.' < UNIX_TIMESTAMP()';
            
            if(
    $stmt $this->_db->prepare($sql)) {
            
                
    $stmt->execute();
            
            }
            
            return 
    true;
        
        }
        
        

    }
    ?>

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    This is the class which the code resides. not all that sure it makes a difference but here it is anyway. The method specific to the session stuff is _handleLogin().

    PHP Code:
    <?php
    abstract class AppController {
        
        
    // keyword to trigger non default method as driver
        
    const actionPathKeyword 'action';

        protected 
    $view;
        protected 
    $action;
        protected 
    $user;
        protected 
    $path;
        protected 
    $post;
        
        public function 
    __construct(Identifiable $pUser,Pathway $pPath) {
            
            
    $this->user $pUser;
            
    $this->path $pPath;
            
            
    $this->view $this->_generateView();
            
            if(
    $this->user->isClient()===false) {
                
    $this->_handleLogin();
            }
            
            
    $this->_init();
            
            
    $this->view->render();
        
        }
        
        protected function 
    _init() {
        
            
    // path and client is special and os passed onto the view
            
    $this->view->setVar('path',$this->path);
            
    $this->view->setVar('client',$this->user);
            
            
    // action is special keyword
            
    if($this->path->hasOption(self::actionPathKeyword)===true && $this->path->isDefined(self::actionPathKeyword)===true) {
                
    $this->action $this->path->getOption(self::actionPathKeyword);
            }
            
            
    $methods get_class_methods($this);
            if(!empty(
    $this->action) && in_array($this->action,$methods)) {
                
    $action $this->action;
                
    $this->$action();
            } else {
                
    $this->_defaultAction();
            }
        
        }
        
        private function 
    _handleLogin() {
                
            
    $login = new LoginForm();
            
    $form $this->_makeLoginForm();
            
    $login->setVar('form',$form);
            
    $this->view->setVar('login-form',$login);
                
            if(isset(
    $_POST['login'])) {
                
                
    $name $_POST['login']['name'];
                
    $pwd $_POST['login']['pwd'];
                
    $remember = isset($_POST['login']['remember'])?1:0;
                    
                
    $form->addValue('user_name',$name);
                
    $form->addValue('pwd',$pwd);
                if(
    $remember==1$form->addChecked('remember_me');
                    
                if(empty(
    $name)) {
                    
    $form->addFeedback('user_name','User name is required');    
                    
    $form->addInvalid('user_name');
                }
                    
                if(empty(
    $pwd)) {
                    
    $form->addFeedback('pwd','Password is required');    
                    
    $form->addInvalid('pwd');                            
                }
                    
                if(!empty(
    $pwd) && !empty($pwd)) {
                    
                    
    $user User::find(
                        
    'one'
                        
    ,array(
                            
    'name'=>$name
                            
    ,'pwd'=>$pwd
                            
    ,'limit'=>1
                        
    )
                    );
                    
                    if(!
    is_null($user)) {

                        
    $_SESSION['id'] = $user->id;
                        
    $_SESSION['access'] = $user->access;
                        
    $_SESSION['name'] = $user->name;
                        
    session_write_close();
                        
    header('Location: '.$this->path->makeUrl());
                        exit(); 
                        
                    } else {
                    
                        
    $form->addInvalid('pwd');                    
                        
    $form->addFeedback('pwd','Incorrect password');    
                        
                        
    $form->addInvalid('user_name');
                        
    $form->addFeedback('user_name','Incorrect user name');    
                    
                    }
                    
                }
                
            }
        
        }
        
        private function 
    _makeLoginForm() {
        
            
    $f = new Form();

            
    $f->addValue('action',$this->path->makeUrl());
            
    $f->addValue('method','post');

            
    $f->addName('user_name','login[name]');
            
    $f->addName('pwd','login[pwd]');
            
    $f->addName('remember_me','login[remember]');
            
    $f->addName('submit','submit');
            
    $f->addName('reset','reset');

            
    $f->addRequired('user_name','pwd');

            
    $f->addType('user_name','text');
            
    $f->addType('pwd','password');
            
    $f->addType('remember_me','checkbox');
            
    $f->addType('submit','submit');
            
    $f->addType('reset','reset');

            
    $f->addMax('user_name','15');
            
    $f->addMax('pwd','10');

            
    $f->addValue('submit','Login');
            
    $f->addValue('reset','Reset Form');    
            
            return 
    $f;        
        
        
        }
        
        abstract protected function 
    _generateView();
        abstract protected function 
    _defaultAction();

    }
    ?>

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    huh… session_start() is called in the initiation script which I didn't post.

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this a real typo in the read()'s sql?
    PHP Code:
    $sql '<snip> t0.'.self::data.' AS data, t0,      <snip>';
    //                                        ^^^ 

  13. #13
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,052
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    yep, that was it. Thanks… always the little things.


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
  •