Welcome Guest, Not a member yet? Register   Sign In
made function to check if at least one of the site admins is connected
#1

[eluser]SPeed_FANat1c[/eluser]
Code:
function admin_connected()
    {
        //we find all usernames which are admins
        $query = $this->db->select('username')->where('admin',1)->get('membership');

        //now we check if any of them is connected
        foreach ($query->result() as $row)
        {
            
            $query2 = $this->db->query('SELECT user_data FROM ci_sessions WHERE (user_data LIKE "%\"'.$row->username.'\"%") AND (last_activity >'.(time()-7200).')');
            
            
            
            foreach ($query2->result() as $row2)
            {    
            
                
            
                //we have string e.g "{s:8:"username";s:5:"admin";s:12:"is_logged_in";s:1:"1";s:6:"offset";s:1:"0";}"
                //now we have to check if is_logged_in value is 1
                
                $pieces = explode(';',$row2->user_data);
                
                //now we find a piece which contaisn word is_logged_in, that will mean that the value is in then nex piece

                $word_found = FALSE;
                foreach($pieces as $piece)
                {
                    if($word_found)    //last piece was with word "is_logged_in", so the current piece is what we need
                    {    
                        $piece_with_value = $piece;
                        break;
                    }
                    if(strpos($piece,'"is_logged_in"') != FALSE)
                    {
                        $word_found = TRUE;        
                    }
                }
                
                //check $piece_with_value, 6th char shows if admin is connected
                
                if(substr($piece_with_value,5,1) == '1')
                {    
                    return TRUE;    //if is connected, we end job here
                }
                
            }
        }
    
        return FALSE;
    }

It works fine, but usernames can't be only numbers, like '12' because in session userdata there easily might be saved numbers, e.g. offset value. And usernames can't be words that are used in session userdata - e.g. offset.

So as you can see there is some retrinctions to usernames thats why it is not very perfect.
What do you think about it, maybe this is not good practice because session data should only be used with session functions and I should choose some other way in my next project?




Theme © iAndrew 2016 - Forum software by © MyBB