PHP generation static page detailed

xiaoxiao2021-03-06  17

First, PHP scripts and dynamic pages.

The PHP script is a server-side scripter that can be mixed with the HTML file by embedding or other methods, or the user requests can be processed in the form of a class, a function package. Regardless of the way, its basic principle is like this. Proposed by the client, request a page -----> web server to introduce the specified script for processing -----> script is loaded into the server ----> The PHP parser specified by the server pair scripts Perform parsing to form an HTML language form ----> Transfer the parsed HTML statements into the browser in a package. It is not difficult to see that after the page is sent to the browser, PHP does not exist, and has been converted to an HTML statement. The customer request is a dynamic file, in fact, there is no real file exists there, is a PHP resolution to a corresponding page, and then send back the browser. This page processing method is called "dynamic page".

Second, the static page.

The static page refers to a page that only includes only HTML and JS, CSS, and other clients that exist only in the server side. Its processing method is. Request by the client, request a page ----> web server confirmation and load a page ----> web server passes the page back to the browser in the form of a package. From this process, we compare the dynamic page, that is, it is available. The dynamic page needs to be parsed by the PHP parser of the web server, and the database is often required to perform the data inventory, and then the HTML language packet can be formed; and the static page, no resolution, no need to connect the database, it can be sent directly. Redightly reduce server pressure, improve server load capacity, and greatly provide page open speed and website overall open speed. However, its disadvantage is that the request must not be dynamically processed, and the file must be existed on the server.

Third, template and template analysis.

The template has not yet filled the content HTML file. E.g:

Temp.html

