search
HomeBackend DevelopmentPHP ProblemWhat is the static method in php

What is the static method in php

Oct 30, 2020 pm 04:07 PM
phpStatic

How to achieve staticization in php: 1. Rewrite the access address and change it through the PATHINFO mode of the URL; 2. The site can be staticized through a certain program before the user accesses the site.

What is the static method in php

How to achieve static php:

During the development process of PHP site, because search engines search for PHP pages There is a certain difference between the inclusion of deer and HTML pages. For the promotion of the site or the needs of SEO, the site must be made static to a certain extent. Staticization does not mean that there are no animation and other elements in the page, but it means that the html code of the webpage is in the page. There is no need to execute server-side languages ​​​​such as PHP scripts. We can directly access the webpage. This is a static web page.

One way is to rewrite the access address, which can be changed through the PATHINFO mode of the URL. Make it look more like a static page. Therefore, there is a greater chance of being crawled and included by search engines, but it is more friendly to search engines and pseudo-static.

The second is that the site can be made static through a certain program before the user accesses the site. Generate static pages. When the user visits this page. Because you are accessing a static page, the access speed will be many times faster than accessing a dynamic page. The front-end performance is that the page loading speed becomes faster, and the back-end performance is that the database connection is reduced. It reduces the pressure on the database. The only disadvantage is that it takes up more hard disks and the hard disks are relatively cheaper.

Pure static is the way to generate HTML files. We need to enable PHP's own caching mechanism, that is, ob_start to enable caching. And there cannot be any output before ob_start, otherwise the operation will fail. Then we use the ob_get_contents function to get the content in the cache, which will return a string. The third function is ob_end_clean, which is used to clear the contents of the cache and close it. It returns True on success and False on failure.

<?php
//开启缓存
ob_start();
//第一步连接数据库
$conn = mysqli_connect("localhost","root","","bbs");
//第二步设置对应的字符编码
$setting = &#39;set names utf8&#39;;
mysqli_query($conn,$setting);
//第三步进行查询
$sql = &#39;SELECT * FROM user&#39;;
$result = mysqli_query($conn,$sql);
//第四步把查询结果转化为一个数组
$rows = mysqli_num_rows($result);
$sqldata = array();
for($i = 0;$i <$rows;$i ++){
    $sqldata[] = mysqli_fetch_assoc($result);
}
//然后打印该信息
var_dump($sqldata);
//得到生成的html文件,下次訪问就无需訪问数据库了
$msg = ob_get_contents();
ob_end_clean();
//把输出内容放入一个html文件里
$f = fopen("static.html","w");
fwrite($f,$msg);
echo "静态化成功";

Generate an html file in the directory

<pre class=&#39;xdebug-var-dump&#39; dir=&#39;ltr&#39;>
<b>array</b> <i>(size=6)</i>
  0 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;0&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;辛星&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;bd04fcc97578ce33ca5fb331f42bc375&#39;</font> <i>(length=32)</i>
  1 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;2&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;小倩&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;61cb72858be523b9926ecc3d7da5d0c6&#39;</font> <i>(length=32)</i>
  2 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;3&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;小楠&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;a3d2de7675556553a5f08e4c88d2c228&#39;</font> <i>(length=32)</i>
  3 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;4&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;刘强&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;fcdb06a72af0516502e5fdccc9181ee0&#39;</font> <i>(length=32)</i>
  4 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;5&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;星哥&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;866a6cafcf74ab3c2612a85626f1c706&#39;</font> <i>(length=32)</i>
  5 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;6&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;辛勇&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;e93beb7663f3320eaa0157730d02dd0c&#39;</font> <i>(length=32)</i>

Related learning recommendations: php programming (video)

The above is the detailed content of What is the static method in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version