Welcome Guest, Not a member yet? Register   Sign In
log out after set_flashdata
#1

[eluser]Ellli[/eluser]
Maybe im stupid, or maybe im just not seeing something but i need help :o)

I have simple method that check if user is logged in:

Code:
public function is_logged(){
        $result = $this->session->userdata('is_log');
        return  (bool) $result;
  }

in my controller methods if i need it i call it like that:

Code:
function show(){        
  if(!$this->model_user->is_logged()){
  redirect('admin/user/login');            
}else{
  //do stuff
}

and it works great when im just moving from page to page (most of them is checking if user is logged). The problem is when i want set flashdata and then redirect.

For example, i have method in cotroller:
Code:
function publish($lvl=0){
  if($this->input->post('submited')){
    //collecting $data
    $stuff = $this->model_pages->update($data);//do stuff, update table
      if($stuff){
        $this->session->set_flashdata('msg','ok');
      }else{
        $this->session->set_flashdata('err_msg','not ok');
      }
    redirect('admin/pages/show/');
  }else{ //if access directly without submit
    redirect('admin/pages/go/');
  }
}

even if i set_flashdata in:

Code:
}else{ //if access directly without submit
  $this->session->set_flashdata('err_msg','no direct access allowed!');
  redirect('admin/pages/go/');
}

the user is redirect to login form. i happens always when i use set_flashdata and redirect because for some reason this -> if(!$this->model_user->is_logged()){ is always true.
I tried echo result of function is_logged, just before redirect but after set_flashdata and the result is "true"...

To store data after login (if success) im using this method:
Code:
public function login_sess($user){
            $data = array(
                                                 'username'  => $user['nick'],
                                                 'user_id'     => $user['id'],
                                                 'is_log' => TRUE
                                         );
            $this->session->set_userdata($data);    }

When im not setting any flashdata everything works fine. And i spend all day trying to solve this stupid problem and because of that i dont want to make some workaround.

For my session config, im using database, the domain is ok, config is:
Code:
$config['sess_cookie_name']        = 'cbcms';
$config['sess_expiration']        = 7200;
$config['sess_encrypt_cookie']    = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']        = 'sessions';
$config['sess_match_ip']        = TRUE;
$config['sess_match_useragent']    = TRUE;
$config['sess_time_to_update']     = 300;
i tried every combination with match ip and useragent.

oh, and in database flashdata is stored, and i can see userdata and session_id is the same before redirect and after.

and if someone wanna ask, session class is in autoload. please please help. its driving me crazy..

ps. and if someone didnt notice CI is something new for me so in this ps i just wanna say hello :o)
#2

[eluser]LuckyFella73[/eluser]
Looks strange to me. Just guessing:
Did you try to set the second parameter o the redirect to 'refresh' ?
Code:
redirect('admin/user/login','refresh');
#3

[eluser]Ellli[/eluser]
yep. tried that. still not working.

as i see now its not only flashdata problem, but set_userdata cause same problem.

i also check echo result from diffrent place.

before redirect result is true
after redirect result is false

of course if i wanna set any data with session class. if im not using any session->set_... everything is ok, and result is always true... //edit always true when im logged in ;o) if i log out from another tab in my browser, im redirected to login page properly
#4

[eluser]OES[/eluser]
What a pain to keep adding that code. How about going about this by adding your own MY_Controller library and check if user is logged in globally for the user area (or any others).

So for example lets say all user actions happen within a user controller/user folder you could create the following library.

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Custom CI controller library extends the default CI controller library
* All controllers should extend this controller
*/

class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
    }
    
    $secure_areas = array(
        'user',
        'admin'
    );
    
    if(in_array($this->uri->segment(1), $secure_areas)):
        if(!$this->session->userdata('logged_in')):
            redirect();
        endif;
    endif;
}

Hope this helps

Lee
#5

[eluser]Ellli[/eluser]
[quote author="OES" date="1290224531"]What a pain to keep adding that code. How about going about this by adding your own MY_Controller library and check if user is logged in globally for the user area (or any others).

So for example lets say all user actions happen within a user controller/user folder you could create the following library.

Lee[/quote]

yep, thats a great idea, i always try to not duplicate same code over and over again.
and i already use that idea and added some other stuff to it like some part of layout :o)

but unfortunatly, still cant use set_flashadata or set_userdata.

Im using simple test with method.
Code:
function publish($lvl=0){
  if($this->input->post('submited')){
    //collecting $data
    $stuff = $this->model_pages->update($data);//do stuff, update table
      if($stuff){
        $this->session->set_flashdata('msg','ok');
      }else{
        $this->session->set_flashdata('err_msg','not ok');
      }
    redirect('admin/pages/show/');
  }else{ //if access directly without submit
    $this->session->set_flashdata('err_msg','no direct access allowed!');
    redirect('admin/pages/go/');
  }
}

when set_flashdata is commented everything works perfectly, but when i uncommented i end up logged out. any ideas?
#6

[eluser]OES[/eluser]
Try this

public function is_logged()
{
return ($this->session->userdata('is_log')) ? true : false;
}
#7

[eluser]Ellli[/eluser]
its almost the same as mine is_logged method. i was using that at the beggining. I tried that now too but with no luck, still not working. :o(
#8

[eluser]Ellli[/eluser]
so, i finally found what cause this problem.
It was unserialize function and special characters in my language. When im not using this characters its working.




Theme © iAndrew 2016 - Forum software by © MyBB