PHP novice

zhaozj2021-02-16  88

Many features of PHP are related to other software or tools. With the PHP knowledge we have learned so far, we can try to build a simple interactive website. Using this process, we can learn a lot. Ok, we are now focusing on the construction of a typical personal website.

3.1 Plan a site generally one personal site includes a welcome page, a message page, a bookmark link page, a counter, contact information, or even photos and some music files, etc. Let's start with a title page, a contact information page and a resume page. We also need standard general page heads and bottom.

Title page --front.html

Here we have a very simple HTML file: My Personal Home - Welcome </ Title> </ head> <body> <h1> My Person Home </ h1> <H2 > Welcome </ h2> <hr> <p> Welcome to my cold house, although there is nothing happened now. </ P> <p> But I hope that I can get more immediately. </ P> <hr> <p align = "center"> <small> <i> copyright? I myself, 1999 </ i> </ small> </ p> </ body> </ html></p> <p>Contact Information Page - Count.html</p> <p>Similarly, we have a simple page: <html> <head> <title> My Personal Home - Contact Information </ Title> </ head> <body> <h1> My Personal Home </ H1> <H2 > Contact Information </ h2> <hr> <p> You can contact me by 1-800-PHP-INFO </ p> <hr> <p align = "center"> <small> <i> copyright? I myself 1999 </ i> </ small> </ p> </ body> </ html></p> <p>3.2 HTML to PHP</p> <p>As you can see above, each page has the same head and bottom. As mentioned above, you can write the same information when the workload is less, but you want to have more than 100 pages and you need to change your head or at the bottom? What is a lengthy thing of the handmade change on one page! So we should write PHP head and bottom files for these pages, then we will quote them in each HTML page. We will put these include files under a sub-directory called include. Below we will write the general content of these sites into the file.</p> <p>General Variable Setting: Common.inc <? // Total Universal Variable $ MyEmail = "PHPTalk@tnc.org"; $ myemaillink = "<a href = /" "> $ myemail </ A> "; $ myName =" php talk "; $ mysitename = $ myName." 's home point ";?></p> <p>General Page Head: Header.inc <? // Define Universal Page Head?> <Html> <head> <title> <? Echo "$ mysitename - $ title";> </ title> </ head> < Body> <h1> <? Echo "$ mysitename";?> </ H1> <h2> <? Echo "$ title";> </ h2> <hr> General page at the bottom of the page: footer.inc <? // The bottom of the general page?> <Hr> <p align = "center"> <small> <i> Copyright? By <? Echo "$ myname ($ myemaillink)";>, 1999 </ i> </ small> < / P> </ body> </ html></p> <p>New page front.php3: <? Include ("incrude / common.inc"); $ title = "welcome"; include ("incrude / header.inc");?> <P> Welcome to my cold house, though There is no current time for the time being. </ P> <p> But I hope that I can get more immediately. </ P> <? Include ("incrude / footer.inc");?></p> <p>New Cont.php3: <? Include ("incrude / common.inc"); $ title = "contact information"; include ("include / header.inc");?> <P> You can pass 1-800- PHP-INFO Contact me </ p> <? Include ("incrude / footer.inc");?></p> <p>Now you can guess the benefits of this arrangement. If you want to change the head or bottom of the page, you only need to change the corresponding file. If you want to modify your e-mail address or even your name, just modify the common.inc file. Also worth noting is that you can include files with any file name or file extension into your file, you can even contain files on other sites.</p> <p>3.3 counter</p> <p>Let's add a counter on the homepage. This example has been speaking many times, but it is still conducive to how to read and write files and create their own functions. Counter.inc contains the following code: <? / * || A simple counter * / function get_hitcount ($ counter_file) {/ * zero the counter This If the counter is not used, the initial value will be 1 you can of course also The initial value is set to 20000 to deceive * / $ count = 0; // If the storage counter file already exists, read the content IF (File_exists ($ counter ($ counter_file, "r" ); // We only take the top 20, I hope your site should not be too popular. $ Count = 0 fgets ($ fp, 20); // Due to the function fgets () returns a string, we can use 0 The method automatically converts it to an integer fclose ($ fp); // Take the file operation} // Add a count value $ count ; // Write the new count value to file $ fp = fopen ($ counter_file, "w "); FPUTS ($ fp, $ count); fclose ($ fp); # 数 数 值 RETURN ($ count);}?> and then we change the Front.php3 file to display this counter: <? incrude (" including include / Counter.inc "); // I put the count value in the file counter.txt, read and output Printf (" <center> <b>% 06d </ b> </ center> <br> / N ", Get_hitcount ("counter.txt")); include ("incrude / footer.inc");?> Take a look at our new Front.php33.4 feedback form</p> <p>Let's add a feedback form so that your viewer fills in and e-mail. For example, we implement it in a very simple way, we only need two pages: a input form for the viewer; a getting form data and processes, mail gives you.</p> <p>Getting form data in PHP is very simple. When a form is sent, the individual elements contained in the form is assigned a corresponding value, so that it can be used as the general variable is referenced. <Form name = "myform" action = "process_form.php3" Method = "post"> <input type = "text" name = "mytext" value = "some value"> </ form></p> <p>In Process_Form.php3, the variable $ mytext is given the input value - very simple! Similarly, you can get a variable value from a list box, multi-select box, a radio box, buttons. The only thing you have to do is to name each element in the form for future reference.</p> <p>According to this method, we can generate a form that contains three elements: name, e-mail address, and message. When the browser sends a form, process the PHP page of the form (SendFDBK.php3) read the data, check if the name is empty, and finally give you the data mail.</p> <p>Form: form.php3 <? Include ("incrude / common.inc"); $ title = "feedback"; include ("incrude / header.inc");?> <P> <form action = "sendfdbk.php3" Method = "post"> <input type = "text" name = "name" value = "Your name" size = "20" maxlength = "30"> <input type = "text" maxlength = "40" width = " 20 "Value =" Your email "name =" email "> <br> <textarea rows =" 7 "cols =" 40 "name =" comment "> Your feedback on my home point. </ Textarea> <br> < INPUT TYPE = "Submit" value = "send feedback!"> </ Form> </ p> <? Include ("include / footer.inc");?> Processing form: sendfdbk.php3 <? Incrude ("incrude / Common.inc "); $ title =" feedback "; include (" incrude / header.inc "); if ($ name ==") {// Now I hate anonymous message! Echo "duh? how come you are anonymous?";} elseif ($ name == "Your name") {// This viewer really does not reveal your name! Echo "Hello? <b> Your name </ b> is supposed to be replaced withyour actual name! </ b>";} else {// Output a courteous thanks echo "Hello, $ name. <br> Thank You for your feedback. It is getly appreciated. <br> Thanks You <br> $ MyName <br> $ myemaillink "; // Last Mail Go out Mail ($ MyEmail," Feedback. "," Name: $ namee-mail: $ EMAILCOMMENT: $ comment ");} include (" incrude / footer.inc ");?></p> <p>3.5 Simple Search Engine</p> <p>PHP can call external programs. In a UNIX environment we can use the program GREP to implement a simple search engine. We can do a slightly complex: Use a page to output a form for users to enter search strings and output query results. <? include ("incrude / common.inc"); $ title = "soarch"; include ("incrude / header.inc");?> <p> <form action = "<? echo" $ PHP_SELF ";? > "Method =" POST "> <input type =" name = "searchstr" value = "<? Echo" $ searchstr ";>" SIZE = "20" MaxLength = "30"> <input type = " Submit "Value =" Search! "> </ form> </ p> <? if (! EMPTY ($ SearchStr)) {// EMPTY () is used to check if the query string is empty // If it is not empty, Call GREP query Echo "<hr> / n"; // Call GREP to check for all files $ cmdstr = "grep -i $ searchstr *"; $ fp = POPEN ($ cmdstr, "r" ); // execute commands and output pipes $ myResult = array (); // Store Query result while ($ buffer = fgetss ($ fp, 4096)) {// grep returns this format: File name: Match string Number // So we use the function split () separate processing data list ($ FNAME, $ FLINE) = Split (":", $ buffer, 2); // We only output the first matching result if (! Defined) $ MyResult [$ FNAME]) $ myResult [$ fname] = $ fine;} // Now we store the results in an array, you can handle and output if (count ($ myresult)) {echo <ol > / n "; while (list ($ fname, $ fline) = Each ($ myresult)) Echo" <li> <a href=/" $ fname/"> $ fname </A>: $ fline </ li > / n "; echo" </ ol> / n ";} else {// If there is no query result echo" Sorry. Search ON <b> $ searchstr </ b> returned no results. <br> / n "; } PCLOSE ($ fp);}?> <incrude ("include / footer.inc";?> Note:</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-11810.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="11810" 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.048</b>, SQL: <b>12</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 = 'hlQyG7XhSeWggk1vB2tQ2FfzQBCsfSzfR8TaAhuBtYad_2BbhNGeMJ35n2lc2N_2B8VoOi39YRI0cfCLdrg7BJk3UQ_3D_3D'; 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>