php realize the static knowledge of the page

1. The concept of buffer:

   The buffer is actually a buffer, a memory address space, the main user storage data area.

   A process of outputting statements in php is: content --php buffer --- tcp --- terminal

  Open the buffer in php: enter php.ini file and open "output_buffering = on"

  Example: (Get the data in the buffer)

 <?php

 echo 'Get buffer data;

 // Get the data of the buffer through the following function

 echo ob_get_contents();

 ?>

2. How does PHP achieve page static method :

   The first kind: through the file_get_contents () function

   file_get_contents (): Write a string to a file.

   file_get_contents ('index.shtml', 'String data');

  The second kind: use PHP built-in caching mechanism to achieve page static: output_buffering

    Several commonly used functions:

    ob_start: open the output control buffer (when output_buffering in the php.ini file is closed, you can directly use this function to start)

    ob_get_contents: return the contents of the output buffer

    ob_clean: clear the contents of the output buffer

    ob_get_clean: get the contents of the current buffer and delete the current output buffer


3. Steps to achieve a purely static case:

    One of the main practical steps is to obtain data from the database-and then fill the obtained data into the template (php dynamic page)-and then convert the dynamic page into a static page to generate a pure static file.

   case study:

  <?php

  dataarray: data found in the database (in the form of an array)

 ob_start (); // Start buffer

 // Fill the data obtained from the database into the template file-and then introduce the template file into the current page to execute

 require_once('/index.php');

 if(file_put_contents('index.shtml', ob_get_clean()))

 {

     echo 'success';

 }else

  echo 'error';

 ?>

 The above program will generate a purely static page: index.shtml


4. How to trigger the system to generate a purely static page:

Three ways:

 First: add cache time to the page

  if (is_file ("./ index.shtml") && (time ()-filemtime ("./ index.shtml")) <500) {require_once ("./ index.shtml") // less than 5 minutes Load the generated static file} else {// Ask a dynamic template file to regenerate a static file}

Second: manual trigger mode

Third: crontab regular scan program (timed update program in linux system)

  crontab regular scanning program (a scanning tool under linux):

  Example: * / 5 **** php /data/static/index.php

         * / 5 **** php: Use asterisks to represent five time zones: the first star represents minutes, and the subsequent stars represent time, day, month, and week in sequence. Obviously, it now means that every 5 minutes is executed under the following path. PHP program.

          How to set the above command under Linux?

         First switch to root and enter crontab -e to edit and write * / 5 **** php /data/static/index.php

         Then use the tail -f /var/log/cron.log command to view a log of the program execution (view the execution process)


5. Partial dynamic case realization:

     Use ajax technology to achieve:

      $.ajax({

        'url': 'Dynamic path',

        'type': 'get / post', // transmission method

         'dataType': 'json', // Data type returned

          ’error‘ : function(){},

          'success' : function(result)

          {

                html = ’‘;

               // Use each function to traverse

              $.each(result.data, fuction(key,value)

              {

                     html + = .......... Here, for example, loop a li

                    // Then find or set the ID value to be put in a certain area

                    $("#a") . html(html);

              });

          }

       })


6. PHP processing pseudo-static:

    Probability: Convert dynamic URLs into static URLs (the essence is still a dynamic address)

    Function: beautiful, conducive to collecting data, etc.

    There are two processing methods:

     The first one: match by regular

       First print the global variable $ _SERVER in php and you will see a PATH_INFO, which is the address behind the domain name and then use regular to match

       preg_math ("Regular Rules", $ _SERVER ['PATH_INFO']);

    The second kind: by configuring rewrite in Apache to achieve a pseudo-static url
      Find httpd_vhosts.conf in the configuration to configure
      
           The yellow area is set to match rules. When a file with the same path and the same name as the pseudo-static file is returned in the file that just specified the path when the pseudo-static is returned, is this pseudo-static file executed? Still execute the static file that exists by itself. At this time, if the above two lines of blue color settings are turned on, only the static file that exists in this fact will be returned, otherwise the file with the pseudo static path will be asked.


Published 14 original articles · won 3 · views 2029

Guess you like

Origin blog.csdn.net/energy_tank/article/details/46885919