Codeigniter会话故障排除

时间:2019-01-05 09:10:34

标签: php sqlite codeigniter

Codeigniter 会话在本地运行良好,但在 托管服务器 上无效。需要帮助。 谢谢 Config.php

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 0;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 3600;
$config['sess_regenerate_destroy'] = FALSE;
$config['cookie_prefix']    = ""; 

$config['cookie_domain']    = ""; 
$config['cookie_path']  = "/"; 
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;

Autoload.php

$autoload['libraries'] = array('session', 'form_validation', 'database', 'upload', 'email', 'encryption');

Home.php

public function konektion(){
    if(isset($_POST['ok']) && isset($_POST['email'])!=null && isset($_POST['passwd']) != null){
        $this->form_validation->set_rules('email','Email','required|valid_email');
        $this->form_validation->set_rules('passwd','Mot De Passe','required|min_length[4]');
        if($this->form_validation->run() == TRUE){
            $email = stripslashes(strtolower(($this->input->post('email'))));
            $passwd = stripslashes($this->input->post('passwd'));
            $salt = '$'.base64_encode('simo').'$'. do_hash($passwd, 'md5');
            $tab = array('login' => $email, 'password' => $salt);
            $tab1 = $this->UserModel->findOneUser($tab);
            if ($tab1['nbre'] == 1) {
                $sessiondata = array(
                        'userid'  => $tab1['iduser'],
                        'username'  => $tab1['nom'],
                        'status'  => $tab1['status'],
                        'login'  => $tab1['login'],
                        'role'  => $tab1['role'],
                        'logged_in' => TRUE
                );
                $this->load->library('session');
                $this->session->set_userdata($sessiondata);

                $data['page_title'] = 'Envol | Admin';
                $this->load->view('admin/index', $data);
            }
            else {
                    $data['msg']  = '<b>Invalid Email Or Password Entered</b>';
                    $data['page_title'] = "Admin Login";
                    $this->load->view("admin/login", $data);
                }

        }else{
            $this->index();
        }
    }else{
        $this->index();
    }
}

这就是我喜欢的代码和配置。 尝试在任何控制器或viex中访问$_session['userid]时,我收到的消息是未定义索引:userid

3 个答案:

答案 0 :(得分:0)

您可以检查您的Codeigniter设置:

application / config / autoload.php

$autoload['libraries'] = array('database', 'session', 'user_agent');

以及 application / config / config.php

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_save_path'] = 'ci_sessions';

继续努力!

答案 1 :(得分:0)

我认为我是解决方案。

  1. 转到system/libraries/Session/Session.php
  2. 搜索session_start()并通过添加//对其进行评论。
  3. 安全为王 下下降到316行,并注释以下代码:ini_set()

    // ini_set('session.use_trans_sid',0); //ini_set('session.use_strict_mode',1); //ini_set('session.use_cookies',1); //ini_set('session.use_only_cookies',1);

  4. 然后转到CodeIgniter的主index.php(根index.php

  5. 在顶部添加session_start()
  6. session_start()下面添加在Session.php

    中注释的以下行

    ini_set('session.use_strict_mode',1);

    ini_set('session.use_cookies',1); ini_set('session.use_only_cookies',1); ini_set('session.hash_function',1); ini_set('session.hash_bits_per_character',4);

我从https://stackoverrun.com/fr/q/12557092那里得到

答案 2 :(得分:0)

在这里设置会话。

if ($tab1['nbre'] == 1) {
                $sessiondata = array(
                        'userid'  => $tab1['iduser'],
                        'username'  => $tab1['nom'],
                        'status'  => $tab1['status'],
                        'login'  => $tab1['login'],
                        'role'  => $tab1['role'],
                        'logged_in' => TRUE
                );
                $this->load->library('session');
                $this->session->set_userdata($sessiondata);

                $data['page_title'] = 'Envol | Admin';
                $this->load->view('admin/index', $data);
            }

问题在于您的数据可能是因为您没有在服务器上导出数据库..在事情上做事

if ($tab1['nbre'] == 1) {
 $sessiondata = array(
                            'userid'  => $tab1['iduser'],
                            'username'  => $tab1['nom'],
                            'status'  => $tab1['status'],
                            'login'  => $tab1['login'],
                            'role'  => $tab1['role'],
                            'logged_in' => TRUE
                    );
                    $this->load->library('session');
                    $this->session->set_userdata($sessiondata);
 echo 'is one  <pre>';
print_r($_SESSION);
EXIT;
 $sessiondata = array(
                            'userid'  => $tab1['iduser'],
                            'username'  => $tab1['nom'],
                            'status'  => $tab1['status'],
                            'login'  => $tab1['login'],
                            'role'  => $tab1['role'],
                            'logged_in' => TRUE
                    );
                    $this->load->library('session');
                    $this->session->set_userdata($sessiondata);
} else {
echo 'is '.$tab1['nbre'].' ... unable to set session;  
EXIT;
}