doujiaozhan4397 2012-12-19 22:13
浏览 71
已采纳

使用CodeIgniter进行身份验证 - 其他会话信息

I have been an avid reader of stackoverflow for a long period of time, and now its my turn to ask a question.

I followed http://net.tutsplus.com/tutorials/php/easy-authentication-with-codeigniter/ tutorial and have managed to get it to work flawlessly for my self, however I need to expand the system ever so slightly. I need to be able to after the user has been verified to pull additional row fields of the authenticated person and store in session variables, for use in other controllers for sending information back to the database.

For instance I want to have in a sessional variable $f_name, and I need their $username. I have looked high and low and have found answers but they have only confused me.

My Model:

class admin_model extends CI_Model {
 function __construct( )
 {

 }

 public function verify_user($user_id, $password)
 {
 $q = $this->db->where('user_id', $user_id)
 ->where('password', sha1($password))->limit(1)->get('users');

 if ( $q->num_rows > 0   ){
     return $q->row();
 }
return false;

}

}

My Controller:

class admin extends CI_Controller {

function __construct()
{
parent:: __construct();
session_start();


}

public function index()
{
    if ( isset($_SESSION['user_id'])){
        redirect('welcome');
    }
    $this->load->library('form_validation');
    $this->form_validation->set_rules('user_id', 'User ID', 'required|min_length[8]');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');

    if ($this->form_validation->run() !==false){
       $this->load->model('admin_model');
      $res = $this
          ->admin_model
          ->verify_user(
          $this->input->post('user_id'),
          $this->input->post('password')
      );
   if ($res !== false) {
    $_SESSION['user_id'] = $this->input->post('user_id');
    redirect ('welcome');
   }






    }

    $this->load->view('login_view');
}

public function logout(){

    session_destroy();
    redirect ('admin');
}
}

Thanks again guys and I look forward to your answers\suggestions

  • 写回答

1条回答 默认 最新

  • duanfei8399 2012-12-19 22:32
    关注

    A couple of things:
    - the __construct() function in admin_model needs parent::__construct (or the function could be removed since it's empty) to inherit the code igniter model constructor.
    - code igniter and PHP sessions are different. You're using PHP sessions, which means that you'll have to call session_start() before any output is sent on every page in which you'd like to access the session data. This can also be set for every page in your php.ini by turning on session.autostart

    If you'd like to use code igniter sessions, look here: http://ellislab.com/codeigniter/user-guide/libraries/sessions.html

    UPDATE 12/21/2012

    Try setting the session variables from the database row returned:

    $_SESSION['f_name'] = $res->f_name; and $_SESSION['username'] = $res->username; in the same place as where you're setting the session user id variable. I'm assuming your database fields are mapping to the fields that you're wanting to use for the f_name and username fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