Submitted by gouki on 2009, February 23, 4:41 PM
一般我们在设置数据库字符集的时候,都是用mysql_query('set names utf8')这样来处理,但其实这样处理的不全,象discuz等都是采用了全部的操作,它是这样的:mysql_query('character_set_connection=utf8, character_set_results=utf8 character_set_client=binary'),也只有这样,才会比较全面。
仔细看手册 ,手册上也这样写着:
Note: This is the preferred way to change the charset. Using mysql_query() to execute SET NAMES .. is not reccomended.
那么,我们该怎么处理呢?从PHP5.2.3开始多了一个函数,它就是:mysql_set_charset,你可以:
PHP代码
- <?
- $conn = ....
- if( mysql_client_encoding ( $conn) != 'utf8' ){
- mysql_set_charset('utf8');
- }
这样才是比较推荐的用法。不过,这样的用法也是有要求的,手册告诉我们:
Note: This function requires MySQL 5.0.7 or later.
传说需要采用这样的方法,是因为set names在某些字符集的时候有安全隐患。
Tags: mysql, charset, php
PHP | 评论:0
| 阅读:23632
Submitted by gouki on 2009, February 18, 3:12 PM
以前也曾经说过,IBM的Developer网站上对于open source之类的介绍比较多,在这些介绍中,也有一部份关于PHP的,这两天在仔细的查找资料时,发现了一个页面,它就是IBM的PHP资源页面,这是对一些PHP资料进行的归档,通过本页,你可以查询一些比较经典的文章。
URL为:http://www.ibm.com/developerworks/cn/opensource/top-projects/php-resources.html
复制一部分如下:
并不全哦,要全的话,还是点最上那个PHP资源的链接,HOHO
Tags: ibm, php, 学习资料
PHP | 评论:0
| 阅读:22941
Submitted by gouki on 2009, February 13, 3:06 PM
用file_get_contents来进行POST?很妖吧。
我也没有想到还有这种妖的东西。在向东的博客上看到这个的:http://www.xiangdong.org/blog/post/1623/
回忆未来?别惊讶,用他的话来说是山寨D。
原文:
file_get_contents.php: Post数据
PHP代码
- <?php
- function Post($url, $post = null)
- {
- $context = array();
-
- if (is_array($post))
- {
- ksort($post);
-
- $context['http'] = array
- (
- 'method' => 'POST',
- 'content' => http_build_query($post, '', '&'),
- );
- }
-
- return file_get_contents($url, false, stream_context_create($context));
- }
-
- $data = array
- (
- 'name' => 'test',
- 'email' => 'test@gmail.com',
- 'submit' => 'submit',
- );
-
- echo Post('http://localhost/5-5/request_post_result.php', $data);
- ?>
接收数据:
request_post_result.php 接收经过Post的数据:
PHP代码
- <?php
- echo $_POST['name'];
- echo $_POST['email'];
- echo $_POST['submit'];
- echo "fdfd";
- ?>
看一下,里面有一个特别的函数:stream_context_create,翻开手册看了一下,也没说什么呀,只是说:Creates and returns a stream context with any options supplied in options preset.
而file_get_contents呢?它说:
Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Reference CLX, Stream Functions.
关于Stream Functions,手册上这么描述的。。。
A wrapper is additional code which tells the stream how to handle specific protocols/encodings. For example, the http wrapper knows how to translate a URL into an HTTP/1.0 request for a file on a remote server. There are many wrappers built into PHP by default (See Appendix O), and additional, custom wrappers may be added either within a PHP script using stream_wrapper_register(), or directly from an extension using the API Reference in Chapter 52. Because any variety of wrapper may be added to PHP, there is no set limit on what can be done with them. To access the list of currently registered wrappers, use stream_get_wrappers().
A stream is referenced as: scheme://target
-
scheme(string) - The name of the wrapper to be used. Examples include: file, http, https, ftp, ftps, compress.zlib, compress.bz2, and php. See Appendix O for a list of PHP built-in wrappers. If no wrapper is specified, the function default is used (typically file://).
-
target - Depends on the wrapper used. For filesystem related streams this is typically a path and filename of the desired file. For network related streams this is typically a hostname, often with a path appended. Again, see Appendix O for a description of targets for built-in streams.
Tags: php, post
PHP | 评论:5
| 阅读:48803
Submitted by gouki on 2009, February 11, 5:10 PM
总是有一些奇怪的事情发生,我刚刚买了<HeadFirst 设计模式>,就有人写了如题的一篇文章。
我是用PHP的,而书上讲的例子是用伪代码(以java为蓝本)的,我也没有看多少呢,也没有想过要来实现一下,因为有些东西使用PHP的话,只会效率更低,解释型的,IO能节约还是要节约一下。
总想等看完再实现这些(第一大章还没有看完呢。)
不过,既然有人写了,总要宣扬一下的吧,代码虽然不一样,但思想总是一样的。
我也没有仔细看,就权当收藏吧。。
链接如下:
讲了什么呢?
摘要如下:
策略模式的设计原则如下:
1. 将应用中需要经常变化的代码独立出来,应和那些不需要经常变化的代码分开。
2. 应针对接口,而不是类进行编程。
3. 在类中应多用组合,少用继承。
例子:
我们要实现一个鸭子模拟器,这个鸭子模拟器由Duck类描述,而Duck类有如下4个行为:
1. display
2. swim
3. fly(飞)
4. quack(叫)
其中swim是所有鸭子都具有的特性,而且所有鸭子的这些特性都相同,因此,这个方法可以直接在Duck类中实现。display方法也是所有鸭子具有的 特性,但随着鸭子的种类不同,display也有所不同,因此,display方法应为Duck类的抽象方法。fly和quack并不是所有鸭子的特性, 如橡皮鸭子即不会飞,也不会叫。因此,可以将这两个方法看作是两个行为,可将每一个行为设计成一个接口。这样可以和Duck类完全脱离。因为,fly和 quack与Duck一点关系都没有(别的东西也可能fly和quack),然后不同的fly和quack分别用实现相应接口的类表示。
Tags: headfirst, 模式, 策略模式, strategy
PHP | 评论:1
| 阅读:22213
Submitted by gouki on 2009, February 5, 10:19 PM
在线截图,对于PHP来说是有一点难度的,但也并非不可完成
从PHP5开始,为windows平台提供了两个现成的函数,但也只能在windows平台下使用,这两个函数其实还是使用了IE浏览器来进行截图,在使用这两个函数的时候,你可以明显看到IE会一闪而过。
不过在非windows平台下,就没有办法使用了。
为了实现这些功能,网上有很多例子哦,有使用mozilla核心的,有使用XXX核心的,但确实是没有一种完美的实现方案。
自己想想还有一种就是利用flash截图,然后生成图片的方案。这种的话就无所谓在哪个平台下面了吧?
网上有一些在线截图的:
http://snapcasa.com
http://webthumb.bluga.net
但没有一款是PHP的。不过,正所谓事情都是人想出来的,我既然不能使用PHP截图,但是我可以利用这些网址截好图,用PHP抓回来嘛。。
黑黑
Tags: 截图, snapshot, webcatch
PHP | 评论:1
| 阅读:22413