zoukankan      html  css  js  c++  java
  • php的304方式

    一般浏览器请求php是不会被缓存的,除非php直接显示的发送head 304

    if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
    			$browserCachedCopyTimestamp = strtotime(preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']));
    			if($browserCachedCopyTimestamp + 604800 > time()){
    				header("http/1.1 304 Not Modified");
    				header ("Expires: " . gmdate ("r", (time() + 604800)));
    				header ("Cache-Control: max-age=604800");
    				exit;
    			}
    		}
    		$id=I('get.id',0,'intval');
    		$file = UPLOAD_IMG_PATH . 'qccode/' . $id . '.png';
    		if(file_exists($file)){
    			header ("Last-Modified: " . gmdate ('r', time()));
    			header ("Expires: " . gmdate ("r", (time() + 604800)));
    			header ("Cache-Control: max-age=604800");
    			header ("Pragma: public");
    			Header ("Content-type: image/png");
    			echo file_get_contents($file);
    		}else{
    			$qr = APPlibget_app_lib('phpqrcode',false);
    			$site = C('SITE_URL');
    			$value=$site . $id . '.html';
    			$errorCorrectionLevel = "L";
    			$matrixPointSize = "4.5";
    			QRcode::png($value, $file, $errorCorrectionLevel, $matrixPointSize);
    			echo file_get_contents($file);
    		}
    

    上面就是通过304来缓存一个动态的js,根据文件的修改时间来判断,这里需要注意一点,如果再次之前有session_start,要注意session_cache_limiter,他会发送一个不缓存的头信息。

    这种情况需要可以强制发送一个program public的头来规避,注意在发送304的时候最好还是补充一下缓存头,我在缓存js时可以不在重设,可能是由于我是通过服务器重定向模拟的动态js吧(请求路径是js,但是是动态生成的)。

  • 相关阅读:
    go语言的grpc安装记录
    MySQL1:客户端/服务器架构
    设计模式
    乐观锁与悲观锁的选择
    compareAndSwapObject 产生ABD原因
    wangEditor
    ckeditor4学习
    git公司远程提交
    java面试题总结
    基本数据类型和包装类
  • 原文地址:https://www.cnblogs.com/kudosharry/p/4121394.html
Copyright © 2011-2022 走看看