1

我的登录会话有问题。

这是我迄今为止尝试过的:

User.php 控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller{
public function __construct()
{
    parent::__construct();
    $this->load->model('user_model');
}
public function index()
{
    //if(($this->session->userdata('logged_in') == TRUE))
    if(($this->session->userdata('username') != "" ))
    {
        $this->welcome();
    }
    else {
        $data['title']= 'Home';
        $this->load->view('include/header',$data);
        $this->load->view("frontpage", $data);
        $this->load->view('include/footer',$data);
    }
}
public function welcome()
{
    $data['title']= 'Welcome';
    $this->load->view('include/header',$data);
    $this->load->view('include/navbar',$data);
    $this->load->view('welcome_view', $data);
    $this->load->view('include/sidebar',$data);
    $this->load->view('include/footer',$data);
}

public function login()
{
    $username = $this->input->post('username');
    $password = $this->input->post('password');
    //md5($this->input->post('password'));
    //$this->input->post('password');  

    $result = $this->user_model->login($username, $password);

    if($result) 
        $this->welcome();
    else       
        $this->index();

}
public function thank()
{
    $data['title']= 'Thank';
    $this->load->view('include/header',$data);
    $this->load->view('thank_view.php', $data);
    $this->load->view('include/footer',$data);
}
public function registration()
{
    $this->load->library('form_validation');
    // field name, error message, validation rules
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
    $this->form_validation->set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');

    if($this->form_validation->run() == FALSE)
    {
        $this->index();
    }
    else
    {
        $this->user_model->add_user();
        $this->thank();
    }
}
public function logout()
{
    $newdata = array(
    //'user_id'   =>'',
    'username'  =>'',
    'logged_in' => FALSE
    ); 

    $this->session->unset_userdata($newdata);
    $this->session->sess_destroy();
    $this->index();
}
 }
?>

user_model.php 模型

<?php
class User_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    function login($username, $password)
    {
        $this->db->where("username", $username);
        $this->db->where("password", md5($password));

        $query = $this->db->get("users");
        if($query->num_rows()>0)
        {
            foreach($query->result() as $rows)
            {
                //add all data to session
                $newdata = array(
                        //'user_id'         => $rows->id,
                        'username' => $rows->username,
                        'logged_in' => TRUE
                   ); 

            }
                $this->session->set_userdata($newdata);
                return true;            
        }
        return false;
    }

    public function add_user()
    {
        $data=array(
            'username'=> $this->input->post('username'),
            'password'=> $this->input->post('password')// md5($this->input->post('password'))
            );
        $this->db->insert('users',$data);
    }
}
?>

frontpage.php 视图

     <div class="container">
   <div class="jumbotron">
     <?php 
     $attributes = array('class' => 'form-signin');
     echo form_open(base_url('login'), $attributes); ?>
      <h2 class="form-signin-heading">VMS Login System</h2>
        <input type="username" name="username" class="form-control" placeholder="Username" required autofocus>
        <input type="password" name="password" class="form-control" placeholder="Password" required>
        <button class="btn btn-primary" type="submit">Login</button>
        <button class="btn btn-primary" type="reset">Cancel</button> <!-- btn btn-lg btn-primary btn-block -->
     <?php echo form_close(); ?>

   </div>
</div>

欢迎视图.php

<div class="container">

      <div class="row row-offcanvas row-offcanvas-right">

        <div class="col-xs-12 col-sm-9">
          <p class="pull-right visible-xs">
            <button type="button" class="btn btn-primary btn-xs" data-toggle="offcanvas">Toggle Sidebar</button>
          </p>
          <div class="jumbotron">
            <h1>Van Management System</h1>
            <p></p>
          </div>
          <div class="row">
            <div class="col-6 col-sm-6 col-lg-4">
              <h2>What's New</h2>
              <p>Sample Only</p>
              <p><a class="btn btn-default" href="#" role="button">View details »</a></p>
            </div><!--/span-->
            <div class="col-6 col-sm-6 col-lg-4">
              <h2>Charts and Graphs</h2>
              <p>Check out sample</p>
              <p><a class="btn btn-default" href="#" role="button">View details »</a></p>
            </div><!--/span-->
            <div class="col-6 col-sm-6 col-lg-4">
              <h2>Announcements</h2>
              <p>Announcements</p>
              <p><a class="btn btn-default" href="#" role="button">View details »</a></p>
            </div><!--/span-->
          </div><!--/row-->
        </div><!--/span-->

问题是,每当我退出系统并返回登录页面时,我总是可以返回欢迎页面,我的会话没有被破坏。我想要的是,当我登录时,我当然在欢迎页面。当我返回登录页面时,系统应该将我重定向到欢迎视图,并且如果我当前已登录,系统将不允许我进入登录页面。有什么想法吗?我试过了,但什么也没发生 :( 非常感谢帮助。

更新: 这是我加载欢迎页面的方式:

     public function index()
{
    //if(($this->session->userdata('logged_in') == TRUE))
    if(($this->session->userdata('username') != "" ))
    {
        $this->welcome();
    }
    else {
        $data['title']= 'Home';
        $this->load->view('include/header',$data);
        $this->load->view("frontpage", $data);
        $this->load->view('include/footer',$data);
    }
}
public function welcome()
{
    $data['title']= 'Welcome';
    $this->load->view('include/header',$data);
    $this->load->view('include/navbar',$data);
    $this->load->view('welcome_view', $data);
    $this->load->view('include/sidebar',$data);
    $this->load->view('include/footer',$data);
}
4

1 回答 1

0

我猜你试试这个

public function logout()
{
    $newdata = array(
      //'user_id'   =>'',
      'username'  =>'',
      'logged_in' => ''
     ); 

    $this->session->unset_userdata($newdata);
    $this->session->sess_destroy();
    $this->index(); 
} 

您的索引函数 User.php 控制器

public function index()
{
  if(($this->session->userdata('logged_in') == TRUE))
  {
    if(($this->session->userdata('username') != "" ))
    {
        $this->welcome();
    }
  }
    else {
        $data['title']= 'Home';
        $this->load->view('include/header',$data);
        $this->load->view("frontpage", $data);
        $this->load->view('include/footer',$data);
      }
}

欢迎功能

public function welcome()
{
  if(($this->session->userdata('logged_in') == TRUE))
  {
    if(($this->session->userdata('username') != "" ))
    {
        $data['title']= 'Welcome';
        $this->load->view('include/header',$data);
        $this->load->view('include/navbar',$data);
        $this->load->view('welcome_view', $data);
        $this->load->view('include/sidebar',$data);
        $this->load->view('include/footer',$data);
    }
  }
  else
  {
      $this->index();
  }  
}
于 2013-11-15T05:13:37.233 回答