当前位置:首页 » 编程语言 » php框架c

php框架c

发布时间: 2025-04-14 15:25:09

‘壹’ php CI框架中的 $this->uri->segment(); 是什么意思啊

获得URL上的参数
比如:...index.php/controller/index/3
$this->uri->segment(3);就是url上从index.php开始往后数,/划分,例子上就是得到的3

‘贰’ 用php的CI框架怎么写登录和注册

第一步:login.php

//登陆方法
public function login(){
//如果用户名和密码为空,则返回登陆页面
if(empty($_POST['username']) || empty($_POST['password'])){
$data['verifycode'] = rand(1000,9999);//生成一个四位数字的验证码
//将验证码放入session中,注意:参数是数组的格式
$this->session->set_userdata($data);
//注意:CI框架默认模板引擎解析的模板文件中变量不需要$符号
//$this->parser->parse("admin/login",$data);
//smarty模板变量赋值
$this->tp->assign("verifycode",$data['verifycode']);
//ci框架在模板文件中使用原生态的PHP语法输出数据
//$this->load->view('login',$data);//登陆页面,注意:参数2需要以数组的形式出现
//显示smarty模板引擎设定的模板文件
$this->tp->display("admin/login.php");
}else{
$username = isset($_POST['username'])&&!empty($_POST['username'])?trim($_POST['username']):'';//用户名
$password = isset($_POST['password'])&&!empty($_POST['password'])?trim($_POST['password']):'';//密码
$verifycode = isset($_POST['verifycode'])&&!empty($_POST['verifycode'])?trim($_POST['verifycode']):'';//验证码

//做验证码的校验
if($verifycode == $this->session->userdata('verifycode')){
//根据用户名及密码获取用户信息,注意:参数2是加密的密码
$user_info=$this->user_model->check_user_login($username,md5($password));
if($user_info['user_id'] > 0){
//将用户id、username、password放入cookie中
//第一种设置cookie的方式:采用php原生态的方法设置的cookie的值
//setcookie("user_id",$user_info['user_id'],86500);
//setcookie("username",$user_info['username'],86500);
//setcookie("password",$user_info['password'],86500);
//echo $_COOKIE['username'];

//第二种设置cookie的方式:通过CI框架的input类库
$this->input->set_cookie("username",$user_info['username'],3600);
$this->input->set_cookie("password",$user_info['password'],3600);
$this->input->set_cookie("user_id",$user_info['user_id'],3600);
//echo $this->input->cookie("password");//适用于控制器
//echo $this->input->cookie("username");//适用于控制器
//echo $_COOKIE['username'];//在模型类中可以通过这种方式获取cookie值
//echo $_COOKIE['password'];//在模型类中可以通过这种方式获取cookie值

//第三种设置cookie的方式:通过CI框架的cookie_helper.php函数库文件
//这种方式不是很灵验,建议大家采取第二种方式即可
//set_cookie("username",$user_info['username'],3600);
//echo get_cookie("username");

//session登陆时使用:将用户名和用户id存入session中
//$data['username']=$user_info['username'];
//$data['user_id']=$user_info['user_id'];
//$this->session->set_userdata($data);

//跳转到指定页面
//注意:site_url()与base_url()的区别,前者带index.php,后者不带index.php
header("location:".site_url("index/index"));
}
}else{
//跳转到登陆页面
header("location:".site_url("common/login"));
}
}
}
}

第二步:User_model.php

//cookie登陆:检测用户是否登陆,如果cookie值失效,则返回false,如果cookie值未失效,则根据cookie中的用户名和密码从数据库中获取用户信息,如果能获取到用户信息,则返回查询到的用户信息,如果没有查询到用户信息,则返回0
public function is_login(){
//获取cookie中的值
if(empty($_COOKIE['username']) || empty($_COOKIE['password'])){
$user_info = false;
}else{
$user_info=$this->check_user_login($_COOKIE['username'],$_COOKIE['password']);
}
return $user_info;
}

//根据用户名及加密密码从数据库中获取用户信息,如果能获取到,则返回获取到的用户信息,否则返回false,注意:密码为加密密码
public function check_user_login($username,$password){
//这里大家要注意:$password为md5加密后的密码
//$this->db->query("select * from ");
//快捷查询类的使用:能为我们提供快速获取数据的方法
//此数组为查询条件
//注意:关联数组
$arr=array(
'username'=>$username,//用户名
'password'=>$password,//加密密码
'status'=>1 //账户为开启状态
);
//在database.php文件中已经设置了数据表的前缀,所以此时数据表无需带前缀
$query = $this->db->get_where("users",$arr);
//返回二维数组
//$data=$query->result_array();
//返回一维数组
$user_info=$query->row_array();
if(!empty($user_info)){
return $user_info;
}else{
return false;
}
}

第三步:其它控制器:

