Summary of common problems in PHP

zhaozj2021-02-16  93

This article is reproduced from http://blog.9cbs.net/decolee/archive/2004/07/25/5128.aspx

Summary of common problems in PHP

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:

Ob_start (); session_start (); echo "; echo" This page received _GET variables are: "; Print_R ($ _ get); echo" This page obtained _POST variables are: "; Print_R ($ _ POST); Echo "This page" The _cookie variable is: "; 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:

Session_start (); $ name = "This is a session example"; session_register ("name"); // notice, don't write: session_register ("$ name"); echo $ _SESSION ["name"]; // _Session ["name"] "Is this a session example?>

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

PHP code:

Session_start (); $ _SESSION ["name"] = "value";?>

Cancel session can be like this:

PHP code:

Session_start (); session_unset (); session_destroy ();?>

Cancel a session variable is also bug above PHP4.2. Note: 1: No output before calling session_start (). For example, it is wrong. =============== =========================== 1 row 2 lines 3 line session_start (); // Previous line already output 4 lines. .... 5 line?> =========================================== Tip 1: Anyone who appears "...... Headers Already Sent ........", it is to output information to the browser before session_start (). It is normal to remove the output, and the cookie will also appear. This error, the error reason is the same) Tip 2: If your session_start () is placed in a loop statement, it is difficult to determine where to output information to the browser, you can use the following method: 1 line

........ here is your program ... 2: What is wrong with this? Warning: session_start (): open (/ tmp / sess_7d190aa36b4c5ec13a5c1649cc2da23f, o_rdwr) failed: .... because you don't specify Session file storage path. Workaround: (1) Open php.ini in the C disk, find session.save_path, modified to session.save_path = "c: / tmp" 4: Why do I want to When a web page is transmitted, only the first half is obtained, and all lost in space.

PHP code:

$ VAR = "Hello PHP"; // Modified to $ VAR = "Hello PHP"; try what results get $ pos = "Receive.php? Name =". $ Var; header ("location: $ post"); ?>

Receive.php content:

PHP code:

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

The correct way is:

PHP code:

$ VAR = "Hello PHP"; $ pos = "Receive.php? Name =". Urlencode ($ VAR); Header ("Location: $ POST");?>

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:

$ STR = "This character is long, ^ _ ^"; $ Short_Str = Showsh ($ STR, 4); // Intercept four Chinese characters, the result is: this character ... Echo "$ Short_STR"; Function Csubstr ($ Str, $ Start, $ LEN) {$ Strlen = Strlen ($ STR); $ Clen = 0; for ($ I = 0; $ I = $ Start $ LEN) Break; IF (Substr ($ Str, $ I, 1))> 0xA0) {IF ($ Clen> = $ Start) $ TMPSTR. = SUBSTR ($ 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 the form, the field plus "` ", so it will not have an error because of the misused keyword, of course, I don't recommend you to use keywords. For example, $ sql =" Insert Into `XLTXLM) `author`,` title`, `id`,` `,` value ('xltxlm', 'use`, 1,' criterion your sql string ',' 2003-07-11 00:00: 00 ') "` "How to enter? On the Tab key. 7: How to make the string of the HTML / PHP format is not interpreted, but is displayed as

PHP code:

$ Str = "PHP"; Echo "is explained:" $ str. "After processing:"; echo htmlentities (NL2BR ($ STR));?>

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

PHP code:

