The principle of using PHP to generate static HTML documents,
The principle of using PHP to generate static HTML documents,
Using PHP to generate a single HTML document from the article data in the database. First of all, it is beneficial to search engine inclusion. Secondly, it is safer to avoid exposing fields in the database to the address bar.
Give the code:
<?<span>php </span><span>//</span><span>引入数据库配置文件</span> <span>include</span>( <span>dirname</span>(<span>dirname</span>(<span>__FILE__</span>))."\include\config.php"<span> ); </span><span>/*</span><span>* * * 将数据库中的文章生成单个HTML文件. * @param Date $Date * @param Time $Time * @param String $Content * @param String $Title </span><span>*/</span> <span>function</span> GenerateHTML(<span>$Date</span>,<span>$Time</span>,<span>$Content</span>,<span>$Title</span>,<span>$Name</span><span>){ </span><span>//</span><span>将日期、时间变量分解成数组</span> <span>$GetDateRow</span> = <span>explode</span>("-", <span>$Date</span><span>); </span><span>$GetTimeRow</span> = <span>explode</span>(":",<span>$Time</span><span>); </span><span>//</span><span>得到文件的名字。比如:20121028210632.html</span> <span>$FileName</span> = <span>$GetDateRow</span>[0].<span>$GetDateRow</span>[1].<span>$GetDateRow</span>[2].<span>$GetTimeRow</span>[0].<span>$GetTimeRow</span>[1].<span>$GetTimeRow</span>[2].".html"<span>; </span><span>//</span><span>打开并读取模板内容</span> <span>$FP</span> = <span>fopen</span>("tmp.html","r"<span>); </span><span>$Str</span> = <span>fread</span>(<span>$FP</span>,<span>filesize</span>("tmp.html"<span>)); </span><span>//</span><span>得到替换后的模板内容</span> <span>$Str</span> = <span>str_replace</span>("{Title}",<span>$Title</span>, <span>$Str</span><span>); </span><span>$Str</span> = <span>str_replace</span>("{Content}", <span>$Content</span>, <span>$Str</span><span>); </span><span>$Str</span> = <span>str_replace</span>("{Name}", <span>$Name</span>, <span>$Str</span><span>); </span><span>$Str</span> = <span>str_replace</span>("{Date}", <span>$Date</span>,<span>$Str</span><span>); </span><span>$Str</span> = <span>str_replace</span>("{Time}", <span>$Time</span>, <span>$Str</span><span>); </span><span>//</span><span>关闭文件,减少服务器的压力。</span> <span>fclose</span>(<span>$FP</span><span>); </span><span>//</span><span>将内容写入HTML文件</span> <span>$Handle</span> = <span>fopen</span>(<span>$FileName</span>,"w"<span>); </span><span>fwrite</span>(<span>$Handle</span>,<span>$Str</span><span>); </span><span>fclose</span>(<span>$Handle</span><span>); </span><span>//</span><span>小测一下 //echo "ok,done!";</span> <span> } </span><span>//</span><span>数据库的操作</span> <span>$querysql</span> = "select * from article"<span>; </span><span>$queryset</span> = <span>mysql_query</span>(<span>$querysql</span><span>); </span><span>//</span><span>循环生成HTML文件。</span> <span>while</span>( <span>$row</span> = <span>mysql_fetch_array</span>(<span>$queryset</span><span>) ){ GenerateHTML(</span><span>$row</span>['date'],<span>$row</span>['time'],<span>$row</span>['content'],<span>$row</span>['title'],<span>$row</span>['name'<span>]); }</span>
OK, DONE!

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,

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

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.

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.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.
