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

常用类NSingleton

常用的一个小工具类吧,为已经使用过的PHP类建立一个实例,也可以防止代码重复加载。

PHP代码
  1. class NSingleton  
  2. {  
  3.     static $instance;  
  4.   
  5.     static public function get($className$autoInit = true) {  
  6.         if (!$className || !is_string($className)) {  
  7.             require_once ('Empty.php');  
  8.             return new NEmpty(); // or return false;  
  9.         }  
  10.         self::set($className$autoInit);  
  11.         return self::$instance;  
  12.     }  
  13.   
  14.     static protected function set($className$autoInit = true) {  
  15.         if (isset(self::$instance[$key]) && self::$instance[$key] != false) {  
  16.             return;  
  17.         }  
  18.         if (!class_exists($className)) {  
  19.             try {  
  20.                 $classFile = sprintf('%s.php',$className);  
  21.                 require_oncesubstr($classFile, 1) ); //remove 'N'  
  22.             } catch (Exception $e) {  
  23.                 trigger_error('class file not exists');  
  24.                 self::$instance[$key] = false;  
  25.             }  
  26.         }  
  27.         $instance = new $className;  
  28.         if ($auoInit && method_exists($instance'init')) {  
  29.             $instance->init();  
  30.         }  
  31.         self::$instance[$key] = $instance;  
  32.     }  
  33. }  
代码写的其实没什么,主要是简化一些操作和代码的加载。。。(用了这个方法加载类,很有可能无法在IDE被识别)

Tags: singleton, instance

Installing Memcached for PHP 5.3 on Windows 7

说出来让我郁闷,memcached -d install。。。一直无法安装成服务,所以,我每次在运行的时候都是memcached -d,窗口不会关闭。。。很丑。

看看老外怎么说,它来自http://shikii.net/blog/installing-memcached-for-php-5-3-on-windows-7/:

Updated:

First off, all credits go to this guy. I’m just listing the steps on how I did it in Windows 7 with PHP 5.3. Also, I tested this using WampServer but I believe it should work on any PHP install.

Install memcached

  1. Download the Memcached Win32 library here: http://code.jellycan.com/memcached. Just get the Win32 binary (direct link). Extract the downloaded archive file in a directory (e.g. c:\memcached). There should be a memcached.exe in there.
  2. Run a command prompt as an administrator. Some info on how to do that here. 【可能就是因为这个,所以我无法装成服务???】
  3. Install memcached as a service. Go to the memcached directory, type and run:

    memcached -d install

    If you get an error saying “MSVCP71.dll is missing”, see this page for a solution.

  4. Start the memcached service by running:

    memcached -d start
  5. You can verify if memcached is running by executing this in the command line:

    wmic process get description, executablepath | findstr memcached.exe

    You should see a result list showing memcached.exe and its full path.

Install PHP Memcache extension (php_memcache.dll)

  1. Chances are you don’t have php_memcache.dll in your PHP extensions yet. You can download a build of it here. Basu has noted in the comments that VC6 builds are no longer available from that link. You can download the correct build here.
  2. The archive should contain php_memcache.dll. Extract the archive to your php extensions directory. On my system (WampServer), this was C:\wamp\bin\php\php5.3.0\ext.
  3. Edit php.ini, add this line to enable the extension:

    extension=php_memcache.dll

    Or if you’re using WampServer, restart it and enable the extension through the WampServer system tray menu.

Test

Test the installation using the sample PHP code here: http://www.php.net/manual/en/memcache.examples-overview.php.

Tags: memcached

学习HTML中的意外发现:xmp,plaintext

最近被人劝唆,准备捡起HTML了。。想从事一下DIV CSS的工程,准备转行做前端吧。。。说说而已,能力太差。
早上借了强哥的书在看,随手一翻,居然翻到了一页,说是在HTML页面里把HTML标记正常显示出来,这个标记好象以前都没有看过。以前只注意了把HTML内容格式化输出。

这两个标签是xmp和plaintext。

例如<xmp><input type="text" /></xmp><plaintext><input type="text" />

这两个标签里的内容,在firebug里可以看出来,被转成类似htmlspecialchar处理过的代码,所以,它们就直接显示在页面上了,也就是说,它里面的内容,如果想再转为标准的DOM结构,还得再作处理,怪不得很多大网站在做DOM转换的时候,都是情愿存存储到textarea里,毕竟在文本框里面,HTML都不用做转义,那是多么开心的事情啊。。。

