php页面静态化笔记
<span style="color: #000000;">php </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * php页面静态化 * 静态化分为 真静态和伪静态 * 静态化的有点在于 SEO 和 安全性 使用缓存机制还可以大大提高网站的速度 </span><span style="color: #008000;">*/</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* * php自带的缓存机制 * 需要在php.ini中进行配置 * 或者使用ob_start()-> output_buffering:on * 关于几个缓存的函数:ob_start();ob_clean();ob_get_content();ob_end_clean();关闭缓存同时清空 * ob_flush();输出缓存里面的内容并且清空,但是不关闭 ob_end_flush();关闭同时输出 </span><span style="color: #008000;">*/</span> <span style="color: #008000;">/**/</span> <span style="color: #008080;">ob_start</span>();<span style="color: #008000;">//</span><span style="color: #008000;">开启缓存</span> <span style="color: #0000ff;">echo</span> 'yyy';<span style="color: #008000;">//</span><span style="color: #008000;">没有开启缓存的情况下会报错,因为没有遇到header的时候,php默认发送一个header,这个header无法修改</span> <span style="color: #008080;">header</span>('content-type:text/htm;charset=utf-8'<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> '123'<span style="color: #000000;">; </span><span style="color: #008080;">ob_get_contents</span>();<span style="color: #008000;">//</span><span style="color: #008000;">得到缓存的内容</span> <span style="color: #008080;">ob_end_clean</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">echo</span> 'aa';<span style="color: #008000;">//</span><span style="color: #008000;">放在程序缓存里面</span> <span style="color: #008080;">header</span>('content-type:text/htm;charset=utf-8');<span style="color: #008000;">//</span><span style="color: #008000;">运行的时候会报错,因为缓存已经关闭</span> <span style="color: #008000;">/*</span><span style="color: #008000;"> * 利用php自带的缓存机制做真缓存 * 真缓存的页面的内容应该是长期都不需要变的,并且不同用户看到的东西都是一样的 * 原理:用户第一次访问的时候查询数据库,然后得到缓存区的内容,并且放到缓存文件里面 * </span><span style="color: #008000;">*/</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* </span><span style="color: #008000;">*/</span> <span style="color: #008080;">ob_start</span><span style="color: #000000;">(); </span><span style="color: #008000;">//</span><span style="color: #008000;">判断缓存文件是否存在</span> <span style="color: #800080;">$html_filename</span>='static.html'<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$html_filename</span>) && (<span style="color: #008080;">filemtime</span>(<span style="color: #800080;">$html_filename</span>)+30) > <span style="color: #008080;">time</span>())<span style="color: #008000;">//</span><span style="color: #008000;">超过30秒则重新生成</span> <span style="color: #000000;">{ </span><span style="color: #008000;">//</span><span style="color: #008000;">直接取出缓存页面的内容</span> <span style="color: #0000ff;">echo</span> <span style="color: #008080;">file_get_contents</span>(<span style="color: #800080;">$html_filename</span><span style="color: #000000;">); </span><span style="color: #0000ff;">exit</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> 'Your content'<span style="color: #000000;">; </span><span style="color: #800080;">$html_content</span>=<span style="color: #008080;">ob_get_contents</span><span style="color: #000000;">(); </span><span style="color: #800080;">$html_filename</span>='static.html'<span style="color: #000000;">; </span><span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$html_filename</span>,<span style="color: #800080;">$html_content</span><span style="color: #000000;">); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> * * 上述原理的缺陷:1.实时性不够 2.不利于seo优化 * 解决办法:在数据库发生增加和更新的时候更新缓存文件 * </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$_REQUEST</span>['act'] == 'add')<span style="color: #008000;">//</span><span style="color: #008000;">update 也是</span> <span style="color: #000000;">{ </span><span style="color: #008000;">//</span><span style="color: #008000;">先获取模版文件</span> <span style="color: #800080;">$template_content</span>=<span style="color: #008080;">file_get_contents</span>('template.tpl'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">替换标签</span> <span style="color: #800080;">$php_content</span>=<span style="color: #008080;">str_replace</span>("{",'<?php echo ',<span style="color: #800080;">$template_content<span style="color: #000000;">); </span><span style="color: #800080;">$php_content</span>=<span style="color: #008080;">str_replace</span>("}",'?>',<span style="color: #800080;">$template_content</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">生成缓存文件</span> <span style="color: #800080;">$php_tplname</span>='php_tpl.php'<span style="color: #000000;">; </span><span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$php_tplname</span>,<span style="color: #800080;">$php_content</span><span style="color: #000000;">); </span><span style="color: #0000ff;">include</span>(<span style="color: #008080;">file_put_contents</span><span style="color: #000000;">); </span><span style="color: #800080;">$html_content</span>=<span style="color: #008080;">ob_get_contents</span><span style="color: #000000;">(); </span><span style="color: #800080;">$html_filename</span>='static.html'<span style="color: #000000;">; </span><span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$html_filename</span>,<span style="color: #800080;">$html_content</span><span style="color: #000000;">); } </span><span style="color: #008000;">/*</span><span style="color: #008000;">* * 真静态的缺陷:生成的 缓存文件很多,占硬盘 * 对于海量的html生成,可以考虑用伪静态 </span><span style="color: #008000;">*/</span> <span style="color: #800080;">$str</span>='123,33,333'<span style="color: #000000;">; </span><span style="color: #008080;">preg_match</span>('/\d\d\d\d/is',<span style="color: #800080;">$str</span>,<span style="color: #800080;">$arr</span>);<span style="color: #008000;">//</span><span style="color: #008000;">匹配连续四个出现的数字 返回到arr 只是匹配一个 arr[0]</span> <span style="color: #008080;">preg_match</span>('/(\d)(\d)/is',<span style="color: #800080;">$str</span>,<span style="color: #800080;">$arr</span>);<span style="color: #008000;">//</span><span style="color: #008000;">这时候arr返回三个 0放置匹配的字符串,arr[n]按照表达式顺序返回匹配的字符串 //对于需要全部匹配的使用preg_match_all()函数;正则表达式里面需要'' 不要使用""</span> <span style="color: #008000;">/*</span><span style="color: #008000;">* * 使用apache的rewrite机制 * 注意点:apache的配置 开启rewrite module 也要设置derectory的权限 allowoverride from all * 文件 .htacss //匹配规则是从上而下的,可以设置error页面 * RewireEngine On * news-id(\d)\.html$ news.php?id=$1 * news-id(\d).html$ error.php #错误页面放到最后 </span><span style="color: #008000;">*/</span>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
