简体   繁体   English

Codeigniter登录和用户会话

[英]codeigniter login and user session

I built an user login to my site by this guide: http://www.iluv2code.com/login-with-codeigniter-php.html 我通过此指南建立了一个登录到我的网站的用户: http : //www.iluv2code.com/login-with-codeigniter-php.html

I have few question about session's 我对会议的问题没什么疑问

I need to put the session_start(); 我需要把session_start(); in every controller or there is a way in codeigniter that it will automaticlly be in all controllers? 在每个控制器中还是在codeigniter中有一种方法可以自动在所有控制器中使用? (should I do that?) (我应该这样做吗?)

and there is a other way rether to put in every function that: 还有另一种方式可以插入每个函数:

    if($this->session->userdata('logged_in'))
    {
       //function code
    }else{
     //If no session, redirect to login page
     redirect('../login', 'refresh');
   }

or should I do that for every controller function (for example if I have controller named page and he have the functions :index,edit,view I need to put it for every one of them? 还是应该为每个控制器功能执行此操作(例如,如果我有一个名为page的控制器,而他具有功能:index,edit,view,则需要为每个控制器放置它吗?

and last question, I have logout button on the top of every page called by view/header should I also put this function: 最后一个问题,在我还应放置此功能的情况下,在视图/标题调用的每个页面的顶部都有退出按钮:

 function logout()
 {
   $this->session->unset_userdata('logged_in');
   session_destroy();
   redirect('home', 'refresh');
 }

in every controller or I can do it a "golbel" function in some way? 在每个控制器中还是可以通过某种方式实现“ golbel”功能?

EDIT: I use this in hooks.php: 编辑:我在hooks.php中使用此:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|   http://codeigniter.com/user_guide/general/hooks.html
|
*/

$hook['post_controller_constructor'] = array(
                                'class'    => 'SessionData',
                                'function' => 'initializeData',
                                'filename' => 'loginHelper.php',
                                'filepath' => 'hooks',
                                'params'   => array()
                                );
and this in loginHelper.php:
<?
class SessionData {
    var $CI;

    function __construct(){
        $this->CI =& get_instance();
        if(!isset($this->CI->session))  //Check if session lib is loaded or not
          $this->CI->load->library('session');  //If not loaded, then load it here
    }

    function initializeData() {
          // This function will run after the constructor for the controller is ran
          // Set any initial values here
          if(!$this->CI->session->userdata('logged_in')){    //call session methods with super object
            redirect('../login', 'refresh');    
          }else{
            $data['user'] = $this->CI->session->userdata('logged_in');
          }
    }
}
?>

    /* End of file hooks.php */
    /* Location: ./application/config/hooks.php */

the user['data'] not created in all the pages. 并非在所有页面中都创建了user ['data']。 where am I wrong? 我哪里错了?

For your second question about logout, I usually put the logout function in a User controller and call it as <a href="<?php echo base_url().'user/logout';?>">Log Out</a> . 对于第二个有关注销的问题,我通常将logout功能放在User控制器中,并将其称为<a href="<?php echo base_url().'user/logout';?>">Log Out</a>

For your first question, I saw a tutorial how you do user login in a controller and extend that controller in your regular controller, thats how you avoid login check in every function. 对于第一个问题,我看到了一个教程,您如何在控制器中登录用户并在常规控制器中扩展该控制器,这就是如何避免在每个功能中进行登录检查的方法。 I am trying to find that tutorial, once I get it, I'll share it, but the concept is like that way. 我试图找到该教程,一旦获得,我将与大家分享,但是概念是这样的。

you do not need to put the session_start(); 您不需要放置session_start(); in every controller! 在每个控制器中!

You could simply start the session class in the autoload.php file in your config directory! 您只需在config目录中的autoload.php文件中启动会话类!

$autoload['libraries'] = array('database', 'session', 'encrypt');

Also, it is better to check if the user is logged in, inside the constructor function of the classes! 另外,最好在类的构造函数中检查用户是否已登录!

if(!$this->session->userdata('logged_in'))
    redirect('loginController/loginFunction', 'refresh');

and to destroy all the sessions when logging out, you could use sess_destroy(); 并在注销时销毁所有会话,可以使用sess_destroy();

Initializing a Session 初始化会话

To autoload Session, open the application/config/autoload.php file and add the item you want loaded to the autoload array so: $autoload['libraries'] = array('session'); 要自动加载会话,请打开application/config/autoload.php文件,然后将要加载的项目添加到autoload数组中,如下所示: $autoload['libraries'] = array('session'); Yo don't use session_start(); don不要使用session_start(); and session_destroy(); session_destroy(); at all. 完全没有

To Check Login 检查登录

Use Hooks to avoid chunky code dupplications and it is better practise. 使用Hooks可以避免繁琐的代码重复,这是更好的实践。 Navigate bottom of the page and read Hook Points so you get an idea. 导航到页面底部,阅读“ 挂钩点”,这样您就可以了。 It is easy! 这很容易! Definitely learn hooks. 绝对学习钩子。

Logout 登出

You answer explained here . 在这里解释的答案。

After alot of working I solved the problem with the login check. 经过大量工作后,我使用登录检查解决了问题。

I didn't use the hooks. 我没有用钩子。 I build a MY_Controller in core folder and exteds it in all my controllers expect from the login controller. 我在核心文件夹中构建了一个MY_Controller,并在所有期望从登录控制器获得的控制器中将其释放。

In the MY_Controller I use thie login check. 在MY_Controller中,我使用该登录检查。

CodeIgniter core controllers: https://philsturgeon.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY/ CodeIgniter核心控制器: https : //philsturgeon.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM