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.
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 = 'set names utf8'; mysqli_query($conn,$setting); //第三步进行查询 $sql = 'SELECT * FROM user'; $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='xdebug-var-dump' dir='ltr'> <b>array</b> <i>(size=6)</i> 0 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'0'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'辛星'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'bd04fcc97578ce33ca5fb331f42bc375'</font> <i>(length=32)</i> 1 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'小倩'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'61cb72858be523b9926ecc3d7da5d0c6'</font> <i>(length=32)</i> 2 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'3'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'小楠'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'a3d2de7675556553a5f08e4c88d2c228'</font> <i>(length=32)</i> 3 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'4'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'刘强'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'fcdb06a72af0516502e5fdccc9181ee0'</font> <i>(length=32)</i> 4 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'5'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'星哥'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'866a6cafcf74ab3c2612a85626f1c706'</font> <i>(length=32)</i> 5 <font color='#888a85'>=></font> <b>array</b> <i>(size=4)</i> 'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'6'</font> <i>(length=1)</i> 'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i> 'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'辛勇'</font> <i>(length=6)</i> 'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'e93beb7663f3320eaa0157730d02dd0c'</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!