PHP中CodeIgniter入门教程——第五课 类库介绍
数据库操作学习了, 一般web语言的要求就是数据库的增删改查,那基本上就差不多了。
简单介绍一下CodeIgniter的一些内置类库
邮件类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/*邮件类 start*/ public function send_email(){ date_default_timezone_set("PRC"); $useremail='674531003@qq.com'; $message='test邮件'; $this->load->library('email'); //加载CI的email类 //以下设置Email参数 $config['protocol'] = 'smtp'; $config['smtp_host'] = 'smtp.126.com'; $config['smtp_user'] = 'xxxxx'; $config['smtp_pass'] = 'xxxxxxx'; $config['smtp_port'] = '25'; $config['charset'] = 'utf-8'; $config['smtp_timeout'] = '5'; $config['validate'] = TRUE; $config['mailtype'] = 'html';//text和html $this->email->initialize($config); //以下设置Email内容 $this->email->from('www.9you9.cn', '九邮九'); $this->email->to($useremail); $this->email->subject('验证邮箱'); $this->email->message($message); if (!$this->email->send()){ echo '发送失败!'; }else{ echo '发送成功!'; } } /*邮件类 end*/ |
session类库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/* session start*/ public function session() { $this->load->library('session'); $data = array( 'username' => 'www.phperblog.cn', 'email' => 'www.9you9.cn', ); $this->session->set_userdata('data',$data); $result=$this->session->userdata('data'); var_dump($result); $this->load->view('welcome_message'); } /* session end*/ |
报错了?In order to use the Session class you are required to set an encryption key in your config file.
在config.php配置文件
1 |
$config['encryption_key'] = 'www.phperblog.cn'; |
就解决了。
URI类
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/* URI类 start*/ public function uri() { //$str="http://www.9you9.cn/index.php/Goods/nine"; $product_id = $this->uri->segment(2, 0); echo $product_id; //输出的内容为 uri =>相当于方法名 $this->load->view('welcome_message'); } /* URI类 end*/ |
Agent类 浏览器类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* agent类 start*/ public function agent(){ $this->load->library('user_agent'); if ($this->agent->is_browser()){ $agent = $this->agent->browser().' '.$this->agent->version(); }elseif ($this->agent->is_robot()){ $agent = $this->agent->robot(); }elseif ($this->agent->is_mobile()){ $agent = $this->agent->mobile(); }else{ $agent = 'Unidentified User Agent'; } echo $agent; echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.) $this->load->view('welcome_message'); } /* agent类 end*/ |
XSS 过滤
建议开启
Page类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
/* page分页类 start*/ function page($index = 0) { $this->load->database(); $per_page = 1; $num = (int)$index; $count = $this->db->query('select count(id) totalnum from users')->row()->totalnum; $this->load->library('pagination'); $config['base_url'] = "welcome/page"; $config['total_rows'] = $count; $config['per_page'] = $per_page ; //每页显示的数据数量 $config['first_link'] = '首页'; $config['last_link'] = '尾页'; $config['prev_link'] = '上一页'; $config['next_link'] = '下一页'; $config['cur_tag_open'] = '<font color="#FF6600">'; $config['cur_tag_close'] = '</font> '; $this->pagination->initialize($config); $pages = $this->pagination->create_links(); $this->load->view('welcome_message',array( "pages" =>$pages, )); } /* page分页类 end*/ |
自定义类写在libraries目录下
调用:
1 2 3 4 5 |
$this->load->library('Aaa'); $result=$this->aaa->ss(); echo $result; |
- PHP中CodeIgniter入门教程——第四课 数据库操作
- PHP中CodeIgniter入门教程——第六课 函数介绍