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

php header 设置Cache

在手册里,关于header函数的说明是说服务端会输出一系列的头,用firebug也可以看得很清楚
一般来说,我们用header控制的情况不是特别多,毕竟不会主动去改什么:一般也就设置设置编码、跳转、压缩等,不太会过多的干涉。下载的时候也会设置头,黑黑

手册上,我们对于cache都是写着如何设置,以便让代码不被cache:


header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Pragma: no-cache"); // Date in the past

而且在设置的时候还得注意在header前不能有输出,否则header设置无效,但都没有写过,如何给页面设置Cache,虽然我们知道有一些办法,比如 E-TAG之类的。当然也有简单的设置:
比如我们在输出前,对内容进行md5,将它当成e-tag只要没变化,就不会有影响。也有其他的方式:

$seconds_to_cache = 3600;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts"); header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");

缓存1小时,主要是过期时间得用gmdate来设置,而不是date,这个要注意,其他都差不多。maxage要和expire能够对得上。

不算笔记的笔记。

Tags: header

粗心,又见粗心

有时候会用到gzip函数,只是有时候而已。。。
于是。。有些函数就会直接用上了。比如 :gzencode,gzdecode,gzcompress,gzuncompress,gzdeflate,gzinflate
在用的时候。。有些是想当然的。。。比如有Gzencode,就一定会有gzdecode吧?错。。。gzdecode暂时还没有正式版本支持,只存在于svn中,虽然手册上有。。但也说明了不能用

gzcompress与gzdeflate的区别,一个是基于RFC1950协议,一个是基于RFC1951,gzcompress因为是用的1950协议,手册上说了,它需要zlib库。gzdeflate就不需要
所以,gzcompress现在用的相对又少了一点。

好吧,现在说说我的粗心了,大家都知道Gzip压缩是有等级的(压缩软件都有,1~9,1最低压缩,9是最高压缩比),用9的话压缩出来的文件最小,但是解压的时候最耗CPU。所以一般 都是level4,其实就算是1也不错了。

粗心的就在于gzinflate的时候,想当然的将第二个参数length想成level了。结果一直在报错。居然我5分钟后才反应过来。NND,太粗心了。。。
第二个参数是length,不是level啊。

Tags: gzip

Yii的又一个BUG?

先申明一下,这个可能不是BUG。只是算起来是实现的方式不一样而已。
场景:我有一个MYSQL数据库,但是现在容量越来越大,主要是因为其中有大字段,多个大字段,所以在查询的时候会特别特别慢。所以想到用mongo来存储一些查询用的结构。单条的时候,我还是准备采用MYSQL。当然其实单条用mongo也合适。不过因为迁移数据有点麻烦,所以还是先忍忍,一步一步来。

测试阶段:因为MYSQL to MONGO有点小麻烦,没有现成的工具。官方说有mongoimport,我看过了,确实OK,但是只支持一些基础的结构,比如:json,csv等。其他的则需要第三方工具。但我自己先在测试中,所以就直接写了个脚本,先将数据用AR取出来。然后插入到mongo中。结果发现,在排序的时候。。9999比10000还大。问了一下神仙 ,他说这应该是按字符串排序的方式来做的。

于是做了一个测试,写进了一个数字,果然就对了。。难道是yii的AR的BUG?想想这不太应该啊。于是我写了一个小的Demo:

PHP代码
  1. $db = mysql_connect("localhost","root","123456");  
  2. mysql_select_db("feed");  
  3. $query = mysql_query("select id from feeds_group limit 1");  
  4. while($rs = mysql_fetch_array($query)){  
  5.     $result = $rs;  
  6.     echo "<pre>";  
  7.     var_dump($result);  
  8.     echo "</pre>";  
  9. }  
  10. echo "<hr />";  
  11. $dsn = 'mysql:host=localhost;dbname=feed';  
  12. $user = 'root';  
  13. $password = '123456';  
  14. $dbh = new PDO($dsn$user$password);  
  15. $sth = $dbh->prepare("select id from feeds_group limit 1");  
  16. $sth->execute();  
  17. $result = $sth->fetchAll();  
  18. echo "<pre>";  
  19. var_dump($result);  
  20. echo "</pre>";  

打印出来一看:

