當前位置:首頁 » 編程語言 » 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');等方法載入你需要的

熱點內容
東城電腦伺服器回收哪家價格高 發布:2025-04-15 23:54:40 瀏覽:572
qt編譯成32位的庫 發布:2025-04-15 23:21:05 瀏覽:319
樓蘭七座車什麼配置 發布:2025-04-15 23:20:11 瀏覽:489
如何取消微信支付登錄指紋密碼 發布:2025-04-15 23:14:20 瀏覽:200
如何將項目工程上傳公司伺服器 發布:2025-04-15 23:12:41 瀏覽:149
如何在政務內網架設伺服器 發布:2025-04-15 23:08:33 瀏覽:685
解壓動態壁紙 發布:2025-04-15 23:06:27 瀏覽:852
夢幻西遊檢測腳本原理 發布:2025-04-15 22:58:23 瀏覽:973
掛當日號老是伺服器忙怎麼回事 發布:2025-04-15 22:57:37 瀏覽:743
蘋果屏幕密碼怎麼改 發布:2025-04-15 22:40:43 瀏覽:28