手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

Yii 错误操作引起的问题

在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

INI配置文件的解析

我不知道怎么说才好,因为我在读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代码
  1. <?php  
  2. class DbaReader implements Iterator  
  3. {  
  4.   
  5.     protected $db = NULL;  
  6.     private $key = false;  
  7.     private $val = false;  
  8.   
  9.     /** 
  10.      * Open database $file with $handler in read only mode. 
  11.      * 
  12.      * @param file    Database file to open. 
  13.      * @param handler Handler to use for database access. 
  14.      */  
  15.     function __construct($file$handler) {  
  16.         if (!$this->db = dba_open($file'r'$handler)) {  
  17.             throw new exception('Could not open file ' . $file);  
  18.         }  
  19.     }  
  20.   
  21.     /** 
  22.      * Close database. 
  23.      */  
  24.     function __destruct() {  
  25.         dba_close($this->db);  
  26.     }  
  27.   
  28.     /** 
  29.      * Rewind to first element. 
  30.      */  
  31.     function rewind() {  
  32.         $this->key = dba_firstkey($this->db);  
  33.         $this->fetch_data();  
  34.     }  
  35.   
  36.     /** 
  37.      * Move to next element. 
  38.      * 
  39.      * @return void 
  40.      */  
  41.     function next() {  
  42.         $this->key = dba_nextkey($this->db);  
  43.         $this->fetch_data();  
  44.     }  
  45.   
  46.     /** 
  47.      * Fetches the current data if $key is valid 
  48.      */  
  49.     private function fetch_data() {  
  50.         if ($this->key !== false) {  
  51.             $this->val = dba_fetch($this->key, $this->db);  
  52.         }  
  53.     }  
  54.   
  55.     /** 
  56.      * @return Current data. 
  57.      */  
  58.     function current() {  
  59.         return $this->val;  
  60.     }  
  61.   
  62.     /** 
  63.      * @return Whether more elements are available. 
  64.      */  
  65.     function valid() {  
  66.         if ($this->db && $this->key !== false) {  
  67.             return true;  
  68.         } else {  
  69.             return false;  
  70.         }  
  71.     }  
  72.   
  73.     /** 
  74.      * @return Current key. 
  75.      */  
  76.     function key() {  
  77.         return $this->key;  
  78.     }  
  79. }  
  80. ?>  

DbaReader使用Iterator接口,当然要实现里面的5个迭代方法。迭代方法对handlerhandlerINI文件的解析,用到了dba扩展。

说点题外话,什么是Dba?为什么使用Dba?
Dba是一款数据库,确切点说,是一款索引化的文件存储系统。适合相对比较静态的索引化的数据存储。所有版本的Linux都会带此数据库。
既然使用文件来存储数据,为什么还有使用Dba呢?原因有二:
1数据记录的存储长度可以不是固定的;
2使用索引存储和检索数据。

DbaReader提供一个访问INI文件数据的迭代方法,如果需要存储删除数据呢?所以DbaArray在继承DbaReader的基础上,实现了此功能。

PHP代码
  1. <?php  
  2. class DbaArray extends DbaReader implements ArrayAccess  
  3. {  
  4.   
  5.     /** 
  6.      * Open database $file with $handler in read only mode. 
  7.      * 
  8.      * @param file    Database file to open. 
  9.      * @param handler Handler to use for database access.取值http://www.php.net/manual/en/dba.requirements.php 
  10.      */  
  11.     function __construct($file$handler)  
  12.     {  
  13.         $this->db = dba_popen($file"c"$handler);  
  14.         if (!$this->db) {  
  15.             throw new exception("Databse could not be opened");  
  16.         }  
  17.     }  
  18.   
  19.     /** 
  20.      * Close database. 
  21.      */  
  22.     function __destruct()  
  23.     {  
  24.         parent::__destruct();  
  25.     }  
  26.   
  27.     /** 
  28.      * Read an entry. 
  29.      * 
  30.      * @param $name key to read from 
  31.      * @return value associated with $name 
  32.      */  
  33.     function offsetGet($name)  
  34.     {  
  35.         $data = dba_fetch($name$this->db);  
  36.             if($data) {  
  37.             if (ini_get('magic_quotes_runtime')) {  
  38.                 $data = stripslashes($data);  
  39.             }  
  40.             //return unserialize($data);  
  41.             return $data;  
  42.         }  
  43.         else  
  44.         {  
  45.             return NULL;  
  46.         }  
  47.     }  
  48.   
  49.     /** 
  50.      * Set an entry. 
  51.      * 
  52.      * @param $name key to write to 
  53.      * @param $value value to write 
  54.      */  
  55.     function offsetSet($name$value)  
  56.     {  
  57.         //dba_replace($name, serialize($value), $this->db);  
  58.         dba_replace($name$value$this->db);  
  59.         return $value;  
  60.     }  
  61.   
  62.     /** 
  63.      * @return whether key $name exists. 
  64.      */  
  65.     function offsetExists($name)  
  66.     {  
  67.         return dba_exists($name$this->db);  
  68.     }  
  69.   
  70.     /** 
  71.      * Delete a key/value pair. 
  72.      * 
  73.      * @param $name key to delete. 
  74.      */  
  75.     function offsetUnset($name)  
  76.     {  
  77.         return dba_delete($name$this->db);  
  78.     }  
  79. }  
  80. ?>  
使用范例
构建文件text.ini,内容如下:
XML/HTML代码
  1. host = localhost  
  2. password = password  
  3. database = data  

 

文件index.php.代码如下:

PHP代码
  1. <?php  
  2. function loadClass($class)  
  3. {  
  4.     require_once __DIR__.DIRECTORY_SEPARATOR.$class.'.php';  
  5. }  
  6. spl_autoload_register('loadClass',false);  
  7.   
  8. $iniFile = __DIR__.DIRECTORY_SEPARATOR.'test.ini';  
  9.   
  10. $ini = new DbaArray($iniFile,'iniFile');  
  11. echo $ini['database'];  
  12. var_dump($ini);  
  13. ?>  
--EOF--

看完上面这一段,是不是有什么想法?原来ini的操作也是这么的方便?不过,如果是纯读取的话,我还是比较推荐于parse_ini_file之类的(突然间忘了,如果编码不一样怎么办?ansi/utf-8,这真是一个永恒的痛。)

领域驱动

说实话,用PHP来实现领域驱动,是一个很蛋疼的想法。。。。外面关于领域驱动的文章很多,但真正能适应PHP的真的很少。几乎没有,因此相对的所有相关文章只能作作参考了。

这里有一系列的文章 ,可以先参考一下:

对领域驱动设计的初步认识(一)

对领域驱动设计的初步认识(二)
对领域驱动设计的初步认识(三)

对领域驱动设计的初步认识(四)

对领域驱动设计的初步认识(五)

对领域驱动设计的初步认识(六)

对领域驱动设计的初步认识(七)

对领域驱动设计的初步认识(八)

对领域驱动设计的初步认识(九)

对领域驱动设计的初步认识(十)


先将就着看看吧,关于领域驱动的书也有很多。只是能看得进的真的不多啊

URL to Barcode

以前,我说过有关于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, 二维码

一张对前端有意义的图片

一些页面的宽高值,来自风干的果子。

前端对于这些值应该会比较关注吧。对于我这种虽然不从事前端,但了解一下也是很必要的。

大小: 190.37 K
尺寸: 372 x 376
浏览: 1748 次
点击打开新窗口浏览全图

Tags: 前端