2016-04-24 8 views
1

check_login機能を使用すると問題が発生します。
http://localhost/shopping/admin/loginと入力して管理者のユーザー名とパスワードを入力すると、のホーム管理ページにリダイレクトされません。しかし、私がしようとすると http://localhost/shopping/admin。それでもログインページにリダイレクトされます。Codeigniterのログインを確認してリダイレクトしませんか?

/** 
* MY_Controller constructor. 
*/ 
class MY_Controller extends CI_Controller 
{ 
    public $data = array(); 
    function __construct() 
    { 
     parent::__construct(); 
     // $this->load->library('session'); 

     $controller = $this->uri->segment(1); 
     switch ($controller) { 
      case 'admin': { 
       $this->load->helper('admin'); 
       $this->_check_login(); 
       break; 
      } 
      default: { 

      } 
     } 
    } 

    /* 
    * Check login admin 
    */ 
    function _check_login() 
    { 
     $controller = $this->uri->rsegment('1'); 
     $controller = strtolower($controller); 
     $login = $this->session->userdata('login'); 
     //redirect to login if not logged in. 
     if (!$login && $controller != 'login') { 
      redirect(admin_url('login')); 
     } 
     //redirect to home if login successful. 
     if ($login && $controller == 'login') { 
      redirect(admin_url('home')); 
     } 
    } 
} 

/* 
* admin_helper 
*/ 
function admin_url($url = '') 
{ 
    return base_url('admin/'.$url); 
} 

しかし、私は私の管理者でログインした後、それが成功したホーム・ページにリダイレクト

//redirect to login if not logged in. 
if (!$login && $controller != 'login') { 
    redirect(admin_url('login')); 
} 

を削除します。

なぜ機能しないのですか?

/* 
    * Controller login 
    */ 
class Login extends MY_Controller 
{ 

    function index() 
    { 

     $this->load->library('form_validation'); 
     $this->load->helper('form'); 

     if ($this->input->post()) { 
      $this->form_validation->set_rules('login', 'login', 'callback_check_login'); 
      if ($this->form_validation->run()) { 
       $this->session->set_userdata('login', true); 

       redirect(admin_url('home')); 
      } 
     } 
     $this->load->view('admin/login/index'); 
    } 

    /* 
    * Kiem tra login 
    * */ 
    function check_login() 
    { 
     /* 
     $this->load->library('form_validation'); 
     $this->load->helper('form'); 
     */ 
     $username = $this->input->post('username'); 
     $password = $this->input->post('password'); 
     $password = md5($password); 

     $this->load->model('admin_model'); 
     $where = array('username' => $username, 'password' => $password); 
     if ($this->admin_model->check_exists($where)) { 
      return TRUE; 
     } 
     $this->form_validation->set_message(__FUNCTION__, 'Login fail.'); 
     return FALSE; 


    } 
} 
+0

にリダイレクトする場合はtrueリダイレクトするように設定されていない場合は、ユーザーが認証するかどうかをチェックするか の値をコントローラだけでなく、セッション値をチェックしますユーザーがログインボタンを押したときのセッション? –

+0

@SoravGarg sr、私は編集されました! – hyphens2

+0

あなたが編集した場所あなたの変更を見つけることができません –

答えて

1

セッションを使用しているところはまだ私は、設定されたセッションのために意味を見つけることができません一般的に、我々は、私はそれはあなたのコードに欠けていると思う$this->session->set_userdata('user_login_data');を使用。あなたの助けのための

<?php 

class Welcome extends CI_Controller{ 

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

    public function index() 
    { 
     $adminId = $this->session->userdata('unique_id'); 
     if(empty($adminId)){ 
      $this->load->view('login'); 
     }else{ 
      redirect('home/dashboard'); 
     } 
    } 

    public function login() 
    { 
     $username = $this->input->post('username'); 
     $password = base64_decode($this->input->post('password')); 
     $arr = array(
      'username' => $username, 
      'password' => md5($password), 
      'user_type' => 'admin' 
      ); 
     $result = $this->common_model->getMultiple('panel_login',$arr); 
     if(!empty($result)){ 
      $this->session->set_userdata('unique_id',$result[0]['s_no']); 
      $this->session->set_userdata('username',$result[0]['username']); 
      $this->session->set_userdata('password',$result[0]['password']); 
      $this->session->set_userdata('type',$result[0]['user_type']); 
      $this->session->set_userdata('status',$result[0]['status']); 
      $this->session->set_userdata('user_activity',time()); 
     } 
     echo json_encode($result); 
    } 
} 

?> 

はまた、私はそれはあなたを助けることを願っていますユーザのログイン..ための私のデモコードを添付ステートメントの下

+0

このスクリプトに従ってください... –

1

if (!$login && $controller != 'login') { 
      redirect(admin_url('login')); 
    } 

$ログインが常になりますので、常にtrueを返しますあなたがset_userdataでその値を設定していないのでfalseです.Uはセグメント1をインデックスとして使用しているため、$ controllerはコントローラとして常にadminを返します。ログインページにリダイレクトしています。

*設定セッションcorrectyとセッション値のログインは、あなたが設定されている他にどこloignこの機能でホームページ

+0

あなたの返信ありがとうございます:)それは動作します – hyphens2

関連する問題