php 注销代码点火器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8616887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:06:07  来源:igfitidea点击:

Logout codeigniter

phpcodeigniter

提问by The real thing

I have a logout controller in codeigniter :

我在 codeigniter 中有一个注销控制器:

<?php

class Logout extends MY_Controller {

    function index()
    {

        $this->session->sess_destroy();
        redirect('index.php');
    }
}

This logs me out but when i call another controller after logging, like "/site/addnewpost", this just logs me in again, as if the sassion had not been destroyed previously. Why is this happening?

这将我注销,但是当我在登录后调用另一个控制器时,例如“/site/addnewpost”,这只会让我再次登录,就好像之前没有销毁 sassion 一样。为什么会这样?

回答by Damien Pirsy

Follow ALex's suggestion, but using CI code:). What I mean, try unsetting each session data individually. I read once about an issue in version 2.0.3 I think, but I don't remember now and I don't have time to search for the reference. It's in their forum, though, and the suggestion was the same: unset each session element one by one.

遵循 ALex 的建议,但使用 CI 代码:)。我的意思是,尝试单独取消设置每个会话数据。我读过一次关于版本 2.0.3 中的一个问题,我想,但我现在不记得了,我没有时间搜索参考。但是,它在他们的论坛中,并且建议是相同的:一个一个地取消设置每个会话元素。

$this->session->unset_userdata('data_one');
$this->session->unset_userdata('data_two');
$this->session->unset_userdata('data_three');
$this->session->unset_userdata('data_one');
$this->session->sess_destroy();
redirect('home','refresh');  // <!-- note that
//you should specify the controller(/method) name here

You needto redirect because CI's session are just cookies, not the native php session array.

需要重定向,因为 CI 的会话只是 cookie,而不是本机 php 会话数组。

Another thing...make sure the fault isn't in your login methods, which logs you in no matter if you succesfully logout or not!

另一件事...确保错误不在您的登录方法中,无论您是否成功注销,它都会让您登录!

回答by Alex

Try explicitly delete items like this:

尝试明确删除这样的项目:

$this->Session->delete('User');
$this->Session->destroy();
$this->Cookie->delete("User");
$this->Cookie->destroy();
$this->Auth->logout();
$this->redirect('whereever');

回答by Kinjal Dixit

My problem had to do with caching on the server side. The quickest I could fix it was by appending random text to the logout link:

我的问题与服务器端的缓存有关。我能修复它的最快方法是在注销链接中附加随机文本:

<?php
    $this->load->helper('string');
    echo anchor('/home/logout/'.random_string(), 'logout');
?>

home/logout contained the same code as function indexin the question.

home/logout 包含与问题中相同的代码function index

Just so you know the redirect('/', 'refresh')did not work for me, but I again I did a quick test.

只是为了让您知道这redirect('/', 'refresh')对我不起作用,但我再次进行了快速测试。

I am guessing that the random_string()method can be replaced by outputting headers that force cache to be cleared etc. As you have probably guessed, I can't do that right now as I am super busy. Maybe later.

我猜测该random_string()方法可以通过输出强制清除缓存等的标题来替换。正如您可能已经猜到的那样,我现在不能这样做,因为我非常忙。也许以后。

回答by Andrea Estrella

You can also try manually setting your "logged_in" or whatever you called the session to false. Then, destroying all other session data.

您还可以尝试手动将“logged_in”或任何您调用会话的内容设置为 false。然后,销毁所有其他会话数据。

    $this->session->set_userdata('logged_in', FALSE);
    $this->session->session_destroy();
    redirect('index');

回答by Faisal Ibrahim

first we have to load session library to deal with session than unset the sessionID and destroy the session. I am using this code to unset my session and secure logout.

首先我们必须加载会话库来处理会话,而不是取消设置 sessionID 并销毁会话。我正在使用此代码来取消我的会话和安全注销。

$this->load->library('session');
$this->session->set_userdata('user_id', FALSE);
$this->session->sess_destroy();
$this->load->view('your URL');