在codeigniter中未设置的会话无法正常工作

时间:2016-01-06 14:38:05

标签: php codeigniter session

我实际上无法在codeigniter中注销后重定向到同一页面。但是当我第一次点击注销时,它会重定向到同一页面,但会话未被删除...而不是当我点击退出会话时被打破了。这里可以追溯问题。

...控制器

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller
{
     public function __construct()
     {
       parent::__construct();
       $this->load->model('admin_model');

     }
     public function index()
      {


         $data['title']= 'Home';
         $this->load->view("registration_view.php", $data);

      }
      public function dashboard()//Page after sucessful Admin Login
      {


           $username= $this->input->post('username');
           $password= md5($this->input->post('password'));;


           $result = $this->admin_model->login($username,$password);
          if ($result == true) 
          {

             $this->load->view('top_menu.php');
             $this->load->view('dashboard.php');
          }
          else 
          {
                 $data = array(
                            'error_message' => 'Invalid Username or Password'
                               );
                $this->load->view('registration_view.php', $data);
          }


      }


     public function logout()//Admin Logout
     {
         $newdata = array(

                          'user_name'  =>'',

                          );
         $this->session->unset_userdata($newdata );
         $this->session->sess_destroy();
         $this->load->view('top_menu.php');
     }
     public function users()//User Lists
      {

          $this->load->view('top_menu.php');
          $this->data['users'] = $this->admin_model->getUsers();
          $this->load->view('list_user.php',$this->data);


      }
      public function delete_user()//Delete Users
      {


          $id=$this->input->post('id');
          $this->admin_model->delete_user($id);



      }
      public function delete_multiple()//Delete Multiple Users
      {

        //$id = $_POST['ids'];
        //echo $data=$id;
        //$this->admin_model->delete_multiple($id);

        $this->admin_model->delete_multiple($_POST['checkedValues']);

      }
      public function add_user()//Add User Form
      {


         $this->load->view('top_menu.php');
          $this->load->view('add_user.php');
      }
      public function insert_user()//Add User Form
      {

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

        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required');
        $this->form_validation->set_rules('contact', 'Contact', 'required');
         if ($this->form_validation->run() == FALSE)
        {
            $this->add_user();
        }
        else
        {
          $this->admin_model->insert_user();
          $this->users();   
        }


      }


}
 ?>

视图文件中的注销选项...

<!-- Start: page-top-outer -->
<div id="page-top-outer">    
<!-- Start: page-top -->
<div id="page-top">
    <!-- start logo -->
    <!--<div id="logo">
    <a href=""><img src="images/shared/logo.png" width="150" height="150" alt="" /></a>
    </div>-->
    <!-- end logo -->

    <div class="clear"></div>
</div>
<!-- End: page-top -->
</div>
<!-- End: page-top-outer -->
<div class="clear">&nbsp;</div>
<!--  start nav-outer-repeat................................................................................................. START -->
<div class="nav-outer-repeat"> 
<!--  start nav-outer -->
<div class="nav-outer">                 
        <!-- start nav-right -->
        <div id="nav-right">            
            <h4><?php echo anchor('admin/logout', 'Logout'); ?></h4>            
             <div id="welcome">

             <h2>Hello <?php echo $this->session->userdata('user_name'); ?>,</h2>
             </div>

            <div class="clear">&nbsp;</div>                 
        </div>
        <!-- end nav-right -->

        <!--  start nav -->
        <div class="nav">
            <h1 style="padding-top:7px;color:#900">AdWare</h1>
        </div>
        <!--  start nav -->                                       
</div>
<div class="clear"></div>
<nav class="hk-menu">
    <div class="nav-outer">
      <ul class="hk-ul select" >
      <?php $current_url=current_url();?>

            <li class="<?php if($current_url==base_url()."index.php/admin/dashboard") echo 'current';?>"><a href="list_category.php"><b>CATEGORY</b></a>
            </li>
            <li class="<?php if(( basename( $_SERVER["SCRIPT_FILENAME"] ) == 'list_video.php') || ( basename( $_SERVER["SCRIPT_FILENAME"] ) == 'create_user.php' ) ) echo 'current';?>"><a href="list_video.php"><b>VIDEOS</b></a>

            </li>
            <li class="<?php if($current_url==base_url()."index.php/admin/users")  echo 'current';?>"><?php echo anchor('admin/users', 'USERS',array('class' => 'li_style'));?>
            </li>
        </ul>
    </div>  
</nav>
<div class="clear"></div>
<!--  start nav-outer -->
</div>
<!--  start nav-outer-repeat................................................... END -->
 <div class="clear"></div>
<!-- start content-outer -->
<div id="content-outer">
<!-- start content -->
<div style="height:20px">&nbsp;</div>
<div id="content">
<style>


</style>

我想要的是&#34;退出&#34;单击会话应该被销毁,它应该重定向到没有会话

的视图页面

2 个答案:

答案 0 :(得分:1)

会话类在CI3中已更改,现在session_destroy不接受任何参数。试试$ this-&gt; session-&gt; sess_destroy();

来源:http://www.codeigniter.com/user_guide/libraries/sessions.html#destroying-a-session

答案 1 :(得分:1)

要破坏会话,您只需:

$this->session->sess_destroy();

我认为您应该尝试在此行之后重定向用户:

$this->session->sess_destroy();
redirect('site/login', 'refresh');