dongnao2582 2015-01-01 14:06
浏览 71

codeigniter登录和用户会话

I built an user login to my site by this guide: http://www.iluv2code.com/login-with-codeigniter-php.html

I have few question about session's

I need to put the session_start(); in every controller or there is a way in codeigniter that it will automaticlly be in all controllers? (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?

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?

EDIT: I use this in 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. where am I wrong?

  • 写回答

4条回答 默认 最新

  • douban5644 2015-01-01 14:15
    关注

    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>.

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题