0

我在下面的代码中使用了codeigniter代码,我的目标是如果用户登录到一个页面并空闲5分钟,它应该注销并重定向到登录页面。请任何人帮助我解决这个问题。

控制器:登录.php

<?php

class Login extends CI_Controller {

    function index()
    {
        $data['main_content'] = 'login_form';
        $this->load->view('includes/template', $data);      
    }

    function validate_credentials()
    {       
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();

        if($query) // if the user's credentials validated...
        {
            $data = array(
                'username' => $this->input->post('username'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('site1/members_area');
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }   

    function signup()
    {
        $data['main_content'] = 'signup_form';
        $this->load->view('includes/template', $data);
    }

    function create_member()
    {
        $this->load->library('form_validation');

        // field name, error message, validation rules
        $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('signup_form');
        }

        else
        {           
            $this->load->model('membership_model');

            if($query = $this->membership_model->create_member())
            {
                $data['main_content'] = 'signup_successful';
                $this->load->view('includes/template', $data);
            }
            else
            {
                $this->load->view('signup_form');           
            }
        }

    }

    function logout()
    {
        $this->session->sess_destroy();
        $this->index();
    }

}

站点1.php

<?php

class Site1 extends CI_Controller 
{


    function members_area()
    {
        $this->load->view('homepage_view');


    }

    function another_page() // just for sample
    {
        echo 'good. you\'re logged in.';
    }

    function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    
            die();      
            //$this->load->view('login_form');
        }       
    }   

}

模型:membership_model

<?php

class Membership_model extends CI_Model {

    function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('membership');

        if($query->num_rows == 1)
        {
            return true;
        }

    }

    function create_member()
    {

        $new_member_insert_data = array(
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email_address' => $this->input->post('email_address'),         
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password'))                       
        );

        $insert = $this->db->insert('membership', $new_member_insert_data);

        return $insert;
    }
}
4

1 回答 1

1
// Add the following into your HEAD section
var timer = 0;
function set_interval() {
  // the interval 'timer' is set when the page loads
  timer = setInterval("auto_logout()", 300000);

}


function auto_logout() {
  window.location = "your_logout_script.php";
}

// Add the following attributes into your BODY tag
onload="set_interval()"
于 2013-11-11T06:56:30.443 回答