Other features of PHP

xiaoxiao2021-03-06  39

Learning

Other features of the 5.PHP: crystal5 Other miscellaneous 5.1 PHP can manipulate image generation process images. If you have already installed a GD library, you can even use a PHP to generate an image. Call, then the above-described button.php3 code acquires the Text value and add this value in another image file - the image file in the above code is images / button1.gif - final output Go to the browser. If you want to use the image button in the form field, it is not desirable to regenerate a new image after the text on each button will be changed, and the image file can be dynamically generated using such a simple method. 5.2 Cookies PHP supports http-based cookies. You can use cookies as needed in need of using a general variable. Cookies is some information segments that are saved on the client, so you can know if anyone on a specific PC has visited your site, and the viewer is in your site. Traces, etc. A typical example of using cookies is the screening of the browser preference. Cookies is set by the function setCookie (). Like the function header () output HTTP header, setCookie () must be called before any actual content cup is output to the browser. The following is a simple example: " again; // Read cookie and determine if IF ((Time () - $ visitedbefore)> = "(60 * 60 * 24 * 7) ") Echo" Why Did You Take A Week To Come Back. You Should Be Here More Often !? ";}?> 5.3 Based on HTTP Verification Based on HTTP Verification When PHP cannot be implemented when the CGI mode is run. We can use the function header () to send HTTP header forced verification, and the client browser pops up a dialog box for entering the username and password. These two variables are stored in $ PHP_AUTH_USER and $ PHP_AUTH_PW, you can use these two variables to verify legality and allow access.

The following example passed the user name / password to the verification of TNC / Nature, a user's login: In fact, it is unlikely that the user name / password pair, Instead, use the database or encrypted password file to access them. 5.4 File Upload You can use the PHP implementation file function, pay attention to the client's browser should be Netscape3 or more or more.

