Static PHP page: two static and real solutions, php static _ PHP Tutorial

Source: Internet
Author: User
Static PHP pages: static php is a real static solution. PHP static page: real static two solutions, php static rewrite Solution 1: If static files are stored in PHP pages, static php pages: static PHP pages

Bytes ----------------------------------------------------------------------------------------------

Solution 1: If a static file exists and is generated within 30 seconds, the static page is directly returned (with time delay)
/*
| ------------------
|
| ------------------
*/
Header ('content-type: text/html; charset = utf-8 '); $ id = $ _ GET ['id']? Intval ($ _ GET ['id']): ''; if ($ id ='') die ('Enter the id of the news you want to query! '); $ Html_file = "news-id -". $ id. ". html "; // 1. main code: if (file_exists ($ html_file) & filemtime ($ html_file) + 30> = time () {echo 'static page: '; echo file_get_contents ($ html_file ); exit;} // You can also use the DB Tool class $ con = mysql_connect ('localhost', 'root', '123'); if (! $ Con) {die ('connection failed! ');} Mysql_select_db ('testdb', $ con); $ SQL = "select * from bo_question where question_id = $ id"; $ res = mysql_query ($ SQL, $ con); if ($ row = mysql_fetch_assoc ($ res) {ob_start (); // 2. start ob cache header ('content-type: text/html; charset = utf-8 '); echo' '; Echo' ; Echo' '; Echo" "; Echo" "; Echo'
Problem Details
Title: {$ row ['question _ title']}
Details: {$ row ['question _ detail ']}
'; $ Ob_str = ob_get_contents (); // 3. save the ob_str file to a static file page. the file name must be: 1. uniquely identifies the News 2. seo file_put_contents ("news-id -". $ id. ". html ", $ ob_str );
  
// Close the database connection (not required; if the connection is not persistent, the database will be closed automatically after the script is executed)
Mysql_close ($ con);} no resource found in else {echo! ';}

@ Blackeye poet

 
Solution 2: Use the template replacement technology (no time delay)
/*
| ------------------
|
| ------------------
*/
$ Tags = $ _ POST ['upload']; // add operation if ($ tags = 'ADD') {$ title = $ _ POST ['title']; $ content = $ _ POST ['content']; // If you strictly follow MVC, the model should be called here $ con = mysql_connect ('localhost', 'root ', '20140901'); if (! $ Con) {die ('connection failed! ');} Mysql_select_db ('news', $ con); $ SQL = "insert into question (null,' $ title', '$ content ','')"; if (mysql_query ($ SQL, $ con) {// 1. generate a static file $ id = mysql_insert_id (); $ html_filename = 'news-id'.$id.'html '; $ html_fp = fopen ($ html_filename, 'w'); // 2.read the template file (news.html) $ fp = fopen ('news. tpl ', 'r'); // r read-only mode; r + read/write mode; w write mode: file content will be cleared! If the file does not exist, it will be created; a will be opened as an append // 3. read the file cyclically // if the last part of the file is not read, it will always read while (! Feof ($ fp) {// read a row $ row = fgets ($ fp ); // replace the placeholder => you can customize the complete replacement rule function $ row = str_replace ('% title %', $ title, $ row ); // if $ row is not re-assigned, the $ row value will not change $ row = str_replace ('% content %', $ content, $ row); fwrite ($ html_fp, $ row); // 4. write content to a static file}
// 5. the file must be fclose ($ html_fp); fclose ($ fp); echo "added successfully. Click to view news! ";} Else {die ('failed to add! ') ;}} // Click the link to view details in the news list. you can change it to the generated static page address to directly access the static file. // News. tpl template file /* % Title % % Title %
%content%
*/

Bytes ---------------------------------------------------------------------------------------------


Php static page method, which can also be pseudo-static

Here is the static member list in jetee.cn. According to this code, you can understand some static concepts.

Static functions
/**
* @ Get list member item get the content of the variable to be replaced in the static template.
* @ Param str: To replace the string
* @ Return string
*/
Function get_staticize_replace_str ()
{
$ Replace_str = ""; // replace string
$ Query = "select member_id, email_name from member ";
$ Result = Mysql: query ($ query );
While ($ row = Mysql: fetch_assoc ($ result ))
{
$ Replace_str. ="

  • ". $ Row [" member_id "]." </li> ";
    $ Replace_str. ="
  • ". $ Row [" email_name "]." </li>
    ";
    }
    Return $ replace_str;
    }

    /**
    * @ Replace the variables in the static template to generate a static page.
    *@
    * @ Return void
    */
    Function staticize_list_member ()
    {
    $ Replace_str = $ this-> get_staticize_replace_str ();
    $ Templet = fopen (TENDAO_DIR. "/templets/default/list_member.html", "r ");
    $ New_file = fopen (TENDAO_DIR. "/member/list_member.html", "w ");
    While (! Feof ($ templet ))
    {
    $ Ripe = fgets ($ templet );
    $ Ripe = str_replace ("{member_items}", $ replace_str, $ ripe );
    Fwrite ($ new_file, $ ripe );
    }

    If (file_exists (TENDAO_DIR. "/member/list_member.html ")){
    Msg ("The Static Member List is successful! Back to homepage ...... ", TENDAO_ROOT, 0,3000 );
    Exit ();
    } Else {
    Msg ("the static member list is not successful! Back to homepage ...... ", TENDAO_ROOT, 0,3000 );
    }

    Fclose ($ templet );
    Fclose ($ new_file );
    }

    /**
    * @ Static template
    *@
    ... The remaining full text>

    Php issues about page static

    Many methods
    Use include_once in php.
    Only when files are included, note that the files you detach are</Body>
    Then look at the page style
    Good programmers will separate many parts
    This facilitates unified management.

    Zookeeper Solution 1: If the static file is stored...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.