SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session Intercepting Filter weirdness

    Hey all, I'm experiencing unexpected behavior from a session IF.

    PHP Code:
    <?php

    class SessionFilter
    {
        public 
    $nextfilter;
        
        public function 
    __construct($nextfilter)
        {
            
    $this->nextfilter $nextfilter;
        }
        
        public function 
    execute()
        {
            if(!isset(
    $_SESSION['user_name'])) // <--- This part
            
    {
                
    session_start();
                
    $_SESSION['user_name'] = 'Anonymous';
                
    $_SESSION['alias'] = 'Anonymous';
                
    $_SESSION['plevel'] = '0';
                
    $_SESSION['loggedin'] = FALSE;
            } else {
                
    session_start();
            }
            
            
    $this->nextfilter->execute();
        }
    }

    ?>
    When I login in, the $_SESSION is set. When I go back to my front page, It doesn't seem to remember me. It has the $_SESSION['user_name'] set to Anonymous. It ran the filter even though the $_SESSION['user_name'] variable was already set, the exact opposite of what I wanted. However:

    PHP Code:
    <?php

    class SessionFilter
    {
        public 
    $nextfilter;
        
        public function 
    __construct($nextfilter)
        {
            
    $this->nextfilter $nextfilter;
        }
        
        public function 
    execute()
        {
            if(isset(
    $_SESSION['user_name'])) // <--- Notice isset rather than !isset
            
    {
                
    session_start();
                
    $_SESSION['user_name'] = 'Anonymous';
                
    $_SESSION['alias'] = 'Anonymous';
                
    $_SESSION['plevel'] = '0';
                
    $_SESSION['loggedin'] = FALSE;
            } else {
                
    session_start();
            }
            
            
    $this->nextfilter->execute();
        }
    }

    ?>
    Does what I want, but exactly the opposite of what I expected. Not only that, but when I go to my front page, the $_SESSION['user_name'] is the same as it was set when I logged in. That means that is isset, rather than !isset.

    Maybe I'm doing this the wrong way? I'm not very experienced with sessions, so there could be somehting I'm missing.

    Hope I was able to communicate my problem clearly.

  2. #2
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have to call session_start() before using $_SESSION.

  3. #3
    SitePoint Zealot sleepeasy's Avatar
    Join Date
    Sep 2003
    Location
    Bristol, UK
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    call session_start() before you start using $_SESSION:

    PHP Code:
    public function execute() { 
        if (
    session_id() == NULL) {
            
    session_start();
        }
        if(!isset(
    $_SESSION['user_name'])) { 
            
    $_SESSION['user_name'] = 'Anonymous'
            
    $_SESSION['alias'] = 'Anonymous'
            
    $_SESSION['plevel'] = '0'
            
    $_SESSION['loggedin'] = FALSE
        }
             
        
    $this->nextfilter->execute(); 

    Always open to question or ridicule

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the info guys.


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
  •