PHP页面静态化学习笔记之四:简易新闻系统v1.0
这是本人根据自己学习PHP技术页面静态化的过程所写的学习笔记,希望能够对大家有所帮助。 1、基本思路: (1)用户第一次访问这个页面时从数据库读出内容,放在缓存中; (2)将缓存中的数据写成一个html静态页面的文件; (3)用户以后访问都去读取生成html
这是本人根据自己学习PHP技术页面静态化的过程所写的学习笔记,希望能够对大家有所帮助。
1、基本思路:
(1)用户第一次访问这个页面时从数据库读出内容,放在缓存中;
(2)将缓存中的数据写成一个html静态页面的文件;
(3)用户以后访问都去读取生成html文件,然后输出在屏幕上;
(4)每隔一定的时间重新生成一次html静态页面。
2、创建数据库
create table news( id int unsigned primary key auto_increment, title varchar(128) not null, content varchar(256) not null, filename varchar(32)) engine=MyISAM
3、测试数据
INSERT INTO `static_pages_news`.`news` (`id`, `title`, `content`, `filename`) VALUES (NULL, 'hello1', '北京你好', NULL), (NULL, 'hello2', '四川你好', NULL);
4、代码
news_list.php(新闻列表页面)<?php //新闻列表 //查询数据库,获取信息=>SqlHelper.class.php $conn = mysql_connect("localhost", "root", "root"); if (!$conn) { die("连接失败"); } mysql_select_db("static_pages_news", $conn); mysql_query("set names utf8"); $sql = "select * from news"; $res = mysql_query($sql); header("content-type:text/html;charset=utf-8"); echo "<h1 id="新闻列表">新闻列表</h1>"; echo "<a href="#">添加新闻</a><hr>"; echo "
id | 标题 | 查看详情 |
{$row['id']} | {$row['title']} | 查看详情 |
show_news.php(新闻详情页面)
<?php $id = @$_GET['id']; //构建文件名 $html_filename = "new_id" . $id . ".html"; $html_path = dirname(__FILE__) . "/" . $html_filename; //判断html页面是否有并且页面最后修改时间到现在小于30s if (file_exists($html_path) && filemtime($html_path) + 30 < time()) { //直接访问html页面(把html页面的内容echo浏览器) echo file_get_contents($html_path); exit ; } $conn = mysql_connect("localhost", "root", "root"); if (!$conn) { die("连接失败"); } mysql_select_db("static_pages_news", $conn); mysql_query("set names utf8"); $sql = "select * from news where id=$id"; $res = mysql_query($sql); //开启缓存 ob_start(); if ($row = mysql_fetch_assoc($res)) { header("content-type:text/html;charset=utf-8"); echo "<table border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>"; echo "<tr><td>新闻详细内容</td></tr>"; echo "<tr><td>{$row['title']}</td></tr>"; echo "<tr><td>{$row['content']}</td></tr>"; echo ""; } else { echo "没有结果"; } $html = ob_get_contents(); $my_header = "<meta http-equiv="Content-Type" content="text/html; charset=utf-8">"; //把ob写进html文档 file_put_contents($html_path, $my_header . $html); mysql_free_result($res); mysql_close($conn); ?>
5、不足之处
(1)在查看新闻详情的时候,仍然是一个PHP页面
(2)实时性不够好,有30s的延时

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

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

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











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,

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.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
