重定向到codeigniter中的另一个视图后的会话到期

时间:2017-03-23 09:59:37

标签: php codeigniter session codeigniter-3

我对codeigniter很新。这是我建立项目的第一个项目,我被困在登录注册模块中。注册功能正常工作,登录后我的目标是将用户重定向到管理页面,在那里他/她可以看到他的详细信息。但问题现在是在检查登录凭证并使用此代码设置用户会话

之后
$session_data = array(
                'username' => $result[0]->user_name,
                'email' => $result[0]->user_email,
                );
$this->session->set_userdata('loggedin', $session_data);
redirect('logincontroller/admin_dashboard');

但是我的会话没有设置。当我在我的管理仪表板视图中转储$this->session->userdata['loggedin']时,它返回空或不显示任何内容

以下是我的登录控制器的代码

<?php
class Logincontroller extends CI_Controller 
{
    public $outputData = array();
    public $loggedInUser;

    public function __construct() 
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->model('login_database');
        $this->load->helper('url');
        $this->load->database();
    }   
    public function index()
    {
        $this->outputData['pagetitle'] = "Login Form";
        $this->load->view('login_form', $this->outputData);
    }

    public function registrationform()
    {
        $this->outputData['pagetitle'] = "Registration Form";
        $this->load->view('registration_form', $this->outputData);
    }

    public function user_registration_process() 
    {

        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('email_value', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');
        if ($this->form_validation->run() == FALSE) 
        {
            $this->outputData['pagetitle'] = "Registration Form";
            $this->load->view('registration_form', $this->outputData);
        } 
        else 
        {
            $data = array(
            'user_name' => $this->input->post('username'),
            'user_email' => $this->input->post('email_value'),
            'user_password' => $this->input->post('password')
            );

            $result = $this->login_database->registration_insert($data);
            if ($result == TRUE) 
            {
                $this->outputData['pagetitle'] = "Login Form";
                $this->outputData['message_display'] = 'Registration Successfully !';
                $this->load->view('login_form', $this->outputData);
            } 
            else 
            {
                $this->outputData['message_display'] = 'Username already exist!';
                $this->outputData['pagetitle'] = "User Registration";
                $this->load->view('registration_form', $this->outputData);
            }
        }
    }

    public function login_process() 
    {
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required');

        if ($this->form_validation->run() == FALSE) 
        {
            if(isset($this->session->userdata['loggedin']))
            {
                    redirect('logincontroller/admin_dashboard');
            }
            else
            {
                    $this->outputData['pagetitle'] = "Login Form";
                    $this->load->view('login_form', $this->outputData);
            }
        } 
        else 
        {
            $data = array(
            'username' => $this->input->post('username'),
            'password' => $this->input->post('password')
            );
            $result = $this->login_database->login($data);
            if ($result == TRUE) 
            {
                $username = $this->input->post('username');
                $result = $this->login_database->read_user_information($username);
                if ($result != false) {
                $session_data = array(
                'username' => $result[0]->user_name,
                'email' => $result[0]->user_email,
                );
                $this->session->set_userdata('loggedin', $session_data);
                redirect('logincontroller/admin_dashboard');
                }
            } 
            else 
            {
                $this->outputData['pagetitle'] = "Login Form";
                $this->outputData['error_message'] = "Invalid Username or Password";
                $this->load->view('login_form', $this->outputData);
            }
        }
    }

    public function admin_dashboard()
    {
        $this->outputData['pagetitle'] = "Admin Dashboard";
        $this->load->view('admin_page', $this->outputData);
    }

    public function logout() 
    {
        $sess_array = array(
        'username' => ''
        );
        $this->session->unset_userdata('loggedin', $sess_array);
        $this->outputData['pagetitle'] = "Login Form";
        $this->outputData['message_display'] = 'Successfully Logout';
        $this->load->view('login_form', $this->outputData);
    }
}

我还使用了此链接session destroying in codeigniter after redirecting中指定的方法,但没有任何反应

我也使用php默认$_SESSION代替$this->session->set_userdata('loggedin', $session_data);但现在没有工作我也在网站上移动到http网站并在配置文件中设置设置这里也是配置文件的设置< / p>

$config['base_url'] = 'http://www.ratneshviop.esy.es/';
$config['index_page'] = 'index.php';
$config['encryption_key'] = '';
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['cookie_prefix']    = 'test';
$config['cookie_domain']    = 'http://ratneshviop.esy.es/';
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;
$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();

我在使用mysql支持的服务器上使用codeigniter 3.0.6

3 个答案:

答案 0 :(得分:2)

请参阅Codeigniter Document。在这份文件中说;使用此代码再增加一个变量集会话:

