Codeigniter PHP会话无法正常工作

时间:2017-11-10 15:51:24

标签: php codeigniter session codeigniter-3

我在CodeIgniter框架中的一个网站上工作,我试图为管理员和用户制作一些会话。但是当我尝试使用这些会话时,他们并没有正常工作。当我退出时,我收到的错误是我无法找到名为$_SESSION['user_level']的会话

此外,当我退出并点击登录表单视图页面时,我发现此错误而没有做任何事情:

A PHP Error was encountered

Severity: Notice

Message: A session had already been started - ignoring session_start()

Filename: controllers/Auth.php

Line Number: 80

Backtrace:

File: /home/ubuntu/workspace/application/controllers/Auth.php
Line: 80
Function: session_start

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once

这是我的控制器auth.php,具有用户登录,管理员登录和注销功能。 :

public function logout()
    {
        $this->session->sess_destroy();
        redirect("https://kadokado-ferran10.c9users.io/", "refresh");
    } 


    public function adminlogin() 
    {
        session_start();
        $this->form_validation->set_rules('admin_email', 'Admin_email', 'required');
            $this->form_validation->set_rules('admin_wachtwoord', 'Wdmin_wachtwoord', 'required|min_length[5]');
            if ($this->form_validation->run() == TRUE) {

            $admin_email = $_POST['admin_email'];
            $admin_wachtwoord = ($_POST['admin_wachtwoord']);

             //check admin in database
            $this->db->select('*');
            $this->db->from('admin');
            $this->db->where(array('admin_email'=>$admin_email, 'admin_wachtwoord' => $admin_wachtwoord));
            $query = $this->db->get();
            $admin = $query->row();

              //Als admin bestaat
            if($admin->admin_email) {

                $this->session->set_flashdata("success","U bent nu ingelogd als admin");

            //Maak admin sessions aan
            $_SESSION['user_logged'] = FALSE; 
            $_SESSION['admin_logged'] = TRUE;
            $_SESSION['is_logged'] = true.
            $_SESSION['user_level'] = 10;
            $_SESSION['admin_id'] = $admin->admin_id;
            $_SESSION['admin_email'] = $admin->admin_email;


             //link naar profiel pagina
            redirect("IndexController", "refresh");
            } else {

                $this->session->set_flashdata('error','Invalid email or password');
                //wanneer er een foutmelding is link weer naar de login pagina
                redirect("https://kadokado-ferran10.c9users.io/auth/adminlogin" , "refresh");
            }

            }
              //laad alle adminlogin views
        $this->load->view('templates/header');
        $this->load->view('adminviews/adminlogin');
        $this->load->view('templates/footer');
    }




    public function login()
    {
        //laad login view
            session_start();
            $this->form_validation->set_rules('email', 'Email', 'required');
            $this->form_validation->set_rules('wachtwoord', 'Wachtwoord', 'required|min_length[5]');
            if ($this->form_validation->run() == TRUE) {

            $email = $_POST['email'];
            $wachtwoord = ($_POST['wachtwoord']);


            //check gebruiker in database
            $this->db->select('*');
            $this->db->from('users');
            $this->db->where(array('email'=>$email, 'wachtwoord' => $wachtwoord));
            $query = $this->db->get();

            $user = $query->row();
            //Als gebruiker bestaat
            if($user->email) {

                $this->session->set_flashdata("success","U bent nu ingelogd");

            $_SESSION['user_logged'] = TRUE; 
            $_SESSION['admin_logged'] = FALSE;
            $_SESSION['is_logged'] = true.
            $_SESSION['user_level'] = 1; 
            $_SESSION['user_id'] = $user->user_id;
            $_SESSION['email'] = $user->email;
            $_SESSION['voornaam'] = $user->voornaam;
            $_SESSION['achternaam'] = $user->achternaam;
            $_SESSION['woonplaats'] = $user->woonplaats;
            $_SESSION['straat'] = $user->straat;
            $_SESSION['huisnummer'] = $user->huisnummer;
            $_SESSION['postcode'] = $user->postcode;
            $_SESSION['beschrijving'] = $user->beschrijving;
            $_SESSION['profiel_foto'] = $user->profiel_foto;




            //link naar profiel pagina
            redirect("user/profile", "refresh");
            } else {

                $this->session->set_flashdata('error','Invalid email or password');
                //wanneer er een foutmelding is link weer naar de login pagina
                redirect("https://kadokado-ferran10.c9users.io/auth/login" , "refresh");
            }



            }
                  //laad login view
                  $this->load->view('loginregistreer');
    }

这是我标题中的一些代码,用我创建的会话回显一些按钮:

<li> 
<?php 
if($_SESSION['user_level'] == 1): ?> 
<a class="l3" href="https://kadokado-ferran10.c9users.io/user/profile">Account</a> 
<?php endif;?> 
</li> 
<li> 
<?php if($_SESSION['user_level'] == 10):?> 
<a class="l3" href="https://kadokado-ferran10.c9users.io/IndexController">Admin dashboard</a> 
<?php endif; ?> 
</li> 
<li> 
<?php if($_SESSION['user_level'] <= 10):?> 
<a class="l4" href="https://kadokado-ferran10.c9users.io/auth/logout">Uitloggen</a> 
<?php else: ?> 
<a class="l5" href="https://kadokado-ferran10.c9users.io/auth/register">Registreren</a> 
</li><li> 
<a class="l6" href="https://kadokado-ferran10.c9users.io/auth/login">Inloggen</a> 
<?php endif ;?> 
</li>

当我以管理员身份登录时,它会显示标题中的正确按钮,但是当我退出时,我在每个包含标题的页面上收到此错误3次:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: user_level

Filename: templates/header.php

Line Number: 31

Backtrace:

File: /home/ubuntu/workspace/application/views/templates/header.php
Line: 31
Function: _error_handler

File: /home/ubuntu/workspace/application/views/home.php
Line: 1
Function: include_once

File: /home/ubuntu/workspace/application/controllers/Site.php
Line: 9
Function: view

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once

有谁知道我为什么会收到这些错误?

2 个答案:

答案 0 :(得分:1)

从代码中删除SCNCamera,codeigniter会话库负责处理它。

首先,您必须加载会话库。

session_start();

如果您已经加载了autoload配置,则无需再次加载。

在会话中设置数据

$this->load->library("session");

从会话中获取数据

$this->session->set_userdata("KEY","VALUE");

从这里阅读更多内容:

https://www.codeigniter.com/user_guide/libraries/sessions.html

答案 1 :(得分:0)

你做错了。

设置会话

$newdata = array(
        'user_logged'  => TRUE,
        'admin_logged'     => FALSE,
        'logged_in' => TRUE
        ......
);

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

<强> Retrivng

$this->session->userdata('admin_logged');

加载库

$this->load->library('session');

Read this Using the Session Class