Summary of common problems in PHP

zhaozj2021-02-16  91

If there is any error, you have a better idea, welcome to post. Please take a closer look beforewards.

PHP manual

,

MySQL manual

And the settings inside PHPINFO I hope you read it.

PHP programming standard

PHP manual download address

1: Why can't I get a variable?

I won't have any value when I output $ Name when I output $ Name? Register_global defaults in the version after PHP4.2, if I want to get the variable submitted from another page: Method 1: Locate register_global in php.ini, set it to ON. Method 2: Put this extra in front of the page, put it onto this Extract ($ _ get); (Note Extract ($ _ session) must have Session_Start ()). Method 3: A read variable $ A = $ _ get ["a"]; $ b = $ _ post ["b"], etc., although this method is troublesome, but is safe.

2: Debug your program

You must know what a variable must be known at runtime. I did this, build a file debug.php, the content is as follows:

PHP code:

"; echo "This page obtained:"; Print_R ($ _ get); echo "This page received _post variables are:"; Print_r ($ _ post); echo "This page received _Cookie variables are:"; Print_R ($ _ cookie); Echo "This page is:"; Print_R ($ _ session); echo " "

Then set in php.ini: include_path = "c: / php", and put the debug.php in this folder, you can include this file in each web page, view the obtained variable name and value .3: How to Using sessions All with session, you must call the function session_start (); it is simple to pay for the session, such as:

PHP code:

After PHP4.2, you can pay for the session:

PHP code:

Cancel session can be like this:

PHP code:

>

Cancel a session variable is also bug above PHP4.2. Note: 1: No output before calling session_start (). For example, it is wrong. =============== =========================== 1 line 2 line =========================================== == Tip 1: Any "....... HEADERS ALREADY SENT ..........", it is sent to the browser before session_start (). It is normal to remove the output, (cookie also This error occurs, the error reason is the same) Tip 2: If your session_start () is placed in the loop statement, and it is difficult to determine where to output information to the browser, you can use the following method: 1 line ...... Here is your procedure ... 2: What is wrong with this? Warning: session_start (): Open (/ tmp / sess_7d190aa36b4c5ec13a5c1649cc2da23f, o_rdwr) failed: ... Because you don't specify the storage path of the session file. Solution: (1) Open php.ini in the C disk, find session.save_path, modified to session.save_path = "C: / TMP" 4 : Why do I send variables to another web page, I only get the first half, and all lost PHP code at the beginning of the space:

Receive.php content:

PHP code:

"; echo $ _GET ["name"]; echo "";?>

The correct way is:

PHP code:

You don't need to use urldecode () in the receiving page, and the variable will automatically encode .5: How to intercept the designated length of Chinese characters without the end of "?>", Exceeding the part to "..." instead of general, to take it The variable comes from mysql, first to ensure that the length is long enough, generally CHAR (200), can keep 100 Chinese characters, including punctuation.

PHP code:

