作者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