$ a = "php"; foo (); function foo () {global $ a; // Delete here to see what results Echo "$ a";}?>

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

PHP code:

$ arr = get_defined_functions (); function php () {} echo "; echo" here display all functions supported by the system, and custom function PHP / N "; Print_R ($ arr); echo";?>>>>

10: How to compare two dates a few days

PHP code:

$ DATE_1 = "2003-7-15"; // can also be: $ DATE_1 = "2003-6-25 23:29:14"; $ DATE_2 = "1982-10-1"; $ DATE_LIST_1 = EXPLODE (" - ", $ DATE_1); $ DATE_LIST_2 = EXPLODE (" - ", $ DATE_2); $ D1 = MKTIME (0, 0, 0. $ DATE_LIST_1 [1], $ DATE_LIST_1 [2], $ DATE_LIST_1 [0]); $ D2 = MKTIME (0, 0, $ DATE_LIST_2 [1], $ DATE_LIST_2 [2], $ DATE_LIST_2 [0]); $ Days = ROUND (($ D1- $ D2) / 3600/24); echo " I have already struggled $ days day ^ _ ^ ";?> 11: Why did I upgrade PHP, the original program appears full screen NOTICE: Undefined Variable: This is the warning, because the variable is not defined. Open PHP. INI, find the bottom Error_Reporting, modified to Error_Reporting = E_ALL & ~ E_NOTICE For Parse Error Error Error_Reporting (0) Can't turn it off. If you want to close any error prompts, open php.ini, find display_errors, set to display_errors = OFF. No mistakes will be prompted. So what is ERROR_REPORTING? 12: I want to add a file in front of each file, but one adds a 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 to the head and tail of each PHP file. 3: Find in php.ini: Automatical Add Files Before or after ANY PHP Document.Auto_prepend_file = auto_prepend_file.php; It is attached to 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:

Include "auto_prepend_file.php"; ....... // Here is your program incrude "auto_append_file.php";?>

13: How to use the PHP upload file

PHP code:

$ Upload_file = $ _ FILES [ 'upload_file'] [ 'tmp_name']; $ upload_file_name = $ _ FILES [ 'upload_file'] [ 'name']; if ($ upload_file) {$ file_size_max = 1000 * 1000; // 1M restricted file Upload Maximum Capacity (Bytes) $ store_dir = "d: /" // Upload file storage location $ accept_overwrite = 1; // Whether to 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) &&! $ Access_overwrite) {echo" exists with the same file name "; exit;} // Copy files to the specified directory IF (! Move_uploaded_file ($ upload_file, $ store_dir. $ Upload_file_name)) {echo "copy file failed"; exit;}} echo "You uploaded the file:"; Echo $ _files ['UPLOAD_FILE'] [ 'name']; echo "; // 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 "; echo" upload file size: "; echo $ _files ['UPLOAD_FILE'] ['size']; // The size of the file has been uploaded, the unit is byte. Echo "; echo" file uploaded after being temporarily stored as: "; Echo $ _FILES ['UPLOAD_FILE'] ['TMP_NAME']; // The file is stored after the server is stored. echo ""; $ erroe = $ _ files ['UPLOAD_FILE'] ['Error']; Switch ($ Erroe) {Case 0: Echo "Upload Success"; Break; Case 1: Echo "uploaded files more than php.ini The value of the UPLOAD_MAX_FILESIZE option is limited. "; Break; Case 2: Echo" Upload file size exceeds the value specified in the HTML form. "; Break; Case 3: Echo" file only partially uploaded "; Break; Case 4: echo "No file is uploaded"; Break;}?>

14: How to configure the lower below my configuration process 1: Use the dos command (you can also manually, copy all DLL files in the DLL file to the 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; remove the comma in front of Extension, if there is no php_gd2.dll, php_gd.dll is the same, Guaranteed this file C: /php/extensions/php_gd2.dll4: Run the following program to test PHP code:

OB_END_FLUSH (); // Note that you cannot output any information to your browser until you 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);?>

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:

/ / Must enter the output echo $ _SERVER ['http_referer'] by super connections.

18: Data is placed in the database and take it out to display what to pay attention to the warehouse $ str = addslashes ($ STR); $ sql = "Insert Into` Tab` (`Content`) VALUES ('$ STR')" Time $ Str = StripsLashs ($ Str); Show $ Str = HTMLSpecialchars (NL2BR ($ Str)); 19: How to read the current address bar information PHP code:

$ S = "http: // {$ _server ['http_host']}: {$ _server [" server_port "]} {$ _ server ['script_name']}"; $ SE = ''; foreach ($ _GET AS $ Key => $ value) {$ se. = $ key. "=" $ value. "&"; "} $ se = preg_replace (" /( (.*)); "/( (.*)); $ SE? $ SE = "?" $ SE: ""; Echo $ s "? $ se";?>

20: I click the back button, why did you fill in something? This is because you use the session. Solution:

PHP code:

Session_cache_limiter ('private, must-revALIDATE); session_start (); ........... .................................................................................................................................................................................................................................................

21: How to display IP addresses in the picture

PHP code:

Header ( "Content-type: image / png"); $ img = ImageCreate (180,50); $ ip = $ _SERVER [ 'REMOTE_ADDR']; ImageColorTransparent ($ img, $ bgcolor); $ bgColor = ImageColorAllocate ($ img , 0x2c, 0x6d, 0xAF); // background color $ shadow = imagecolorallocate ($ IMG, 250, 0); // Shadow color $ textColor = ImageColoralLocate ($ IMG, OXFF, OXFF, OXFF); // Font Color ImageTfText ($ IMG, 10, 0, 78, 30, $ Shadow, "D: /Windows/fonts/tahoma.ttf", $ IP); // Show background imageTftext ($ IMG, 10, 0, 25, 28, $ TEXTCOLOR, "D: /Windows/fonts/tahoma.ttf", "Your IP IS". $ IP); // Displays IP ImagePng; ImageCreateFromNG ($ IMG); ImageDestroy;?>

22: How to get the real IP of the user

PHP code:

function iptype1 () {if (getenv ( "HTTP_CLIENT_IP")) {return getenv ( "HTTP_CLIENT_IP");} else {return "none";}} function iptype2 () {if (getenv ( "HTTP_X_FORWARDED_FOR")) {return getenv ("Http_x_forwarded_for");} else {return "none";}} function}} function}} ("remote_addr")) {Return GetENV ("remote_addr");} else {return ";}} 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 from the database for three days, first, you have a DateTime in the form. Field recording time, format is' 2003-7-15 16: 50: 00'select * from `xltxlm` WHERE to_DAYS (now ()) - to_DAYS (` Date`) <= 3; 24: How to remotely link MySQL database Increasing the user's MySQL table has a Host field, 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

Published on July 25, 2004 2:36 PM

Rel = "pingback">

转载请注明原文地址:https://www.9cbs.com/read-12053.html

New Post(0)