public function __construct(){

//调用父类的构造函数
parent::__construct();
$this->load->library('tp'); //smarty模板解析类
$this->load->helper('url'); //url函数库文件
$this->load->model("user_model");//User_model模型类实例化对象
$this->cur_user=$this->user_model->is_login();
if($this->cur_user === false){
header("location:".site_url("common/login"));
}else{
//如果已经登陆,则重新设置cookie的有效期
$this->input->set_cookie("username",$this->cur_user['username'],3600);
$this->input->set_cookie("password",$this->cur_user['password'],3600);
$this->input->set_cookie("user_id",$this->cur_user['user_id'],3600);
}

$this->load->library('pagination');//分页类库
$this->load->model("role_model");//member_model模型类
$this->load->model("operation_model");//引用operation_model模型
$this->load->model("object_model");//引用object_model模型
$this->load->model("permission_model");//引用permission_model模型
}

‘叁’ PHP CI框架修改数据的方法

CI框架下的PHP增删改查总结:
controllers下的 cquery.php文件
[php] view plain
<?php

class CQuery extends Controller {

//构造函数
function CQuery() {
parent::Controller();
// $this->load->database();

}

function index() {
//调用model 其中train为外层文件夹 MQuery为model名称 queryList为重命名
$this->load->model('train/MQuery','queryList');
//获得返回的结果集 这里确定调用model中的哪个方法
$result = $this->queryList->queryList();
//将结果集赋给res
$this->smarty->assign('res',$result);
//跳转到显示页面
$this->smarty->view('train/vquery.tpl');
}

//进入新增页面
function addPage() {
$this->smarty->view('train/addPage.tpl');
}

//新增
function add() {
//获得前台数据
//用户名
$memberName = $this->input->post('memberName');
//密码
$password = $this->input->post('password');
//真实姓名
$userRealName = $this->input->post('userRealName');
//性别
$sex = $this->input->post('sex');
//出生日期
$bornDay = $this->input->post('bornDay');
//e_mail
$eMail = $this->input->post('eMail');
//密码问题
$question = $this->input->post('question');
//密码答案
$answer = $this->input->post('answer');
//调用model
$this->load->model('train/MQuery','addRecord');
//向model中的addRecord传值
$result = $this->addRecord->addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
//判断返回的结果,如果返回true,则调用本页的index方法,不要写 $result == false 因为返回的值未必是false 也有可能是""
if ($result) {
$this->index();
} else {
echo "add failed.";
}
}
//删除
function deletePage() {
//获得ID
$deleteID = $this->uri->segment(4);
//调用model
$this->load->model('train/MQuery','delRecord');
//将值传入到model的delRecord方法中
$result = $this->delRecord->delRecord($deleteID);
//判断返回值
if ($result) {
$this->index();
} else {
echo "delect failed.";
}
}
//修改先查询
function changePage() {
$changeID = $this->uri->segment(4);
$this->load->model('train/MQuery','changeRecord');
$result = $this->changeRecord->changeRecord($changeID);
//将结果集赋给res
$this->smarty->assign('res',$result);

//跳转到显示页面
$this->smarty->view('train/changePage.tpl');
}
//修改
function change() {
//获得前台数据
//ID
$ID = $this->input->post('id');
//用户名
$memberName = $this->input->post('memberName');
//密码
$password = $this->input->post('password');
//真实姓名
$userRealName = $this->input->post('userRealName');
//性别
$sex = $this->input->post('sex');
//出生日期
$bornDay = $this->input->post('bornDay');
//e_mail
$eMail = $this->input->post('eMail');
//密码问题
$question = $this->input->post('question');
//密码答案
$answer = $this->input->post('answer');
//调用model
$this->load->model('train/MQuery','change');
//向model中的change传值
$result = $this->change->change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer);
//判断返回的结果,如果返回true,则调用本页的index方法,不要写 $result == false 因为返回的值未必是false 也有可能是""
if ($result) {
$this->index();
} else {
echo "change failed.";
}
}
}
models中的 mquery.php 文件
[php] view plain
<?php