$newdata = array(
    'username'  => 'johndoe',
    'email'     => 'johndoe@some-site.com',
    'logged_in' => TRUE
);

$this->session->set_userdata($newdata);

所以你应该改变你的代码:

$this->session->set_userdata($session_data);

答案 1 :(得分:0)

<?php class Logincontroller extends CI_Controller 
{
public $outputData = array();
public $loggedInUser;

public function __construct() 
{
    parent::__construct();
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('session');
    $this->load->model('login_database');
    $this->load->helper('url');
    $this->load->database();
}   
public function index()
{
    $this->outputData['pagetitle'] = "Login Form";
    $this->load->view('login_form', $this->outputData);
}

public function registrationform()
{
    $this->outputData['pagetitle'] = "Registration Form";
    $this->load->view('registration_form', $this->outputData);
}

public function user_registration_process() 
{

    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
    $this->form_validation->set_rules('email_value', 'Email', 'trim|required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');
    if ($this->form_validation->run() == FALSE) 
    {
        $this->outputData['pagetitle'] = "Registration Form";
        $this->load->view('registration_form', $this->outputData);
    } 
    else 
    {
        $data = array(
        'user_name' => $this->input->post('username'),
        'user_email' => $this->input->post('email_value'),
        'user_password' => $this->input->post('password')
        );

        $result = $this->login_database->registration_insert($data);
        if ($result == TRUE) 
        {
            $this->outputData['pagetitle'] = "Login Form";
            $this->outputData['message_display'] = 'Registration Successfully !';
            $this->load->view('login_form', $this->outputData);
        } 
        else 
        {
            $this->outputData['message_display'] = 'Username already exist!';
            $this->outputData['pagetitle'] = "User Registration";
            $this->load->view('registration_form', $this->outputData);
        }
    }
}

public function login_process() 
{
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');

    if ($this->form_validation->run() == FALSE) 
    {
        if(isset($this->session->userdata['loggedin']))
        {
                redirect('logincontroller/admin_dashboard');
        }
        else
        {
                $this->outputData['pagetitle'] = "Login Form";
                $this->load->view('login_form', $this->outputData);
        }
    } 
    else 
    {
        $data = array(
        'username' => $this->input->post('username'),
        'password' => $this->input->post('password')
        );
        $result = $this->login_database->login($data);
        if ($result == TRUE) 
        {
            $username = $this->input->post('username');
            $result = $this->login_database->read_user_information($username);
            if ($result != false) {
            $session_data = array(
            'username' => $result[0]->user_name,
            'email' => $result[0]->user_email,
            );
            $this->session->set_userdata('loggedin', $session_data);
            redirect('logincontroller/admin_dashboard');
            }
        } 
        else 
        {
            $this->outputData['pagetitle'] = "Login Form";
            $this->outputData['error_message'] = "Invalid Username or Password";
            $this->load->view('login_form', $this->outputData);
        }
    }
}

public function admin_dashboard()
{
 if(!(isset($this->session->userdata['loggedin'])))
        {
                $this->outputData['pagetitle'] = "Login Form";
                $this->load->view('login_form', $this->outputData);
        }
    $this->outputData['pagetitle'] = "Admin Dashboard";
    $this->load->view('admin_page', $this->outputData);
}

public function logout() 
{
    $sess_array = array(
    'username' => ''
    );
    $this->session->unset_userdata('loggedin', $sess_array);
    $this->outputData['pagetitle'] = "Login Form";
    $this->outputData['message_display'] = 'Successfully Logout';
    $this->load->view('login_form', $this->outputData);
}
}

试试这段代码 对于要加载视图的每个方法,您必须检查会话

   if(!(isset($this->session->userdata['loggedin'])))
        {
                $this->outputData['pagetitle'] = "Login Form";
                $this->load->view('login_form', $this->outputData);
        }

答案 2 :(得分:0)

PHP 7升级-*已知的SESSION / COOKIE错误

此答案解决了已知的会话/ cookie错误-从PHP 5升级到PHP7时。

如果您的CodeIgniter版本为@ 3.1.0或更低版本-并且您要升级到PHP 7.1-您将需要更新CodeIgniter。 否则,将PHP 7.1更改为5.6即可正常工作。

$ this-> session-> set_userdata()有一个错误; -可能很烦人。一旦您重定向或访问站点结构中的另一个页面,它将覆盖您的会话。

或者您可以这样做

如果您在CI 3.x中工作,并且刚刚将服务器php版本升级到php 7.x

转到第281行的system / libraries / Session / session.php并替换ini_set('session.name',$ params ['cookie_name']);通过ini_set('session.id',$ params ['cookie_name']);