<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Model {
/*
* 調用方式
*
*
########## Controllers 控制器各頁面 ##########
if (!$this->session->userdata("admin")) redirect("member/login");
########## Controllers 控制器部份 ##########
public function login()
{
//檢查session值
//echo $this->session->userdata("admin");
//echo $this->session->userdata("uid");
$data["path_name"] = "登入頁";
$data['base_url'] = $this->config->item('base_url');
$data['this_url'] = $data['base_url'] . "index.php/member/login";
$check = array(
'bl' => false,
'msg' => ''
);
if(isset($_POST['login']))
{
$logid = $this->input->post('login_id'); //對應view的表單
$logpwd = $this->input->post('login_pwd');
$this->load->model('Login');
$check = $this->Login->check($logid,$logpwd);//回傳參數bl(布林),以及msg 何種錯誤訊息
if ($check['bl']){
$newdata = array( //寫入session的參數
'uid' => $logid,
'admin' => TRUE, //必須對應驗證
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
redirect("member"); //成功後轉向
}
}
$data['login_msg']= $check['msg'] ;
$this->load->view('view_login',$data);
}
//登出
public function logout()
{
$del_session = array('uid', 'admin', 'logged_in');
$this->load->model('Login');
$this->Login->logout($del_session);
redirect('member/login');
}
########## Views 視圖部份 ##########
<?php echo form_open_multipart('member/login');?>
<label>
帳號
<input type="text" name="login_id" id="login_id" />
</label>
<br />
<label>
密碼
<input type="password" name="login_pwd" id="login_pwd" />
</label>
<input type="hidden" name="login" value="1" id="login" />
<br />
<label>
<input type="submit" name="button" id="button" value="登入" />
</label>
</form>
<a href="logout">登出</a>
*
*
*/
function __construct()
{
parent::__construct();
$this->load->database();
}
function check($id,$pwd)
{
//讀取資料庫字串
//$query = $this->db->get('check_db');
$key = "@lib";
$msg[0] = "登入成功";
$msg[1] = "查無此帳號";
$msg[2] = "請重新檢查帳號密碼";
$msg[3] = "無法進行認證";
if (true)//$query->num_rows() > 0)
{
//$check = $query->result_array();
$check[0]['id'] = "abc";
$check[0]['pwd'] = "ecfc8084e7c6c29e3ccf3f202635a197";
$check[1]['id'] = "def";
$check[1]['pwd'] = "d3bb34166864b20243936820ef8c5935";
$check[2]['id'] = "user";
$check[2]['pwd'] = "user001";
$i = 1;
foreach ($check as $row)
{
if($id!=$row['id'])
{
$check['msg'] = $msg[1];
$check['bl'] = false;
continue;// 檢查帳號,不是就跳出此次循環
}else{
$mdpwd = md5($pwd.$key);
if($mdpwd == $row['pwd'])
{
//驗證成功
$check['msg'] = $msg[0];
$check['id'] = $id;
//$check['level'] = "可以添加權限回傳";
$check['bl'] = true;
}else{
echo "出錯訊息:" . $mdpwd ."<br/>";
$check['msg'] = $msg[2];
$check['bl'] = false;
}
break;//檢測密碼,帳號對了就不再跑迴圈
}
}
return $check;
}else{
$check['msg'] = $msg[3];
$check['bl'] = false;
return $check;
}
}
function logout($del_session)
{
foreach ($del_session as $row)
{
$this->session->unset_userdata($row);
}
}
}
/* End of file login.php */
/* Location: ./application/Model/login.php */
- Nov 27 Sun 2011 17:02
Codeiginter Session登入登出 控制器、模組、視圖試作
文章標籤
全站熱搜
留言列表