PS:plaintext是单标签,所以,在它之处的所有内容全部变成了HTML输出了。。。小心使用啊。

白激动了。

Tags: html, xmp, plaintext

UCHOME开发中遇到的问题

最近在做uchome二次开发的时候遇到一个小问题:强制登录。
什么是强制登录,即,我们在任何 时候,只要点击登录,就显示登录框,同时清除原来所有的登录状态信息。为什么呢,是因为担心在同步登录的时候,如果有某个频道没有同步成功,在点击登录的时候,会显示当前用户已登录,而不会触发登录了。所以必须强制清除登录状态。

于是根据这个需求,简单的在do_login里进行了cleancookie处理。然后而是加了一句:unset($_SGLOBAL['supe_uid']);

结果,几个FORM框里生成的formhash()值都不一样了。导致直接在提交的时候说来路不正常。

检查了很久(不过这次不是我解决的),发现居然是那个unset($_SGLOBAL['supe_uid']);导致的,但是看了formhash这个函数,里面是针对$_SGLOBAL['formhash']变量,没有和supe_uid有关。。。

所以感觉有点妖异。包括把$_SGLOBAL['supe_uid']设为0,设为null都没有关系,只是unset后就出错了。。。

又是一个没有时间解决的问题,先知道解决方法就成了。。。。记录一下。

Tags: uchome, unset, sglobal

HTTP 204和205的应用

上次看的那篇博客里介绍说到一点关于HTTP的code的问题,在jquery里面已经在1.5里被改进了。上次的问题:

XML/HTML代码
  1. 原本对于“请求成功”的浏览器状态码,除200-299以及304外,还有一个1223,来自于IE的一个BUG,会将204的状态码变成 1223。现在因为有了jXHR对象,相当于中间多了一层,因此从jXHR对象获取statusCode不会出现1223的情况,已经被变回204了。  

说实话,我对于204代码真的不知道有具体的作用,因为事实上,我们很少会用到,往往 返回的时候,要么返回1、0之类的,就要么是JSON之类的数据。

然而这篇文章,就说明了一些简单的理由:

内容由此开始:。。。。。。

之前和人讨论过这个问题,,, 今天感冒在家休息, 就回忆了一下, 整理如下.

我们很多的应用在使用Ajax的时候, 大多数情况都是询问型操作, 比如提交数据, 则Ajax只是期待服务器返回:

JavaScript代码
  1. {status: 0, message:""//status 0代表成功, 非零的时候, message中包含出错信息.  

我们知道HTTP的状态码, 2xx都是表示成功, 而HTTP的204(No Content)响应, 就表示执行成功, 但是没有数据, 浏览器不用刷新页面.也不用导向新的页面.

在HTTP RFC 2616中关于204的描述如下:

XML/HTML代码
  1. If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent’s active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent’s active view.  

类似的还有205 Reset Content, 表示执行成功, 重置页面(Form表单).

XML/HTML代码
  1. The server has fulfilled the request and the user agent SHOULD reset the document view which caused the request to be sent. This response is primarily intended to allow input for actions to take place via user input, followed by a clearing of the form in which the input is given so that the user can easily initiate another input action.  

于是, 当有一些服务, 只是返回成功与否的时候, 可以尝试使用HTTP的状态码来作为返回信息, 而省掉多余的数据传输, 比如REST中的DELETE和如上所述的查询式Ajax请求.

最后说说205, 205的意思是在接受了浏览器POST请求以后处理成功以后, 告诉浏览器, 执行成功了, 请清空用户填写的Form表单, 方便用户再次填写,

总的来说, 204适合多次对一个Item进行更新, 而205则适合多次提交一个系列的Item.

但, 请注意, 目前还没有一个浏览器支持205, 大部分的浏览器, 都会把205当做204或者200同样对待.

------------

 

 

事实上,大多数时候,我们都是只利用200状态,然后返回一些数据来决定到底返回怎么样的数据喽。。。

上面的文章来自于:http://www.laruence.com/2011/01/20/1844.html,主要在于有次讨论204的时候涉及到的事情,然后遇到了就记下来了

 

 

 

 

 

Tags: http, jquery, 204