CI_URI类的解析url的相关信息
直接使用$this->uri可以使用它的相关属性
system/core/URI.php文件中
部分常用属性:
$this->uri->segment(4);//获取url中pathinfo
//的第四段的值
入口文件.php/控制器/动作/参数1/参数2/...需要设默认值和顺序要注意
index.php/user/index/3/zhangsan
public function index($id=0,$name=''){
echo $id,$name;
}在application/core/文件夹下面
添加自己的扩展控制器
class MY_Controller extends CI_Controller{
public function __construct(){
parent::__construct
}
}
//配置模型前缀
$config['subclass_prefix']='MY_';//默认值文件名全小写,类名首字母大写
建议类名加上 _model后缀
在控制器中加载模型:
在construct中加入:
$this->load->model('User_model');
$this->User_model->get();
//为模型起别名
$this->load->model('User_model','user');
$this->user->get();$this->load->helper('url');
site_url('控制器/方法');$this->load->helper('url');
<img src="<?php echo base_url();?>upload/a.jpg" />
//可以在autoload.php中配置自动加载
$autoload['helper']加入url$router['show/([\d]+)\.html']='article/show/$1'; article/show/5.html => article/show/5;
#开启apache的rewrite模块
#在根目录中放入.htaccess文件进行重写
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]//模型中操作
//装载分页类文件
$this->load->library('pagination');
$this->load->helper(url);
//分页链接
$config['base_url'] = site_url('user/test');
//总记录条数
$config['total_rows'] = 100;
//每页显示10条数据
$config['per_page'] = 10;
//偏移量
$offset_limit = intval($this->uri->segment(3));
$this->pagination->initialize($config);
echo $this->pagination->create_links();
分页中按钮的定制(注意在初始化之前配置好)
$config['first_link'] = '首页';
...
$config['uri_segment'] =3;//分页数据查询偏移量
在url的哪一段上,对应上面的$offset
默认是3,否则需要修改对应值
//加载session库
$this->load->library('session');//比如获取客户端的ip地址
$this->session->userdata('ip_address');//添加
$this->session->set_userdata('some_name', 'some_value');
//获取
$this->session->userdata('some_name');
//删除
$this->session->unset_userdata('some_name');//添加
$this->session->set_flashdata('item', 'value');
//获取
$this->session->flashdata('item');
登录数据中 返回登录前的那一个页面的url可以记录下来,
注意:一次性的数据,读取一次后会自动销毁。
为了确保安全,在config.php生成随机加密的字符串中加入
$config['encryption_key']="fjkdsffjkhjd#kjh";
是否要将cookie加密
$config['sess_encrypt_cookie'] =TRUE;<form action="<?php echo site_url('user/upload');?>" enctype="multipart/form-data">
<input type="file" name="pic"/>
<input type="submit" value="submit">
</form>
//上传处理
$config['upload_path']="./upload";
$config['allowed_types']='gif|jpeg|jpg';
$this->load->library('upload',$config);
$this->upload->do_upload('pic');
文件上传的数据
$filedata = $this->upload->data();//生成验证码
$this->load->helper('captcha');
$this->load->helper('url');
$vals = array(
'word'=>rand(1000,9999),
'img_path'=>'./captcha/',
'img_url'=>base_url().'/captcha/'
'img_width'=>'150',
'img_height'=>'100',
'expiration'=>7200
);
$cap = create_captcha($vals);
echo $cap['image'];
//将验证码获取的数字放在session中
session_start();
$_SESSION['cap'] = $cap['word'];
原文:http://blog.csdn.net/wujiangwei567/article/details/44565453