Submitted by gouki on 2010, December 28, 11:15 AM
在YII项目实施过程中,遇到一个问题,即,只在某一个module里需要获取IP的来源,并判断具体来自于何处。
默认在yii里取得客户端IP只有一个简单的方法,来自于ChttpRequest类,只是这里只有一个判断,即Remote_addr,而这个,一般来说是不准的,因此,我直接COPY了康盛的IP来源判断。
然后通过那个比较出名的iplocation类读取wry.dat,来获取IP的定位(iplocation仿佛是几年前王总写的还是andot写的?用来分析纯真IP库的。说起这个wry.dat,事实上,它的来源还真是要从以前的一款软件:追捕说起,但那个年代远了,早就消失在历史的长河中了。)
好吧,上面就是我要说的背景,于是,我认为,既然要在module里获取,那么我就在module的init方法里直接实现了吧。
然后,我把具体的获取IP,并且分析IP来源,并到数据库查询以获取Area_id的代码扔在了module 的 init 方法里,然后运行一看,直接502 bad gateway出现了。折腾了好久,发现都一直是502。
但是测试了一下,把这些代码COPY到Controller的action里面,又十分正常(因为没空研究module的实现),于是就把代码COPY到module的beforeAction方法中了。
然后问题解决。(没仔细想为什么,只是纯粹一个记录,让自己知道,尽量不要在init里写上一大堆实现代码。。。)
Tags: yii
PHP | 评论:1
| 阅读:18397
Submitted by gouki on 2010, December 27, 1:12 PM
我不知道怎么说才好,因为我在读INI文件的时候,往往都是用现成的函数:parse_ini_file或者是parse_ini_string,但怎么写入,就是另外的方法了(自己实现。。。。)
所以看到这篇文章的时候,我也才刚刚知道,原来,还有一个dba的函数可以用,嗯,仔细看了一下dba这个函数的installtion,发现支持inifile也是从PHP5才开始实现的。好吧,相应的dba相关的可以看看这里:http://www.php.net/manual/en/dba.installation.php,详细的还是看这里吧:http://www.php.net/manual/en/book.dba.php
OK,上原文,它来自于:http://www.cardii.net/php-spl-parse-ini-file/。
曾经介绍过SPL的各类型接口和迭代器。今天,在浏览PHP源码目录时,发现有个解析INI文件的例子,觉得不错,于是整理了一个实例,拿来分享下。
在PHP应用程序中,配置文件不可或缺,特别是商城,CMS之类的产品,不同的客户需求不同,当然,不会每个客户开发一套程序,好办法的是每个客户 有一套不同的配置文件。适合做配置文件的我曾经也说过,主要有四类:PHP数组(几乎其他的配置方法最终都是解析成为PHP数组),XML,YAML和 INI。今天只讲INI文件。ZendFramework使用此配置。
下看个DbaReader类。文件名为 DbaReader.php:
PHP代码
- <?php
- class DbaReader implements Iterator
- {
-
- protected $db = NULL;
- private $key = false;
- private $val = false;
-
-
-
-
-
-
-
- function __construct($file, $handler) {
- if (!$this->db = dba_open($file, 'r', $handler)) {
- throw new exception('Could not open file ' . $file);
- }
- }
-
-
-
-
- function __destruct() {
- dba_close($this->db);
- }
-
-
-
-
- function rewind() {
- $this->key = dba_firstkey($this->db);
- $this->fetch_data();
- }
-
-
-
-
-
-
- function next() {
- $this->key = dba_nextkey($this->db);
- $this->fetch_data();
- }
-
-
-
-
- private function fetch_data() {
- if ($this->key !== false) {
- $this->val = dba_fetch($this->key, $this->db);
- }
- }
-
-
-
-
- function current() {
- return $this->val;
- }
-
-
-
-
- function valid() {
- if ($this->db && $this->key !== false) {
- return true;
- } else {
- return false;
- }
- }
-
-
-
-
- function key() {
- return $this->key;
- }
- }
- ?>
DbaReader使用Iterator接口,当然要实现里面的5个迭代方法。迭代方法对handlerhandlerINI文件的解析,用到了dba扩展。
说点题外话,什么是Dba?为什么使用Dba?
Dba是一款数据库,确切点说,是一款索引化的文件存储系统。适合相对比较静态的索引化的数据存储。所有版本的Linux都会带此数据库。
既然使用文件来存储数据,为什么还有使用Dba呢?原因有二:
1数据记录的存储长度可以不是固定的;
2使用索引存储和检索数据。
DbaReader提供一个访问INI文件数据的迭代方法,如果需要存储删除数据呢?所以DbaArray在继承DbaReader的基础上,实现了此功能。
PHP代码
- <?php
- class DbaArray extends DbaReader implements ArrayAccess
- {
-
-
-
-
-
-
-
- function __construct($file, $handler)
- {
- $this->db = dba_popen($file, "c", $handler);
- if (!$this->db) {
- throw new exception("Databse could not be opened");
- }
- }
-
-
-
-
- function __destruct()
- {
- parent::__destruct();
- }
-
-
-
-
-
-
-
- function offsetGet($name)
- {
- $data = dba_fetch($name, $this->db);
- if($data) {
- if (ini_get('magic_quotes_runtime')) {
- $data = stripslashes($data);
- }
-
- return $data;
- }
- else
- {
- return NULL;
- }
- }
-
-
-
-
-
-
-
- function offsetSet($name, $value)
- {
-
- dba_replace($name, $value, $this->db);
- return $value;
- }
-
-
-
-
- function offsetExists($name)
- {
- return dba_exists($name, $this->db);
- }
-
-
-
-
-
-
- function offsetUnset($name)
- {
- return dba_delete($name, $this->db);
- }
- }
- ?>
使用范例
构建文件text.ini,内容如下:
XML/HTML代码
- host = localhost
- password = password
- database = data
文件index.php.代码如下:
PHP代码
- <?php
- function loadClass($class)
- {
- require_once __DIR__.DIRECTORY_SEPARATOR.$class.'.php';
- }
- spl_autoload_register('loadClass',false);
-
- $iniFile = __DIR__.DIRECTORY_SEPARATOR.'test.ini';
-
- $ini = new DbaArray($iniFile,'iniFile');
- echo $ini['database'];
- var_dump($ini);
- ?>
--EOF--
看完上面这一段,是不是有什么想法?原来ini的操作也是这么的方便?不过,如果是纯读取的话,我还是比较推荐于parse_ini_file之类的(突然间忘了,如果编码不一样怎么办?ansi/utf-8,这真是一个永恒的痛。)
PHP | 评论:1
| 阅读:17420
Submitted by gouki on 2010, December 25, 8:40 PM
说实话,用PHP来实现领域驱动,是一个很蛋疼的想法。。。。外面关于领域驱动的文章很多,但真正能适应PHP的真的很少。几乎没有,因此相对的所有相关文章只能作作参考了。
这里有一系列的文章 ,可以先参考一下:
对领域驱动设计的初步认识(一)
对领域驱动设计的初步认识(二)
对领域驱动设计的初步认识(三)
先将就着看看吧,关于领域驱动的书也有很多。只是能看得进的真的不多啊
PHP | 评论:0
| 阅读:16837
Submitted by gouki on 2010, December 23, 11:42 PM
以前,我说过有关于QR CODE的博客(QR CODE),也推荐了google的开源工具,事实上我的每篇博客右侧也都生成了二维码。
我这里是介绍的别人的文章 ,来自于:http://www.mikespook.com/index.php/archives/793
每当我使用PC上网的时候,总是有一些链接我想分享到手机里。Barcode 2D 正合适做这件事情。所以,你看到了这个“URL to Barcode”。这个简陋的网站帮你将 URL 转换为 Barcode。
使用
- 输入URL;
- 点击“生成 BARCODE”按钮;
- 获取到 BARCODE;
- 使用手机上的条码扫描程序扫描;
- 使用手机里的浏览器打开 URL。
致谢
Barcode 是由 PHP QR Code 生成的。
在 Android 平台,Barcode Scanner 是非常棒的扫描软件。它给了我很多帮助!
在 iOS 平台,我并不清楚哪个软件更好。这个文章可能会给你一些帮助:5 个最好的 Barcode iPhone 应用(英文)。
When I’m using PC browsing the Internet. There will always be some URLs, that I want to share to my mobile. The barcode 2d should be the right thing. So, now you see this “URL to Barcode”. This is a simple site that provides a URL conversion Barcode services.
Usage
- Input the url;
- Press “Make BARCODE”;
- Get the BARCODE;
- Scan it with the scaner software in your mobile;
- Open the url with the browser in your mobile.
Acknowledgements
The barcode was made by PHP QR Code
In the Android, here is a great barcode scaner: Barcode Scanner. It helps me much more!
In the iOS, I don’t konw which software is better. This article should gives you some help: 5 Best Barcode iPhone Applications.
Tags: url, barcode, qrcode, 二维码
PHP | 评论:2
| 阅读:19024
Submitted by gouki on 2010, December 21, 9:35 PM
一些页面的宽高值,来自风干的果子。
前端对于这些值应该会比较关注吧。对于我这种虽然不从事前端,但了解一下也是很必要的。

Tags: 前端
PHP | 评论:0
| 阅读:15533