加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

CodeIgniter学习笔记五:分页,文件上传,session,验证码

发布时间:2020-05-25 03:27:24 所属栏目:PHP 来源:互联网
导读:一、分页示例代码://装载类文件$this - load - library(pagination);$controller = $this-router-fetch_class();$action = $this-router-fetch_method();//每页显示10条数据,共有101条数据$config[base_url] = site_url($controller/$action);$config[total

一、分页

示例代码:

-> load -> library('pagination'<span style="color: #800080;">$controller = <span style="color: #800080;">$this->router-><span style="color: #000000;">fetch_class();
<span style="color: #800080;">$action = <span style="color: #800080;">$this->router-><span style="color: #000000;">fetch_method();

<span style="color: #008000;">//<span style="color: #008000;">每页显示10条数据,共有101条数据
<span style="color: #800080;">$config['base_url'] = site_url("<span style="color: #800080;">$controller/<span style="color: #800080;">$action"<span style="color: #000000;">);
<span style="color: #800080;">$config['total_rows'] = 101<span style="color: #000000;">;
<span style="color: #800080;">$config['per_page'] = 10<span style="color: #000000;">;
<span style="color: #800080;">$config['first_link'] = "首页"<span style="color: #000000;">;
<span style="color: #800080;">$config['prev_link'] = "上一页"<span style="color: #000000;">;
<span style="color: #800080;">$config['next_link'] = "下一页"<span style="color: #000000;">;
<span style="color: #800080;">$config['last_link'] = "最后一页"<span style="color: #000000;">;

<span style="color: #008000;">//<span style="color: #008000;">如果setment不是3需要设置,默认为3
<span style="color: #800080;">$config['uri_segment'] = 3<span style="color: #000000;">;

<span style="color: #800080;">$this -> pagination -> initialize(<span style="color: #800080;">$config<span style="color: #000000;">);

<span style="color: #008000;">//<span style="color: #008000;">偏移量
<span style="color: #800080;">$offset = <span style="color: #008080;">intval(<span style="color: #800080;">$this -> uri -> segment(3<span style="color: #000000;">));

<span style="color: #800080;">$sql = "SELECT * FROM TEST LIMIT <span style="color: #800080;">$offset," . <span style="color: #800080;">$config['per_page'<span style="color: #000000;">];

<span style="color: #800080;">$pager = <span style="color: #800080;">$this -> pagination -><span style="color: #000000;"> create_links();

<span style="color: #800080;">$this -> load -> view('article/index',<span style="color: #0000ff;">array('pager' => <span style="color: #800080;">$pager,'sql' => <span style="color: #800080;">$sql));

需要注意的是,如果配置了url中隐藏index.php,在site_url中生成的路径中默认还是有index.php,需要在application/config/config.php中改成如下配置:

['index_page'] = '';

即可。

二、文件上传

示例代码:

['upload_path'] = './uploads/'['allowed_types'] = "gif|png|jpg|jpeg"['max_size'] = 512['file_name'] = <span style="color: #800080;">$this -> load -> library('upload',<span style="color: #800080;">$config<span style="color: #000000;">);
<span style="color: #008000;">//<span style="color: #008000;">pic为input type=file的name
<span style="color: #800080;">$this -> upload ->do_upload('pic'<span style="color: #000000;">);

<span style="color: #008000;">//<span style="color: #008000;">上传文件信息
<span style="color: #800080;">$uploadInfo = <span style="color: #800080;">$this -> upload -><span style="color: #000000;"> data();
<span style="color: #008080;">var_dump(<span style="color: #800080;">$uploadInfo<span style="color: #000000;">);
<span style="color: #008000;">/<span style="color: #008000;">
array (size=14)
'file_name' => string '55a61d04e96f4.jpg' (length=17)
'file_type' => string 'image/jpeg' (length=10)
'file_path' => string 'E:/wamp/www/citest/uploads/' (length=27)
'full_path' => string 'E:/wamp/www/citest/uploads/55a61d04e96f4.jpg' (length=44)
'raw_name' => string '55a61d04e96f4' (length=13)
'orig_name' => string '55a61d04e96f4.jpg' (length=17)
'client_name' => string 'hk.jpg' (length=6)
'file_ext' => string '.jpg' (length=4)
'file_size' => float 125.09
'is_image' => boolean true
'image_width' => int 675
'image_height' => int 900
'image_type' => string 'jpeg' (length=4)
'image_size_str' => string 'width="675" height="900"' (length=24)
<span style="color: #008000;">
/

即可。

三、session

1、开启

-> load -> library('session');

默认已经开启,用上面的代码加载session库。

2、写入

= 'id' => 3,'name' => 'jim' -> session -> set_userdata("user",);

-> session -> userdata('user')

注:建议在 application/config/config.php 中配置加密key:

['encryption_key'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

四、验证码

示例:

->load->helper('captcha' = 'Random word', 'img_path' => './captcha/','img_url' => base_url() . 'captcha/', './path/to/fonts/texb.ttf', 'img_width' => '200','img_height' => 80,'expiration' => 60,'word_length' => 10,'font_size' => 26,'img_id' => 'Imageid','pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'colors' => 'background' => (255,255,255),'border' => (255,'text' => (0,0),'grid' => (255,40,40<span style="color: #800080;">$cap = create_captcha(<span style="color: #800080;">$vals<span style="color: #000000;">);
<span style="color: #008080;">var_dump(<span style="color: #800080;">$cap<span style="color: #000000;">);
<span style="color: #008000;">/<span style="color: #008000;">
array (size=4)
'word' => string 'hFan3NM4xB' (length=10)
'time' => float 1436954914.3053
'image' => string '' (length=129)
'filename' => string '1436954914.3053.jpg' (length=19)
<span style="color: #008000;">
/
<span style="color: #0000ff;">echo <span style="color: #800080;">$cap['image'];

注意:需要手动创建存放验证码的文件夹,验证码图片会在超时后自动删除。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读