= $ LEN) Break; IF (Substr ($ Str, $ I, 1))> 0xA0) {IF ($ Clen> = $ Start) $ TMPSTR. = Substr ($ Str, $ I , 2); $ I ;} else {IF ($ Clen> = $ Start) $ TMPSTR. = Substr ($ Str, $ I, 1);}} Return $ TMPSTR;} Function Showsh ($ STR, $ LEN) {$ Tempstr = CSUBSTR ($ STR, 0, $ LEN); if ($ STR <> $ TEMPSTR) $ TEMPSTR. = "..."; // To end, modify here. Return $ TEMPSTR; } 6: Specifies your SQL statement in front of the table, the field is "` ", so it will not have an error because of misuse the keyword, of course, I don't recommend you to use keywords. For example, $ sql =" Insert Into` Xltxlm` (`Author`,`, `date`) Values ​​('XLTXLM', 'Use`', 1, 'Criterion Your Sql string', '2003-07-11 00 : 00: 00 ') "" `" How to enter? On top of the Tab button. 7: How to make the string of the HTML / PHP format is not interpreted, but is displayed as

PHP code:

PHP "; echo "is explained:" $ Str. "
processed:"; Echo Htmlentities (NL2BR ($ STR));? >

8: How to get the variable value outside the function in the function

PHP code:

>

9: How do I know what function is supported by default?

PHP code:

"; echo "here display all functions supported by the system, and self-qualified function PHP / N"; Print_R ($ ARR); Echo "";?>

10: How to compare two dates a few days

PHP code:

11: Why after I upgrade PHP, the original program appears full screen NOTICE: Undefined Variable: This is the meaning of warning, because the variable is not defined. Open php.ini, find the lowermost error_reporting, modify to Error_Reporting = E_ALL & ~ E_NOTICE For PARSE ERROR Error Error_Reporting (0) Can't turn off. If you want to close any error prompt, open php.ini, find display_errors, set to display_errors = Any error will not be prompted later. What is ERROR_REPORTING? 12: I want to add a file in front of each file, but in the end, but a one is very trouble 1: Open the php.ini file settings include_path = " C: "2: Write two files auto_prepend_file.php and auto_append_file.php Save in the C drive, they will automatically attach the head and tail of each PHP file. 3: Find in php.ini: Automatically Add Files Before or After any php document.auto_prepend_file = auto_prepend_file.php; Reit the head auto_APpend_file = auto_append_file.php; After the end of the tail, you will be equivalent to each PHP file.

PHP code:

13: How to use the PHP upload file

PHP code:

Upload file form </ title> </ head> <body> <form encType = "Multipart / Form-data" action = "" "" Method = "POST"> Select file: <br > <input name = "UPLOAD_FILE" type = "file"> <BR> <input type = "submit" value = "Upload File"> </ form> </ body> </ html> <$ upload_file = $ _ files ['UPLOAD_FILE']; $ upload_file_name = $ _ files ['UPLOAD_FILE'] ['Name']; if ($ upload_file) {$ file_size_max = 1000 * 1000; // 1m limit file upload maximum capacity (Bytes ) $ store_dir = "d: /"; // Upload file storage location $ accept_overwrite = 1; // Do not allow overwriting the same file // Check file size if ($ upload_file_size> $ file_size_max) {echo "Sorry, your file Capacity is greater than the specified "; exit;} // check the read and write file IF (file_exists ($ store_dir. $ Upload_file_name) &&! $ Accept_overwrite) {echo" file "; exit;} // copy file to the specified directory IF (! Move_uploaded_file ($ upload_file, $ store_dir. $ upload_file_name)) {echo "copy file failed"; exit;}} echo "<p> You uploaded the file:"; Echo $ _files ['UPLOAD_FILE'] ['Name ']; echo "<br>"; // The original name of the client machine file. The MIME type of the echo file is: "; echo $ _files ['UPLOAD_FILE'] ['type']; // file MIME type, you need to provide this information for the information, such as" image / gif ". Echo "<br>"; Echo "Upload File Size:"; Echo $ _Files ['UPLOAD_FILE'] ['size']; // The size of the file has been uploaded, the unit is byte. Echo "<br>"; Echo "file uploaded after being temporarily stored as:"; echo $ _files ['UPLOAD_FILE'] ['TMP_NAME']; // The file is stored in the server after the file is stored.</p> <p>Echo "<br>"; $ erroe = $ _ files ['UPLOAD_FILE'] ['Error']; Switch ($ Erroe) {Case 0: Echo "Upload Success"; Break; Case 1: Echo "uploaded files exceeded The value of the UPLOAD_MAX_FILESIZE option restricted. "; Break; Case 2: Echo" Uploaded file size exceeds the value specified in the HTML form. "; Break; Case 3: Echo" file is only partially uploaded "; Break; Case 4: Echo "No file is uploaded"; Break;}?> 14: How to configure the lower part of the GD library is my configuration process 1: Use the dos command (you can also manually, copy all DLL files in the DLLS folder SYSTEM32 directory) COPY C: / PHP / DLLS / *. DLL C: / Windows / System32 / 2: Open php.ini Sets EXTENSION_DIR = "C: / PHP / EXTENSIONS /"; 3: Extension = php_gd2.dll; The comma in front of Extension is removed. If there is no php_gd2.dll, php_gd.dll is the same, the guarantee does exist this file C: /PHP/EXTENSIONS/PHP_GD2.DLL4: Run the following program to test</p> <p>PHP code:</p> <p><? php ob_end_flush (); // Note that you cannot output any information to your browser until you have set whether auto_prepend_file. Header ("Content-Type: Image / PNG"); $ IM = @ImageCreate (200, 100) OR DIE ("Unable to create images"); $ background_color = imagecolorallocate ($ IM, 0, 0, 0); $ text_color = imagecolorallocate ($ IM, 230, 140, 150); ImageString ($ IM, 3, 30 , 50, "a Simple Text String", $ text_color; imagepng ($ IM);?></p> <p>Click here to view the result 15: What is the UBB code UBB code is a variant of HTML. It is a special TAG that uses this program in a foreign BBS program. Even if there are many places in China. Even if you use HTML, You can also use ubbcode? Maybe you prefer to use ubbcode? Instead of HTML, even if the forum allows HTML, because the code is less and safer, there is an example in the UBB of Q3BOY, you can run test 16: I want to modify my mysql's users, the password must first declare that in most cases, modifying mysql is a root permission in MySQL, so the general user cannot change the password unless the administrator is requested. Method is used to use phpMyadmin, this is the most Simple, modify the User table of the MySQL library, but don't forget to use the Password function. Method 2 uses mysqladmin, which is a special case of the previous declaration. Mysqladmin -u root -p password mypasswd Enter this command, you need to enter the original password of the root, and then the root's password will be changed to mypasswd. Change the root in the command to your username, you can change your own password. Of course, if your mysqladmin connection is not mysql server, or you have no way to execute mysqladmin, then this method is invalid. And mysqladmin cannot empty your password. The following method is used in the MySQL prompt, and must have a mysql root permission: Method 3 mysql> INSERT INTO mysql.user (Host, User, Password) Values ​​('%', 'Jeffrey', Password ('Biscuit' )))); Mysql> Flush privileges is exactly that this is increasing a user, the username is Jeffrey, the password is Biscuit. There is this example in the "MySQL Chinese Reference Manual", so I will write it. Note To use the Password function, then use Flush Privileges. Methods 4 and Methods, like the three, just use the Replace statement mysql> Replace Into mysql.user (Host, User, Password) VALUES ('%', 'Jeffrey', Password ('Biscuit')); mysql> Flush Privileges method 5 Use the set password statement, mysql> set password for jeffrey @ "%" = password ('biscuit'); you must also use the password () function, but do not need to use flush privileges. Method 6 Use Grant ... Identified by statement mysql> grant usage on *. * To jeffrey @ "%" Identified by 'biscuit'; here the password () function is unnecessary, nor does it need to use Flush Privileges. Note: Password () [is not] The password encryption is applied to the same method encrypted at UNIX password. 17: I want to know which site is connected to this page PHP code:</p> <p><? php // The super connection must be used to output Echo $ _SERVER ['http_referer'];?> 18: Data is put into the database and take it out to display what to pay attention to 入 Str = addslashes ($ STR) $ SQL = "Insert Into` Tab` (`Content`) VALUES ('$ STR')"; went out $ str = stripslashes ($ STR); Show $ Str = HTMLSpecialchars (NL2BR ($ Str)); 19: How to read the current address bar information</p> <p>PHP code:</p> <p><? PHP $ S = "http: // {$ _server ['http_host']}: {$ _server [" server_port "]} {$ _ server ['script_name']}"; $ SE = ''; foreach ($ _Get as $ key => $ value) {$ SE. = $ key. "=" $ Value. "&";} $ Se = preg_replace ("/( (" /( ("/( (" / (")," $ 1 ", $ 1 SE); $ SE? $ SE = "?" $ SE: ""; Echo $ s. "$ se";?></p> <p>20: I click the back button, why did you fill in something? This is because you use the session. Solution:</p> <p>PHP code:</p> <p><? php session_cache_limiter ('private, must-revALIDATE'); session_start (); ........... .....................</p> <p>21: How to display IP addresses in the picture</p> <p>PHP code:</p> <p><? Header ("Content-Type: Image / PNG"); $ IMG = ImageCreate (180, 50); $ IP = $ _SERVER ['Remote_addr']; ImageColortransparent ($ IMG, $ BGCOLOR); $ bgcolor = imagecolorlocate $ IMG, 0x2C, 0x6D, 0xAF); // Background Color $ shadow = ImageColoralLocate ($ IMG, 250, 0); // Shadow Color $ TEXTCOLOR = ImageColoralLocate ($ IMG, Oxff, Oxff, Oxff); // Font Color ImageTfText ($ IMG, 10, 78, 30, $ Shadow, "D: /Windows/fonts/tahoma.ttf", $ IP); // Display Background ImageTfText ($ IMG, 10, 0, 25, 28, $ textColor, "D: /Windows/fonts/tahoma.ttf", "Your IP IS". $ IP); // Displays IP ImagePng ($ IMG); ImageCreateFromPng; ImageDestroy; ImageDestroy; ?></p> <p>22: How to get the real IP of the user</p> <p>PHP code:</p> <p><? Function iptype1 () {if ("http_client_ip")) {return getenv ("http_client_ip");} else {return "none";}}} function iptype2 () {if (GetENV ("http_forwarded_forward_for")) { Return getev ("http_x_forwarded_for");} else {return "none";}} function}} function} {ified ("remote_addr")) {Return getenv ("remote_addr");} else {return "none";} } Function IP () {$ IP1 = iptype1 (); $ IP2 = iptype2 (); $ IP3 = iptype3 (); if (Isset ($ IP1) && $ IP1! = "None" && $ ip1! = "unknown" ) {RETURN $ IP1;} elseif (isset ($ IP2) && $ IP2! = "NONE" && $ IP2! = "Unknown") {Return $ IP2;} elseif (isset ($ IP3) && $ IP3! = " NONE "&& $ ip3! =" unknown ") {return $ ip3;} else {return" none ";}} echo ip ();?> 23: How to read all records within three days from the database first in the form A datetime field record time, format is' 2003-7-15 16: 50: 00'select * from `xltxlm` WHERE to_DAYS ()) - to_DAYS (` Date`) <= 3; 24: How to remotely link mysql The database has an Host field in increasing the user's MySQL table, modified to "%", or specify the IP address that allows the connection, so you can call remotely. $ link = mysql_connect ("192.168.1.80:3306", "root", ""); 25: How to use the special character in the regular expression in the regular expression 26: After using Apache, the home page is garbled. 1: AddDefaultcharset ISO -8859-1 Change to ADDDEFAULTCHARSET OFF Method 2: AddDefaultcharset GB2312</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-13408.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="13408" 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.052</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 = 'JOGeFc65WmaLr_2Biq5rDT3YFtrhqceNskc3jUOSNYncPP0jdWvOD8t7Bq8jkpGz7VBSjiCd6xikhFA90i'; 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>