Code: {title} </ title> <body> this is a {file} file's templets </ body> </ html></p> <p>PHP processing:</p> <p>Templetest.php</p> <p>$ TITLE = "PHP enthusiast test template"; $ file = "Twomax Inter Test Templet, Author: Sheyi";</p> <p>$ fp = fopen ("Temp.html", "R"); $ Content = Fread ($ FP, FileSize ("Temp.html")); $ Content. = STR_REPLACE ("{file}", $ FILE, $ $ content. = str_replace ("{title}", $ TITLE, $ Content);</p> <p>Echo $ content;?></p> <p>Template parsing processing, that is, the resulting result of the result fill after the PHP script parsing process is processed. Usually by means of template classes. Currently popular template parsing classes include phplib, smarty, fastsmarty, and more. The principle of template parsing processing is usually replaced. Some programmers habits will judge, looped, etc., in the template file, use the parsing class, typically apply to the Block concept, simply as a loop process. The number of cycles is specified by the PHP script, how to loop into the form, and then the template resolution class is specifically implemented. Ok, contrast the static pages and dynamic pages, now let's talk about how to generate static files with PHP.</p> <p>PHP generation static page does not refer to PHP dynamic resolution, output HTML pages, but refers to the HTML page with PHP. At the same time, because HTML is unpackable, if there is a modification, if there is a modification, you can delete the regeneration. (Of course, you can also choose to modify it with regular, but individuals think that it is better to delete and regenerate, and some will not pay.)</p> <p>The words retired. PHP Fans with PHP file operation functions know that there is a file operation function fopen in PHP, that is, opens the file. If the file does not exist, try creating. This is the theoretical basis for PHP can be used to create an HTML file. As long as the folder with the HTML file is written (ie, the privilege definition 0777) can create a file. (For UNIX systems, Win systems do not need to be considered.) Still in the example, if we modify the last sentence, and specify a static file called Test.html in the Test directory:</p> <p>$ TITLE = "Tutomo International Test Template"; $ FILE = "Twomax Inter Test Templet, Author: Matrix @ Two_max";</p> <p>$ fp = fopen ("Temp.html", "R"); $ Content = Fread ($ FP, FileSize ("Temp.html")); $ Content. = STR_REPLACE ("{file}", $ FILE, $ $ content. = str_replace ("{title}", $ TITLE, $ Content);</p> <p>// echo $ content; $ filename = "test.html"; $ handle = fopen ($ filename, "w"); // Open the file pointer, create a file / * Check if the file is created and writable * / IF ("is_writable) {Die (" file: ". $ filename." Do not be written, please check after its property! ");} if (! fwrite ($ handle, $ content)) {/ / Write the information to file DIE ("Generate File". $ Filename. "Failed!");} Fclose ($ Handle); // Turns the pointer Die ("Create File". $ Filename. "Success!");? ></p> <p>FAQ in practical applications Solutions Reference:</p> <p>First, the list of articles: Create a field in the database, record the file name, each generate a file, save the automatically generated file name into the database, for the recommended article, simply point to the specified folder stored in the specified folder in the static file, ie can. Use the PHP operation to process the article list, save the string, and replace this string when generating a page. For example, place the table list in the page to add tag {articletable}, and in the PHP processing file: Code: $ title = "Timai International Test Template"; $ file = "Twomax Inter Test Templet, Author: Matrix @ Two_max "</p> <p>$ fp = fopen ("Temp.html", "R"); $ Content = Fread ($ FP, FileSize ("Temp.html")); $ Content. = STR_REPLACE ("{file}", $ FILE, $ "$ content. = STR_REPLACE (" {Title} ", $ TITLE, $ Content); // Generate a list Start $ list = ''; $ SQL =" SELECT ID, TILENAME from Article "; $ query = Mysql_Query; while ($ rist = mysql_fetch_array) {$ list. = ''. $ result ['title']. '';} $ content. = STR_REPLACE ("{articles", $ List, $ content); // Generate a list End // Echo $ content; $ filename = "test / test.html"; $ handle = fopen ($ filename, "w"); // Open file pointer, create files / * Check that the file is created and writable * / if (! ") {Die (" file: ". $ Filename." Do not be written, please check after its properties! ");} If (! FWRITE ($ Handle, $ Content) {// Write the information to file DIE ("Generate File". $ filename. "Failure!");} fclose ($ hand); // Close the pointer DIE ("Created File" $ filename. "Success!");?></p> <p>Second, paging problems.</p> <p>20 pieces per page as we specify pagination. The article inside a sub-channel is 45, then, first we get the following parameters by query: 1, the total number of pages; 2, the number of each page. Step 2, for ($ I = 0; $ I <Allpages; $ I ), page elements are obtained, analyzed, and article generation is executed in this loop. Different, DIE ("Create File". $ Filename. "Success!"; This sentence is removed, put it behind the loop because the statement will be aborted. Example: Code: $ fp = fopen ("Temp. HTML "," R "); $ Content = FREAD (" Temp.html "); $ ONEPAGE = '20'; $ SQL =" Select ID from Article Where Channel = '$ CHANELID' " $ query = mysql_query ($ sql); $ Num = mysql_num_rows ($ query); $ allpages = CEIL ($ Num / $ onepage);</p> <p>For ($ I = 0; $ I <$ allpages; $ i ) {IF ($ I == 0) {$ indexpath = "index.html";} else {$ indexpath = "index _". $ i. "HTML ";} $ start = $ i * $ onepage; $ list = ''; $ sql_for_page =" select name, filename, title from article where channel = '$ channelid' limit $ start, $ onepage "; $ query_for_page = mysql_query ( $ SQL_FOR_PAGE); While ($ Result = $ query_for_page) {$ list. = ''. $ title. '';} $ content = str_replace ("{articles}", $ list, $ content);</p> <p>IF (IS_FILE ($ indexpath) {@unlink ($ indexpath); // If the file already exists, delete}</p> <p>$ Handle = FOPEN ($ INDEXPATH, "W"); // Open the file pointer, create a file / * Check if the file is created and writable * / if (! is_writable ($ indexpath)) {echo "file:". Indexpath. "Do not be written, please check after its properties!"; // Modified to echo} if (! fwrite ($ handle, $ content) {// Write information to file Echo "Generate file". $ indexpath "Failure!"; // Modified to echo} fclose ($ hand); // Close pointer} fclose ($ fp); DIE ("Generate paging files complete, if generated is incomplete, please check the file permission system to regenerate ! ");"</p> <p>?></p> <p>Thoughts, such as other data generation, data input and output check, paging content points, etc., may be added to the page as appropriate.</p> <p>In the actual article system processing process, there are many problems to be considered, and there are many places where the dynamic page is required. But roughly thinking is, other aspects can be given to two.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-41026.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="41026" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.028</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '5Y3RHAAFr9aPHgu9S2_2B3B0efedVXnsPLTw6Jwttz4zCxND5d4dvMhl_2FCmfss0MEUCZbQEzXM0LMHbXH5'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>