class MQuery extends Model {
//构造函数
function MQuery() {
parent::Model();
//连接数据库
$this->load->database();
}

//查询列表
function queryList() {
//防止select出的数据存在乱码问题
//mysql_query("SET NAMES GBK");
//SQL语句
$sql = "SELECT ID,member_name,sex,e_mail FROM user_info_t";
//执行SQL
$rs = $this->db->query($sql);
//将查询结果放入到结果集中
$result = $rs->result();
//关闭数据库
$this->db->close();
//将结果集返回
return $result;
}

//新增
function addRecord($memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
//防止select出的数据存在乱码问题
//mysql_query("SET NAMES GBK");
//SQL语句
$sql = "INSERT INTO user_info_t (member_name,password,user_real_name,sex,born_day,e_mail,question,answer) " .
"VALUES ('$memberName','$password','$userRealName','$sex','$bornDay','$eMail','$question','$answer')";
//执行SQL
$result = $this->db->query($sql);
//关闭数据库
$this->db->close();
//返回值
return $result;
}

//删除
function delRecord($deleteID) {
//防止select出的数据存在乱码问题
//mysql_query("SET NAMES GBK");
$sql = "DELETE FROM user_info_t WHERE ID = $deleteID";
$result = $this->db->query($sql);
$this->db->close();
return $result;
}

//修改前查询
function changeRecord($changeID) {
//防止select出的数据存在乱码问题
//mysql_query("SET NAMES GBK");
$sql = "SELECT ID,member_name,password,user_real_name,sex,born_day,e_mail,question,answer FROM user_info_t WHERE ID = $changeID";
//执行SQL
$rs = $this->db->query($sql);
$result = $rs->row();//$result = $rs[0]
//关闭数据库
$this->db->close();
//将结果集返回
return $result;
}

//修改
function change($ID,$memberName,$password,$userRealName,$sex,$bornDay,$eMail,$question,$answer) {
//防止select出的数据存在乱码问题
//mysql_query("SET NAMES GBK");
//SQL语句
$sql = "update user_info_t set member_name = '$memberName',password = '$password', user_real_name = '$userRealName'," .
"sex = '$sex',born_day = '$bornDay',e_mail = '$eMail',question = '$question',answer = '$answer'" .
"where ID = $ID";
//执行SQL
$result = $this->db->query($sql);
//关闭数据库
$this->db->close();
//返回值
return $result;
}
}

views 下的 addPage.tpl文件

[php] view plain
<html>
<head>
</head>
<body><form action="{{site_url url='train/cquery/add'}}" method="post">
<table border='1'>

<tr>
<td>用户名</td>
<td><input type="text" class="text" name="memberName" id="memberName"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" class="text" name="password" id="password"/></td>
</tr>
<tr>
<td>真实姓名</td>
<td><input type="text" class="text" name="userRealName" id="userRealName"/></td>
</tr>
<tr>
<td>性别</td>
<td><input type="text" class="text" name="sex" id="sex"/></td>
</tr>
<tr>
<td>出生日期</td>
<td><input type="text" class="text" name="bornDay" id="bornDay"/></td>
</tr>
<tr>
<td>e_mail</td>
<td><input type="text" class="text" name="eMail" id="eMail"/></td>
</tr>
<tr>
<td>密码问题</td>
<td><input type="text" class="text" name="question" id="question"/></td>
</tr>
<tr>
<td>密码答案</td>
<td><input type="text" class="text" name="answer" id="answer"/></td>
</tr>

</table>
<table>
<tr>
<td><input type="submit" class="button" name="OK" value="提交" />
</td>
</tr>
</table></form>
</body>
</html>

‘肆’ php的CI框架如何实现异步调用

在你自定义的类库中初始化CodeIgniter资源

要你自定义的类库中访问CodeIgniter的原始资源,你必须使用 get_instance() 函数.这个函数返回一个CodeIgniter super object.

一般来说在你的控制器函数中你可以通过 $this 调用任何可用的CodeIgniter函数:
$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
//etc.
$this, 只直接作用在你自己的控制器,模型和视图中.当你在自定义类中想使用CodeIgniter原始类时,你可以这样做:

首先,定义CodeIgniter对象赋给一个变量:
$CI =& get_instance();
一旦定义某个对象为一个变量,你就可以使用那个变量名 取代 $this:
$CI =& get_instance();

$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
//etc.
注意: 你将注意到get_instance()这个函数通过被引用的方式被传递:

$CI =& get_instance();

这十分重要. 通过引用的方式赋给变量将使用原始的 CodeIgniter 对象,而不是创建一个副本。

//------------------------------------------------------------------------------------------------//
我想这也许是你需要的.$CI =& get_instance();之后再用$CI->load->library('session');等方法加载你需要的

热点内容
java常用集合类 发布:2025-04-16 02:01:33 浏览:816
百度云解压无法预览 发布:2025-04-16 01:46:49 浏览:368
hsqldb数据库 发布:2025-04-16 01:46:45 浏览:728
苹果xs和安卓5哪个好 发布:2025-04-16 01:46:12 浏览:903
萤石c5c怎么配置有线连接 发布:2025-04-16 01:39:16 浏览:455
潮主解压码 发布:2025-04-16 01:26:20 浏览:609
license文件夹 发布:2025-04-16 01:16:26 浏览:253
怎么给笔记本提升配置 发布:2025-04-16 01:15:46 浏览:186
java接口抽象 发布:2025-04-16 01:15:03 浏览:196
虚拟云服务器s 发布:2025-04-16 00:59:54 浏览:276