一个做页面静态化的php类
<?php namespace Common; /* * * 功能:页面静态化的创建和删除 * 创建:当且仅当,一个页面需要被静态化并且还未静态化时。 * 删除:当且仅当,一个页面存在静态化页面并且需要被重新静态化时。 * * 作者:郭军周 * * 注 :本类基于ThinkPHP3.2,或者其他具有“单一入口且MVC模式”的其他php框架。 * * 使用方式:在Controller的构造方法中获取其对象;在Controller的销毁方法里,用其对象的_static方法。 * 例:XXXController extends BaseController. * BaseController: * function __construct(){ * $this->__sh = StaticHtml::getInstance(); * } * function __destruct(){ * $this->__sh->_static(); * } * * */ class StaticHtml{ private static $_instance = null; /* 单例模式,自身的引用 */ private $_needStatic = false; /* 是否需要将其静态化 */ private $_needDeleteStatic = false; /* 是否需要删除其静态化的页面 */ private $_hasStaticed = true; /* 是否存在其的静态化页面 */ private $_controller = null; /* 当前被访问的controller */ private $_action = null; /* 当前被访问的action */ // private $_staticAgain = false; /* 删除静态文件后,是否马上重新更新【【注意】再次请求】 */ private $_save_path = null; /* 将要创建或者删除的静态文件的路径 */ private $_conf = array( /* 此文件定义一些静态文件的存放方式 */ 'files_per_directory' => 100 /* 此值不允许被修改,除非是要删除所有已经存在的静态文件,重新缓存 */ ); private $_staticList = array( /* 此数组,定义了需要创建静态化页面的Controller的action */ // 'Base' => array( /* Base为controller name */ // 'aaa' => array( /* aaa为action name */ // 'save_path' => '/StaticHtml/Base/aaa/', /* save_path为生成的静态文件存放的根路径 */ // 'static_base' => 'id', /* static_base为生成静态文件的“依据”。建议为对应数据库的primary_key */ // 'alias' => 'aaa' /* 静态文件的名字,否则为1.html */ // ) // ) 'Mynotes' => array( 'look_notes' => array( 'save_path' => '/StaticHtml/Mynotes/look_notes/', 'static_base' => 'id', 'alias' => 'note' ), 'add_personal_notes' => array( 'save_path' => '/StaticHtml/Mynotes/', 'alias' => 'note-add' ) ), 'Resource' => array( 'allResource' => array( 'save_path' => '/StaticHtml/Resource/', 'alias' => 'allResource' ), 'resource_add' => array( 'save_path' => '/StaticHtml/Resource/', 'alias' => 'resourceAdd' ) ), 'Thing' => array( 'suggestion_of_lecture' => array( 'save_path' => '/StaticHtml/Lecture/', 'alias' => 'voteLecture' ) ), 'Passwordfix' => array( 'index' => array( 'save_path' => '/StaticHtml/Information/', 'alias' => 'updatePassword' ) ), 'Information' => array( 'index' => array( 'save_path' => '/StaticHtml/Information/', 'static_base' => 'user_id', 'alias' => 'information' ) ), 'Courseinfo' => array( 'course_show' => array( 'save_path' => '/StaticHtml/Information/', 'static_base' => 'user_id', 'alias' => 'course' ) ) ); private $_deleteList = array( /* 此数组,定义了需要删除某些静态化页面的Controller的action */ // 'Base' => array( /* Base为controller name */ // 'bbb' => array( /* bbb为action name */ // 'save_path' => '/StaticHtml/Base/aaa/', /* save_path为要删除的静态文件存放的根路径 */ // 'static_base' => 'id', /* static_base为确定静态文件路径的“依据”。建议为对应数据库的primary_key */ // 'alias' => 'aaa' /* 静态文件的名字,否则为1.html */ // ) // ) 'Mynotes' => array( 'edits_notes' => array( 'save_path' => '/StaticHtml/Mynotes/look_notes/', 'static_base' => 'id', 'alias' => 'note' ) ), 'Information' => array( 'save_user_info' => array( 'save_path' => '/StaticHtml/Information/', 'static_base' => 'user_id', 'alias' => 'information' ) ), 'Courseinfo' => array( 'course_update' => array( 'save_path' => '/StaticHtml/Information/', 'static_base' => 'user_id', 'alias' => 'course' ) ) ); private function __construct(){ $this->needStatic(); /* 确定本次请求是否需要静态化 */ $this->hasStaticed(); /* 确定本次请求是否已经存在静态化页面 */ $this->needDeleteStatic(); /* 确定本次请求是否需要删除某些静态页面 */ } /* 确定需要删除的静态文件的存放路径 */ private function needDeleteStatic(){ if($this->_needDeleteStatic){ $save_path = $this->getSavePath($this->_deleteList[$this->_controller][$this->_action]); $this->_hasStaticed = false; if(file_exists(ROOT_PATH.$save_path)){ $this->_hasStaticed = true; } // $this->_staticAgain = $this->_deleteList[$this->_controller][$this->_action]['visitAgain']; $this->_save_path = ROOT_PATH.$save_path; } } /* 获取本类的,唯一的,实例化 */ public static function getInstance(){ if(!(self::$_instance instanceof self)){ self::$_instance = new self(); } return self::$_instance; } /* 判断是否存在其静态化的文件 */ private function hasStaticed(){ if($this->_needStatic){ $save_path = $this->getSavePath($this->_staticList[$this->_controller][$this->_action]); if(!file_exists(ROOT_PATH.$save_path)){ $this->_hasStaticed = false; ob_start(); }else{ header("location: http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']).$save_path); } $this->_save_path = ROOT_PATH.$save_path; } } /* 获取本次请求要生成或者删除的,静态化文件的路径 */ private function getSavePath($conf){ if(!isset($conf['static_base'])){ $save_path = $conf['save_path']; $save_path .= $conf['alias'].'.html'; }else{ if($conf['static_base'] == 'user_id'){ $id = (int)$_SESSION['logined_user']['id']; }else{ if(IS_GET){ $id = $_GET[$conf['static_base']]; }else{ $id = $_POST[$conf['static_base']]; } } $save_path = $conf['save_path']; $directory_id = ceil($id/$this->_conf['files_per_directory']); $save_path .= $directory_id.'/'; if($conf['alias']){ $fileName = $conf['alias'].'-'; } $fileName .= $id.'.html'; $save_path .= $fileName; } return $save_path; } /* 确定本次请求,是否需要生成静态化文件 */ private function needStatic(){ $url = explode('/',__ACTION__); $this->_controller = $url[4]; $this->_action = $url[5]; if(isset($this->_staticList[$this->_controller]) && isset($this->_staticList[$this->_controller][$this->_action])){ $this->_needStatic = true; } if(isset($this->_deleteList[$this->_controller]) && isset($this->_deleteList[$this->_controller][$this->_action])){ $this->_needDeleteStatic = true; } } /* 生成,或者删除,静态化文件 */ public function _static(){ if($this->_needStatic && !$this->_hasStaticed){ $html = ob_get_contents(); $this->_mkdir(dirname($this->_save_path)); file_put_contents($this->_save_path,$html); } if($this->_needDeleteStatic && $this->_hasStaticed){ unlink($this->_save_path); /*if($this->_staticAgain){ header("location: http://www.baidu.com"); // header("location: http://".$_SERVER['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']); }*/ } } /* 创建目录 */ private function _mkdir($path){ if (!file_exists($path)){ $this->_mkdir(dirname($path)); mkdir($path, 0777); } } } ?>

PHP在电子商务、内容管理系统和API开发中广泛应用。1)电子商务:用于购物车功能和支付处理。2)内容管理系统:用于动态内容生成和用户管理。3)API开发:用于RESTfulAPI开发和API安全性。通过性能优化和最佳实践,PHP应用的效率和可维护性得以提升。

PHP可以轻松创建互动网页内容。1)通过嵌入HTML动态生成内容,根据用户输入或数据库数据实时展示。2)处理表单提交并生成动态输出,确保使用htmlspecialchars防XSS。3)结合MySQL创建用户注册系统,使用password_hash和预处理语句增强安全性。掌握这些技巧将提升Web开发效率。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。

PHP仍然具有活力,其在现代编程领域中依然占据重要地位。1)PHP的简单易学和强大社区支持使其在Web开发中广泛应用;2)其灵活性和稳定性使其在处理Web表单、数据库操作和文件处理等方面表现出色;3)PHP不断进化和优化,适用于初学者和经验丰富的开发者。

PHP在现代Web开发中仍然重要,尤其在内容管理和电子商务平台。1)PHP拥有丰富的生态系统和强大框架支持,如Laravel和Symfony。2)性能优化可通过OPcache和Nginx实现。3)PHP8.0引入JIT编译器,提升性能。4)云原生应用通过Docker和Kubernetes部署,提高灵活性和可扩展性。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

记事本++7.3.1
好用且免费的代码编辑器