The following is a simple demonstration of this feature: UPLOAD YOUR FILE </ Title> </ head> <body> <form action = "received.php3" enctype = "Multipart / form-data "method = post> <input type =" hidden "name =" max_file_size "value =" 2000000 "> <input type =" file "name =" UPLOADFILE "SIZE =" 24 "MaxLength =" 80 "> <BR> <BR> <INPUT TYPE = "SUBMIT" VALUE = "Upload File!" NAME = "sendit"> <INPUT TYPE = "SUBMIT" VALUE = "Cancel" NAME = "cancelit"> <BR> </ FORM > <I> <font size = "2"> (You May Notice a Slight Delay While We Upload Your File.) </ Font> </ i> </ body> </ html> The following is to process uploaded files: ( ? receiver.php3): <function do_upload () {global $ uploadfile, $ uploadfile_size; global $ local_file, $ error_msg; if ($ uploadfile == "none") {$ error_msg = "You did not specify a file for uploading. "}}}}} {$ error_msg =" sorry, your file is to lad. "; return;} $ THE_TIME = TIME (); // You need to write permission to write permission $ upload_dir = "/ local / uploads"; $ local_file = "$ upload_dir / $ THE_TIME"; if (file_exists ('$ local_file')) { $ Seq = 1; while (file_exists ( "$ upload_dir / $ the_time $ seq")) {$ seq ;} $ local_file = "$ upload_dir / $ the_time $ seq";}; rename ($ uploadfile, $ local_file); display_page ();} function display_page () {// Here is your page content} <html> <head> <title> php3 receiving script </ title> <</p> <p>/ HEAD> <? BODY> <if ($ error_msg) {echo "<B> $ error_msg </ B> <BR> <BR>";} if ($ sendit) {do_upload ();} elseif ($ cancelit) {Header ("Location: $ SOME_OTHER_Script"); exit;} else {some_other_func ();}?> </ body> </ html> 5.5 common functions We simply look at some common functions. Array array - generates array count - array elements Sort - array sorting, other sorting functions available for use list - List array elements EACH - Return to the next key / value to Current - Return to the current array element next, prev - Back to the current array element before and after the date and time Checkdate - Verification Date / Time Format Date - Generate Date / Time Format Time - Current Time Information Strftime - Format Date / Time Directory, File System ChDir - Change Directory Dir - Directory Category OpenDir , ReadDir, CloseDir - Open, Read, Turn off Directory Fopen, Fclose - Turn on, Close File FGETS, FGETSS - Read Content File - Read the entire file in a group variable regular expression EREG - match regular expression EREGI - Size-sensitive non-sensitive matching regular expression EREG_REPLACE - Match the regular expression and replace EREGI_REPLACE - Unsochemically non-sensitive matching regular expression and replaces Split - Store strings according to rules and stores strings in an array situation - plus slash After using a string echo - Output one or more strings Join, Implode - combines the array elements into strings HTMlentities, HTMLSpecialchars - Convert HTML Special Characters to HTML Tag Forms Split - Cut strings according to rules and store them in array 5.6 Extending Our Sample Homepage We will use some of the above functions and ideas to add more dynamic content to our sample homepage. We can add the navigation bar at the top of each page, while making the current page automatically not displayed; at the same time, you can also add a user verification form to upload music, images and other files and automatically update the page. The navigation bar is actually plus a code in the footer.inc file. Assume that all the suffixes in your Web site will appear in the navigation bar, the following is the code stored as include / navbar.inc: <? / * Output the navigation bar, link all in the station in the current page. PHP3 file * / # read directory $ d = dir ("./"); echo "<p align = /" center / "> | / n"; while ($ entry = $ d-> read ()) { / / Ignore the file IF (! Is_file ($ entry) Continue; / * Separate the file name with the extension.</p> <p>Since it is a regular expression special character, it should be used / extracted * / list ($ filenm, $ fileext) = split ("/.", $ ee, 2); // ignore non-.php3 files IF ($ fileext! = "Php3") Continue; / * Now we have elected .php3 files, the first (title) in the search file is similar to $ title = "something"; and separate the above title content, used as link text * / $ linknm = ""; $ fp = fopen ($ Entry, "R"); while ($ buffer = fgets ($ fp, 4096)) {$ buffer = trim ($ buffer); // We have already put each The title of the file is placed in the first line of the file to search // but it may bring a big trouble if you change the variable name, "" Title * = * / ", $ buffer) {/ * We have obtained The title content can be removed on this basis. It must be handled in PHP code, such as $ title = "Blah Blah" * / EVAL ($ buffer); // then display the link text as title text $ linknm = $ Title; Break;}} fclose ($ fp); if ($ entry == basename ($ PHP_SELF)) Echo "$ linknm"; else echo "<a href=/",11/1/"> $ linknm </ A> "; echo" | "} $ D-> close (); echo" </ p> / n ";> Photo favorite We will reference HTTP-based authentication, file system functions and file upload function maintenance placement The image file directory. At the same time we need to create a page that can list all photos in this directory.</p> <p>File upload <? Incrude ("include / common.inc"); // We are here to do a user verification if (! Isset ($ PHP_AUTH_USER)) {header ("www-authenticate: Basic realm = /" $ mysitename / ""); Header ("http / 1.0 401 unauthorized"); Echo "Sorry, You are not authorized to upload files / n"; exit;} else {if (! ($ PHP_AUTH_USER == $ MyName && $ PHP_AUTH_PW == $ Mypassword)) {// If it is a wrong user name / password pair, forced re-authentication Header ("WWW-Authenticate: Basic Realm = /" my realm / "); Header (" http / 1.0 401 unauthorized); Echo "Error: $ PHP_AUTH_USER / $ PHP_AUTH_PW Is Invalid. <p>"; exit;}}} f ($ cancelit) {// When the viewer presses the "Cancel" button, turn to the Header ("location: front_2.php3 "); exit;} function do_upload () {global $ userfile, $ userfile_size, $ userfile_name, $ userfile_type; global $ local_file, $ error_msg; global $ HTTP_REFERER; if ($ userfile ==" none ") {$ error_msg =" You Did Not Specify A File for Uploading. ";}} IF ($ Userfile_Size> 2000000) {$ Error_MSG =" Sorry, Your File is to Large. "; Return;} // wherever you have write permission below ... $ UPLOAD_DIR = "Photos"; $ LO CAL_FILE = "$ upload_dir / $ userfile_name"; if (file_exists ($ local_file)) {$ error_msg = "sorry, a file with tria name already exists"; return;}; // You can also check file name / type To determine what file: GIF, JPG, MP3 ... RENAME ($ userfile, $ local_file); echo "the file is uploaded <br> / n"; echo "<a href=/" $ http_referer/"> Go Back </a> <br> / N ";} $ title =" Upload File "; include (" incrude / header.inc "); if ($ userfile) || $ userfile ==" none ") { / / Output the following form?> <</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-77395.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="77395" 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.035</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 = 'PWMM2PixT8MSnCZpYcsMAnKeNTpRRvNnG7n2tr6hHGSS_2BKUDtcyIRj1mkAjw0stG2VDkNtusNFUoJuz8Wb36jw_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>