Php FAQs and solutions-PHP source code

Source: Internet
Author: User
Ec (2); 1: Why can't I get the variable I post data name to another webpage on one webpage? Why can't I get any value when I output $ name? In Versions later than PHP4.2, the default value of register_global is off. If you want to get the variable submitted from another page: Method 1: in PHP. find register_global in ini and set it to on. method 2: Put the extract ($ _ POST) at the top of the receiving webpage, extract ($ script ec (2), and script

1: Why can't I get the variable?

Why can't I get any value when I output $ name when I POST data name to another webpage?

In Versions later than PHP4.2, the default value of register_global is off.
To get the variables submitted from another page:

Method 1: Find register_global in PHP. ini and set it to on.
Method 2: Put the extract ($ _ POST); extract ($ _ GET) at the beginning of the receiving page. (Note that extract ($ _ SESSION) you must have Session_Start ()).
Method 3: Read the variable $ a =$ _ GET ["a"]; $ B =$ _ POST ["B"] One by one. Although this method is troublesome, it is safer.

2: Debug your program

You must know the value of a variable at runtime. In this case, create a debug. php file with the following content:

PHP code :--------------------------------------------------------------------------------

Ob_Start ();
Session_Start ();
Echo"

";

Echo "The _ GET variables obtained on this page include :";
Print_R ($ _ GET );

Echo "The _ POST variables obtained on this page include :";
Print_R ($ _ POST );

Echo "The _ COOKIE variables on this page are :";
Print_R ($ _ COOKIE );

Echo "The _ SESSION variables obtained on this page include :";
Print_R ($ _ SESSION );
Echo"
";
?>

--------------------------------------------------------------------------------

In php. ini, set: include_path = "c:/php" and put debug. php in this folder,
In the future, you can include this file in each webpage to view the variable name and value.

3: How to Use session

The session_start () function must be called before session-related operations ();

It is very easy to pay for the session, such:


PHP code :--------------------------------------------------------------------------------

Session_start ();
$ Name = "This is a Session example ";
Session_Register ("Name"); // do not write it as Session_Register ("$ Name ");
Echo $ _ SESSION ["Name"];
// After $ _ SESSION ["Name"] is "This is a Session example"
?>

--------------------------------------------------------------------------------



After php4.2, you can directly pay the value for the session:

PHP code :--------------------------------------------------------------------------------

Session_Start ();
$ _ SESSION ["name"] = "value ";
?>

--------------------------------------------------------------------------------

You can cancel the session as follows:

PHP code :--------------------------------------------------------------------------------

Session_start ();
Session_unset ();
Session_destroy ();
?>

--------------------------------------------------------------------------------


There is a BUG in canceling a session variable above php4.2.



Note:

1: No output is allowed before Session_Start () is called. For example, the following is incorrect.
========================================================== =
1 line
2 rows 3 rows Session_Start (); // output already exists in the first row
4 rows .....
5 rows?>
========================================================== =


Tip 1:

If "... headers already sent..." appears, it means to output information to the browser before Session_Start.
It is normal to remove the output (this error will also occur in the COOKIE, the same reason for the error)

Tip 2:

If your Session_Start () is placed in a 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 program ......



2: What is the error?

Warning: session_start (): open (/tmp \ sess_7d1_aa36b4c5ec13a5c1649cc2da23f, O_RDWR) failed :....
Because you have not specified the path for storing session files.

Solution:
(1) create a folder tmp on drive C
(2) Open php. ini, find session. save_path, and change it to session. save_path = "c:/tmp"



4: Why do I get only the first half when I send a variable to another web page, and all the variables starting with a space are lost?


PHP code :--------------------------------------------------------------------------------

$ Var = "hello php"; // change it to $ Var = "hello php"; What are the results?
$ Post = "receive. php? Name = ". $ Var;
Header ("location: $ post ");
?>

--------------------------------------------------------------------------------

Content of receive. php:

PHP code :--------------------------------------------------------------------------------

Echo"
";
Echo $_GET["Name"];
Echo "
";
?>

--------------------------------------------------------------------------------


The correct method is:

PHP code :--------------------------------------------------------------------------------

$ Var = "hello php ";
$ Post = "receive. php? Name = ". urlencode ($ Var );
Header ("location: $ post ");
?>

--------------------------------------------------------------------------------


You do not need to use Urldecode () on the receiving page. The variable is automatically encoded.


5: How to intercept a specified length of Chinese characters without "?> "End, the excess part is replaced "..."


In general, the variables to be intercepted come from Mysql. First, make sure that the field length is long enough. Generally, it is char (200) and 100 Chinese characters can be kept, including punctuation.

PHP code :--------------------------------------------------------------------------------

$ Str = "this character is long ,''';
$ Short_Str = showShort ($ str, 4); // extract the first four Chinese characters and the result is: this character...
Echo "$ Short_Str ";
Function csubstr ($ str, $ start, $ len)
{
$ Strlen = strlen ($ str );
$ Clen = 0;
For ($ I = 0; $ I <$ strlen; $ I ++, $ clen ++)
{
If ($ clen >=$ start + $ len)
Break;
If (ord (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 showShort ($ str, $ len)
{
$ Tempstr = csubstr ($ str, 0, $ len );
If ($ str <> $ tempstr)
$ Tempstr. = "..."; // you can modify the description here.

Return $ tempstr;
}

--------------------------------------------------------------------------------



6: standardize your SQL statements


Add "" 'in front of the field in the table, so that errors will not occur due to misuse of keywords,
Of course, I do not recommend you use keywords.

For example
$ SQL = "INSERT INTO 'xltxlm '('author', 'title', 'id', 'content', 'date') VALUES ('xltxlm ', 'use', 1, 'criterion your SQL string', '2017-07-11 00:00:00 ')"

"" 'How to input? On the TAB key.


7: How to make Html/PHP Strings not interpreted, but displayed as they are


PHP code :--------------------------------------------------------------------------------

$ Str = "PHP ";
Echo "interpreted:". $ str ."
Processed :";
Echo htmlentities (nl2br ($ str ));
?>

--------------------------------------------------------------------------------



8: how to obtain variable values outside the Function


PHP code :--------------------------------------------------------------------------------

$ A = "PHP ";
Foo ();
Function foo ()
{
Global $ a; // Delete the result.
Echo "$ ";
}
?>

--------------------------------------------------------------------------------



9: How do I know what functions are supported by the system by default?


PHP code :--------------------------------------------------------------------------------

$ Arr = get_defined_functions ();
Function php (){
}
Echo"
";
Echo "This shows all functions supported by the system, and the custom function php \ n ";
Print_r ($ arr );
Echo"
";
?>
--------------------------------------------------------------------------------


10: how to compare the differences between two dates for a few days


PHP code :--------------------------------------------------------------------------------

$ Date_1 = "2003-7-15"; // You can also: $ 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 struggled for $ Days ''';
?>

--------------------------------------------------------------------------------
11: Why does the original program show the full screen Notice: Undefined variable after I Upgrade PHP:


This is warning because the variable is undefined.
Open php. ini, find the bottom error_reporting, and change it to error_reporting = E_ALL &~ E_NOTICE

For Parse error errors
Error_reporting (0) cannot be closed.
If you want to disable any error prompt, open php. ini, find display_errors, and set it to display_errors = Off. No error will be prompted in the future.

So what is error_reporting?



12: I want to add a file at the beginning and end of each file, but it is very troublesome to add one file at a time.

1: Open the php. ini file.
Set include_path = "c :"

2: Write two files
Auto_prepend_file.php and auto_append_file.php are stored on drive C, and they are automatically attached to the header and tail of each PHP file.

3: in php. ini, find:
Automatically add files before or after any PHP document.
Auto_prepend_file = auto_prepend_file.php; attached to the header
Auto_append_file = auto_append_file.php;

In the future, each PHP file will be equivalent

PHP code :--------------------------------------------------------------------------------

Include "auto_prepend_file.php ";

... // Here is your program


Include "auto_append_file.php ";
?>

--------------------------------------------------------------------------------




13: How to Use PHP to upload files



PHP code :--------------------------------------------------------------------------------


Upload File Form





$ Upload_file = $ _ FILES ['upload _ file'] ['tmp _ name'];
$ Upload_file_name = $ _ FILES ['upload _ file'] ['name'];

If ($ upload_file ){
$ File_size_max = 1000*1000; // 1 MB limit the maximum file upload capacity (bytes)
$ Store_dir = "d:/"; // storage location of the uploaded file
$ Accept_overwrite = 1; // whether to overwrite the same file
// Check the file size
If ($ upload_file_size> $ file_size_max ){
Echo "sorry, your file capacity exceeds the limit ";
Exit;
}

// Check the read/write File
If (file_exists ($ store_dir. $ upload_file_name )&&! $ Accept_overwrite ){
Echo "files with the same file name exist ";
Exit;
}

// Copy the file to the specified directory
If (! Move_uploaded_file ($ upload_file, $ store_dir. $ upload_file_name )){
Echo "failed to copy the file ";
Exit;
}

}

Echo"

You uploaded the file :";
Echo $ _ FILES ['upload _ file'] ['name'];
Echo"
";
// The original name of the client machine file.

Echo "the MIME type of the file is :";
Echo $ _ FILES ['upload _ file'] ['type'];
// MIME type of the file, which must be supported by the browser, for example, "image/gif ".
Echo"
";

Echo "Upload file size :";
Echo $ _ FILES ['upload _ file'] ['SIZE'];
// Size of the uploaded file, in bytes.
Echo"
";

Echo "the file is temporarily stored after being uploaded :";
Echo $ _ FILES ['upload _ file'] ['tmp _ name'];
// Temporary file name stored on the server after the file is uploaded.
Echo"
";


$ Erroe = $ _ FILES ['upload _ file'] ['error'];
Switch ($ Erroe ){
Case 0:
Echo "uploaded successfully"; break;
Case 1:
Echo "the uploaded file exceeds the limit of the upload_max_filesize option in php. ini."; break;
Case 2:
Echo "the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. "; Break;
Case 3:
Echo "only part of the file is uploaded"; break;
Case 4:
Echo "no file is uploaded"; break;
}
?>

--------------------------------------------------------------------------------



14: how to configure the GD library


The following is my configuration process
1: Use the doscommand (you can also manually copy all dll files in the dlls folder to the system32 directory) copy c: \ php \ dlls \ *. dll c: \ windows \ system32 \
2: Open php. ini
Set extension_dir = "c:/php/extensions /";
3:
Extension = php_gd2.dll; remove the comma before extension. If php_gd2.dll is not available, the same applies to php_gd.dll. Ensure that the file c:/php/extensions/php_gd2.dll exists.
4: run the following program for testing.

PHP code :--------------------------------------------------------------------------------

Ob_end_flush ();
// Note: no information can be output to the browser before this time. Be sure to set auto_prepend_file.
Header ("Content-type: image/png ");
$ Im = @ imagecreate (200,100)
Or die ("Images cannot be created ");
$ Background_color = imagecolorallocate ($ im, 0, 0 );
$ Text_color = imagecolorallocate ($ im, 230,140,150 );
Imagestring ($ im, 3, 30, 50, "A Simple Text String", $ text_color );
Imagepng ($ im );
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.