Php experts-FAQs

Source: Internet
Author: User

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:

<? PHP
Ob_Start ();
Session_Start ();
Echo "<pre> ";

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 "</pre> ";
?>

Then in php. set in ini: Export de_path = "c:/php" and debug. php is placed in this folder, and you can include this file in each web page 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:

<? Php
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:

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

You can cancel the session as follows:

This topic consists of five pages, currently on page 1 2 3 4 5


PHP code:

<? Php
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:

Anything that appears "........ headers already sent .......... ", that is, 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 <? PHP Ob_Start ();?>
... Here is your program ......

2: What is the error?

Warning: session_start (): open (/tmpsess_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:

<? Php
$ 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:

<? PHP
Echo "<pre> ";
Echo $ _ GET ["Name"];
Echo "</pre> ";
?>

The correct method is:

PHP code:

<? Php
$ 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.

This topic consists of five pages, currently on page 1 2 3 4 5


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:

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.