【问题标题】:how to end sessions in code igniter如何在codeigniter中结束会话
【发布时间】:2016-02-02 13:19:03
【问题描述】:

我最近在代码点火器中创建了带有会话的登录。它登录成功,但我忘了注销。我不小心点击了返回,然后我得到了这个错误,,

Fatal error: Cannot redeclare login_model::validate() in C:\xampp\htdocs\HR\application\models\login_model.php on line 41
A PHP Error was encountered

Severity: Compile Error

Message: Cannot redeclare login_model::validate()

Filename: models/login_model.php

Line Number: 41

Backtrace:

然后当我回到原来的页面http://localhost/hr/login/index1 我也得到了那个错误。我怎样才能停止这个会话?以及为什么会话不进入我的数据库 ci_sessions。

【问题讨论】:

  • 错误消息看起来与您所说的问题不同。看起来像重复的方法名称,而不是会话问题
  • // 用于登录public function validateCreds(){ $this->load->model('login_model'); $q = $this->login_model->validate(); if ($q == 1){ if($q){ //if creds are validated $data = array( 'USERNAME' => $this->input->post('USERNAME'), 'is_logged_in' => true ); $this->session->set_userdata($data); redirect('login/index1'); }else{ //incorrect username or password $this ->index(); } } }
  • //login_model public function validate($USERNAME, $PASSWORD){ $sql ="SELECT * FROM tbl_users WHERE USERNAME = ? AND PASSWORD = ?"; $data1 = array( 'USERNAME' => $this->input->post('USERNAME'), 'PASSWORD' => $this->input->post('PASSWORD') ); $query = $this->db->query($sql, $data1); //$username = $this->db->where('USERNAME', $this->input->post('USERNAME')); //$this->db->where('PASSWORD', $this->input->post('PASSWORD')); //$query = $this->db->get('tbl_users'); return $query->num_rows(); }
  • “看起来像重复的方法名称,而不是会话问题”@DamienPirsy 是的。你的权利。哈哈。我复制了两次验证功能。哈哈。但我还有另一个问题。我的登录功能和注销功能看起来只是链接。当我单击登录然后单击返回按钮时,我会自动注销,当我单击前进时,它无需输入任何内容即可登录
  • 看起来您的模型中有两次验证函数。

标签: php codeigniter session


【解决方案1】:

您可以通过以下方式销毁会话:-

$this->session->sess_destroy();

如果您注销,那么尽管会话被销毁,会话用户数据仍会在当前 CI 页面构建期间保留。

你应该做一个像下面这样的函数。

function log_out()
{
    $this->session->sess_destroy();
    // null the session (just in case):
    $this->session->set_userdata(array('user_name' => '', 'is_logged_in' => ''));    
    redirect('your_page_url');
}

有销毁一些你想要的会话的功能

   $this->session->unset_userdata('session_name');

【讨论】:

  • 我做了那个,但我无法访问注销 bec。未显示注销按钮的页面。
【解决方案2】:
public function logout() {
    $this->session->unset_userdata();
    $this->session->unset_userdata('is_client_login');
    $this->session->sess_destroy();
    $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate, no-transform, max-age=0, post-check=0, pre-check=0");
    $this->output->set_header("Pragma: no-cache");
    redirect($this->getUrl('/'));
}

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 2012-05-23
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多