PHP中CodeIgniter入门教程——第十课 调用钩子
CodeIgniter 的钩子的功能是让你在不修改系统核心文件的基础上来改变或增加系统的核心运行功能。 当 CodeIgniter 运行后,它会产生出一个特殊的进程。
钩子功能可以在全局范围内打开或关闭,您可以在 application/config/config.php 文件中设定。
下面的钩子函数 是返回浏览器类型、关键字、搜索引擎。
get_engine.php
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
<?php /** * 返回浏览器类型、关键字、搜索引擎 */ class get_engine extends CI_Controller { function get_e(){ if (empty($_SERVER['HTTP_REFERER'])) { return; } $url = $_SERVER['HTTP_REFERER']; $arr = $this->save_www_iiwnet_com_keyword($url,$url); $keywords = $arr['keywords']; $searchengine = $arr['searchengine']; $ip = $this->input->ip_address(); $browse = $this->input->user_agent(); // $data = array( // 'keywords' => $keywords, // 'searchengine' => $searchengine, // 'ip' => $ip, // 'browse' => $browse // ); // $this->db->insert('engine',$data); return $data; } function save_www_iiwnet_com_keyword($domain,$path){ $keywords = ''; $searchengine = ''; if(strpos($domain, 'google.com.tw')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){ $searchengine = 'GOOGLE TAIWAN'; $keywords = urldecode($regs[1]); // google taiwan } if(strpos($domain,'google.cn')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){ $searchengine = 'GOOGLE CHINA'; $keywords = urldecode($regs[1]); // google china } if(strpos($domain,'google.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){ $searchengine = 'GOOGLE'; $keywords = urldecode($regs[1]); // google }else if(strpos($domain,'baidu.')!==false && preg_match('/wd=([^&]*)/i',$path,$regs)){ $searchengine = 'BAIDU'; $keywords = urldecode($regs[1]); // baidu }else if(strpos($domain,'baidu.')!==false && preg_match('/word=([^&]*)/i',$path,$regs)){ $searchengine = 'BAIDU'; $keywords = urldecode($regs[1]); // baidu }else if(strpos($domain,'114.vnet.cn')!== false && preg_match('/kw=([^&]*)/i',$path,$regs)){ $searchengine = 'CT114'; $keywords = urldecode($regs[1]); // ct114 }else if(strpos($domain,'iask.com')!==false && preg_match('/k=([^&]*)/i',$path,$regs)){ $searchengine = 'IASK'; $keywords = urldecode($regs[1]); // iask }else if(strpos($domain,'soso.com')!==false && preg_match('/w=([^&]*)/i',$path,$regs)){ $searchengine = 'SOSO'; $keywords = urldecode($regs[1]); // soso }else if(strpos($domain, 'sogou.com')!==false && preg_match('/query=([^&]*)/i',$path,$regs)){ $searchengine = 'SOGOU'; $keywords = urldecode($regs[1]); // sogou }else if(strpos($domain,'so.163.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){ $searchengine = 'NETEASE'; $keywords = urldecode($regs[1]); // netease }else if(strpos($domain,'yodao.com')!== false && preg_match('/q=([^&]*)/i',$path,$regs)){ $searchengine = 'YODAO'; $keywords = urldecode($regs[1]); // yodao }else if(strpos($domain,'zhongsou.com')!==false && preg_match('/word=([^&]*)/i',$path,$regs)){ $searchengine = 'ZHONGSOU'; $keywords = urldecode($regs[1]); // zhongsou }else if(strpos($domain,'search.tom.com')!==false && preg_match('/w=([^&]*)/i',$path,$regs)){ $searchengine = 'TOM'; $keywords = urldecode($regs[1]); // tom }else if(strpos($domain,'live.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){ $searchengine = 'MSLIVE'; $keywords = urldecode($regs[1]); // MSLIVE }else if(strpos($domain, 'tw.search.yahoo.com')!==false && preg_match('/p=([^&]*)/i',$path,$regs)){ $searchengine = 'YAHOO TAIWAN'; $keywords = urldecode($regs[1]); // yahoo taiwan }else if(strpos($domain,'cn.yahoo.')!==false && preg_match('/p=([^&]*)/i',$path,$regs)){ $searchengine = 'YAHOO CHINA'; $keywords = urldecode($regs[1]); // yahoo china }else if(strpos($domain,'yahoo.')!==false && preg_match('/p=([^&]*)/i',$path,$regs)){ $searchengine = 'YAHOO'; $keywords = urldecode($regs[1]); // yahoo }else if(strpos($domain,'msn.com.tw')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){ $searchengine = 'MSN TAIWAN'; $keywords = urldecode($regs[1]); // msn taiwan }else if(strpos($domain,'msn.com.cn')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){ $searchengine = 'MSN CHINA'; $keywords = urldecode($regs[1]); // msn china }else if(strpos($domain,'msn.com')!==false && preg_match('/q=([^&]*)/i',$path,$regs)){ $searchengine = 'MSN'; $keywords = urldecode($regs[1]); // msn } if ($keywords == '') { $keywords = $path; } if ($searchengine == '') { $searchengine == $path; } return array('keywords'=>$keywords,'searchengine'=>$searchengine); } } ?> |
在config.php下hook.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /* | ------------------------------------------------------------------------- | Hooks | ------------------------------------------------------------------------- | This file lets you define "hooks" to extend CI without hacking the core | files. Please see the user guide for info: | | http://codeigniter.com/user_guide/general/hooks.html | */ $hook['pre_controller'] = array( 'class' => 'get_engine', 'function' => 'get_e', 'filename' => 'get_engine.php', 'filepath' => 'hooks', 'params' => array() ); /* End of file hooks.php */ /* Location: ./application/config/hooks.php */ |
到这里了,学的差不多了,接下来就是自己研究了。谢谢。
- PHP中CodeIgniter入门教程——第九课 调用函数
- 淘点金js代码自动跳转到爱淘宝(淘宝客)