XML/HTML代码
  1. array(2) {  
  2.   [0]=>  
  3.   string(1) "1"  
  4.   ["id"]=>  
  5.   string(1) "1"  
  6. }  
  7.   
  8. array(1) {  
  9.   [0]=>  
  10.   array(2) {  
  11.     ["id"]=>  
  12.     string(1) "1"  
  13.     [0]=>  
  14.     string(1) "1"  
  15.   }  
  16. }  

 

果然出来就是字符串了,最后只能自己在插到mongo的时候写了一个小函数,对于数值型字段做了转换,问题就这样解决了。

 

 

 

 

Tags: yii, mongodb, mysql, string

HtmlPurifier的一个小问题:处理flash

在没有TIdy库的情况下,修复HTML我们是用htmlPurifier类库,但这个类库有点小问题。。。比如,对flash支持不好,虽然官网上有例子,可以支持yotube,不过,国内的flash视频站很多,我们不可能一一写扩展啦。怎么办?

后来有人说:

PHP代码
  1. $config->set('HTML.SafeObject', true);  
  2. $config->set('Output.FlashCompat', true);  
  3. $config->set('HTML.Allowed''object[width|height|data],param[name|value],embed[src|type|allowscriptaccess|allowfullscreen|width|height]');  

但经过测试,发现这样也不行。。不信?你去官网的Demo.php中测试一下就知道了。

测试代码可以是:
http://htmlpurifier.org/demo.php?filter[HTML.SafeObject]=1&filter[Output.FlashCompat]=1&filter[HTML.Allowed]=object[width|height|data],param[name|value],embed[src|type|allowscriptaccess|allowfullscreen|width|height]&html=%3Cp%3E%3C%2Fp%3E%3Cp%3E%3Cembed+src%3D%22http%3A%2F%2Fplayer.youku.com%2Fplayer.php%2Fsid%2FXMzIwMjQ5NTk2%2Fv.swf%22+allowfullscreen%3D%22true%22+quality%3D%22high%22+width%3D%22480%22+height%3D%22400%22+align%3D%22middle%22+allowscriptaccess%3D%22always%22+type%3D%22application%2Fx-shockwave-flash%22%2F%3E%3C%2Fp%3E&submit=Submit

所以。如果stripos($data,"embed")!==false的话,还是不要用htmlpurifier过滤了。

参考:
1、http://htmlpurifier.org/docs/enduser-youtube.html(文中有为什么不支持embed的说明)

  1. <embed> is not recognized by W3C, so if you want standards-compliant code, you'll have to get rid of it.
  2. The code is exactly the same for all instances, except for the identifier AyPzM5WK8ys which tells us which movie file to retrieve.

2、http://stackoverflow.com/questions/1368809/allow-embed-object-param-html-tags-with-htmlpurifier(即使设置了allowed,还是不行)
3、http://htmlpurifier.org/phorum/read.php?5,2239,page=3 (在这里,也还是没有解决。寄希望于下一版了。。。)

Tags: htmlpurifier

PHPer下一步应该学go?

其实看到现在,很多人都在推荐go语言,理由有很多种,但对于PHPer来说,不一定就是适合的。go是google推出的语言,到现在为止也已经有很长时间了,再加上go又能底层,又能WEB,所以。。。
但是,究竟应该不应该学,还是会有点想法,所以看到这篇全英文的介绍的时候就蛋定不了了。
原文是来自于:Why you PHP guys should learn Golang

Go or Golang, is a open source, community supported, fast, consistent, scalable, productive language, and conceived by Google. More and more applications were build with it. Although Rob Pike has said that “… we expected C++ programmers to see Go as an alternative …”, but I really think of that: You PHP guys should learn Golang! Let’s talk about WHY.

 

Easy to learn

PHP is easy to learn. Golang is as easy as PHP!

The syntax of PHP is similar to C family’s programming languages(C/C++/Java etc.). If you have some experiences with those languages, it is not too hard to write some codes when you first met PHP.

Golang is also a C family’s programming language. Eh… with a litte syntax difference. For example, the keyword “for” is similar with PHP’s, but without the pair of parentheses. And the condition keyword “if” is without the pair of parentheses either. You can read Effective Go for more things.

Golang has only 30 keywords and 47 operators, delimiters or other special tokens. There is no need to work hard for remembering all the literals. The type system is exquisite and easy to use. No huge OO but a practical struct type with methods on it. The interface design is one of my favorate parts of Golang. When you have finished this A Tour of Go, based on your PHP experiences, some jobs could be done with Golang immediately.

Easy to Use

