作者hareion08 (Hareion)
看板PHP
標題[請益] MVC model超新手問題-use CI
時間Fri Jun 7 21:32:09 2013
初嘗CI 初用MVC架構@@
請大家多指教,並見諒我的白痴問題=口=
我有一個controller 在construct時
load 一個叫做(access) 的model 進來
裡面寫的是一些當前使用者權限處理之類的函數
==Model: access.php==
function islogin()
{
$this->load->library('session');
return ($this->session->userdata('is_login')==TRUE) ? (TRUE)
: (FALSE);
}
function noaccess()
{
if($this->islogin()==FALSE)
{
die("Access Denied");
}
}
==
AND 這個controller我是要用來做系統登入
所以後面有 個函數來做 帳號密碼判斷
==
$this->load->model("administrator/user");
$get_uid = $this->user->user_check($_POST['username'],$_POST['password']);
==
=Model: user.php==
function user_check($username,$password)
{
$query = $this->db->query("select uid from _user where
_username='$username' && _password='".md5($password)."'");
$row = $query->row();
return (isset($row->uid)) ? ($row->uid) : (FALSE);
}
==
就變成總共載入了兩個model到一個controller裡
但是這樣第2個載入進來的會出錯T_T
詢問了前輩,他說只能載入一個?
但這樣要怎麼做呢? 移到libary裡用libary方式載入?
還是說...我的MVC架構根本有問題QQ
懇請各位大師指教!!
--
傷眼時間開始~
附上完整檔案內容:
--
==Controller: process.php==
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Process extends CI_Controller
{
private $is_login;
function __construct()
{
parent::__construct();
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
$this->load->library('session');
$this->load->model("administrator/access");
$this->islogin = $this->access->islogin();
}
function index()
{
}
function login()
{
$echo_data = array();
if($this->islogin)
{
$echo_data["status"] = 0;
}
else
{
if($_POST['safecode']==""||$_POST['username']==""||$_POST['password']=="")
{
$echo_data['status'] = 1;
}
else
{
if($_POST['safecode']==$this->session->userdata('safecode'))
{
$this->load->model("administrator/user");
$get_uid =
$this->user->user_check($_POST['username'],$_POST['password']);
if($get_uid==FALSE)
{
$echo_data['status'] = 3;
}
else
{
$echo_data['db'] = $get_uid;
$this->session->set_userdata(array("is_login"=>TRUE,"uid"=>$get_uid,"username"=>$_POST['username']));
$echo_data['status'] = 4;
}
}
else
{
$echo_data['status'] = 2;
}
}
}
$this->session->unset_userdata('safecode');
echo json_encode($echo_data);
}
function logout()
{
$this->access->noaccess();
$this->session->sess_destroy();
}
}
?>
==END==
==Model: access.php==
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Access extends CI_Controller
{
function islogin()
{
$this->load->library('session');
return ($this->session->userdata('is_login')==TRUE) ? (TRUE)
: (FALSE);
}
function noaccess()
{
if($this->islogin()==FALSE)
{
die("Access Denied");
}
}
}
?>
==END==
==Model: user.php==
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Model
{
function __construct()
{
parent::__construct();
$this->load->database();
}
function user_check($username,$password)
{
$query = $this->db->query("select uid from _user where
_username='$username' && _password='".md5($password)."'");
$row = $query->row();
return (isset($row->uid)) ? ($row->uid) : (FALSE);
}
}
?>
==END==
-
非常感謝您閱讀完畢!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.232.236
1F:→ chenstin:出錯是出什麼錯誤? 另外,為什麼你的access.php是model 06/07 23:02
2F:→ chenstin:卻繼承controller? 06/07 23:03
3F:→ chenstin:剛試了一下,應該是access.php繼承錯class造成的問題 06/07 23:15
4F:→ hareion08:阿阿阿,糗斃了><;感謝大大提醒,因為我開新檔案時懶得 06/07 23:19
5F:→ hareion08:打開頭,所以直接複製,結果複製到controller的開頭了.. 06/07 23:20
6F:→ hareion08:可以執行了T_T,感謝您;好想挖個洞跳進去.... 06/07 23:23
7F:→ tails32100:CI有input library 可以取post值哦 06/08 01:20
8F:→ kosjason:照理來說載入N的應該都沒關係 錯誤應該是有衝到吧 06/08 01:28
9F:→ hareion08:$this->input->post??好長=口=,謝謝指教,已經修正~ 06/08 02:03
10F:→ tkdmaf:長歸長,卻處理了很多安全性的機制。 06/08 20:36
11F:→ tkdmaf:當然你可以用$post = $this->input->post(); 06/08 20:36
12F:→ tkdmaf:這個$post就會是經過處理的$_POST 06/08 20:37
13F:→ hareion08:我有改config的global_xss_filteri這樣OK?! 06/08 21:46