Your PHP scripts will be evaluated by SAPI components: web server module, PHP-fpm or CLI. All needed for PHP deploying is a SAPI environment. Deployment of the environment is the most difficult part to understand for a PHP beginner.

All the Golang’s codes will be compiled and linked into native code. So there is no need to deploy a environment for it executing but a build environment. Compaire with deployment of the PHP environment, it is not so much complex. Do you really think of the deployment of the PHP environment is really complex? I don’t think so. Really!

To deploy the build environment, just need 3 steps:

  • Download Golang‘s source code.
  • Setup the environment variables. Here’s a great blog post: Go environment setup
  • Run the ‘all.bash’ in the ‘src’ directory.

Or only one step: Use the binary package to setup.

Then a toolset called “go” can be used. Using the go tool is as easy as using PHP’s CLI tool. “The Go Tool” has already explained it.

A myth of PHP

If a programming language is easy to learn and use, shall we learn and use it? Lots of programming languages are easy to learning and use. Shall we learn and use all of them? The answer is straightforward: NO!

But why you PHP guys should learn Golang? Just because it is cool! Ja, I’m kidding, but it is true. Anyway, let’s talk about PHP itself first.

PHP is “a general-purpose server-side scripting language originally designed for Web development to produce dynamic Web pages.(Wikipedia)”. The most important feature of PHP is embedding with HTML. PHP code write in “<?php … ?>” tag; HTML write outside. It also has a powerful extension system. The extension can be writen by C with Zend API. Data processing was actually done by those extensions. IMO, PHP must be the best template programming language.

But if you have years experiences with PHP and began face to some complex web application. You must have already felt some powerlessness to PHP. It dosen’t have concurrent mechanism build-in, no threads, multiprocessing or X-routines. A slow data source will be blocked entire page’s processing. A powerful web server in the front of PHP is always needed. Backend programes were writen to process data. Message Queue, Cache, Proxy … The system is not only about PHP, but also about a lot of services and system components. This time, PHP is really be a template language with few logical jobs.

PHPers are always looking for solutions, like “PHP multithread” or a concurrent RPC framework for PHP. I could not say witch one is better. But I’m sure that you must choose an other programming language for the backend job. In my own case, I tried C(struggle with malloc/free)/Java(stumble in the jar hell)/Python(never be pythonic but type lost)..If I get profermance, I must struggle with the memory management; If I use GC, I must deploy and turning VM; If I get convenient, careless would kill me….Eche has advantages, each also has a problem.

OK! Let’s back to Golang now!

Golang has GC, there is no need to care about memory management(or could pay less attention to it). And the code are compiled into native machine code. So, “cp” and “mv” are all needed for deploying the application writen by Golang. Oh, as I just said, Golang is a compiled language with a static type system. You have no chance to confuse veriable types. Of course, one of the most important thing is PHPer who moves to Golang “don’t have to surrender much expressiveness, but gain performance and get to play with concurrency(by Rob Pike)”. “Why Not Go?” talked about this deeply.

I would show parts of my own experience: a Gearman worker works for us to process backend data. PHP connected to the worker though Gearman Job Server with its Gearman API. The original worker was writen by python. There are lots of problems with that version(They are our own problems, but python’s). But it works for us. Then we rewrite the worker with Golang. It was amazing. I wrote a Gearman API for Golang, and a Golang package using Zend API(cgo is another amazing thing) for evaluating the PHP script in Golang. Then combined them together – A Gearman worker witch can execute PHP scripts. It works about 3 mounth in our production enverionment and looks great! Oh, I also wrote a RPC-combinator for combining the RPC call in PHP script. It will be used in our next product.

Everything goes right. Thanks Golang!

In any case, most of PHPers alwasy need to learn other languages for the backend development. If you have began looking for, or already tasted some of other languages. Why don’t you try Golang? It would make your life happy and easy. You could spend more time with your family and friends, eat your favorite foods and visit the places you love.

That’s why you PHP guys should learn Golang! Is it enough? I think so!

This is my writing exercise. If you find any grammatical or spelling error, plz tell me!

---EOF---

其实我也挺纠结的,要知道当时我刚跟着tour做了30多篇的时候,突然golang的网页就再也打不开了。再苦了,墙就是我们学习的最大障碍。想想这些年我们遇到了什么。。
sf.net被墙
groups.google.com被墙
python.org被墙
很多。。。。。这些都是我们的障碍啊