In this chapter, why does HTTP spread continuously? The GET parameter is another method for processing Form variables using GET-style URLs. This Chapter briefly describes some content about transferring data between Web pages. Such information transmission between webpages
Highlights of this Chapter
Why does HTTP spread continuously?
GET parameters
Another URL that uses the GET Style
Process form variables
PHP Super array
This chapter briefly describes how to transfer data between Web pages. Such information is not unique to PHP, but is an important part of PHH/HTML or HTTP.
HTTP has no status Record (stateless)
The most important thing to remember about Web services is that HTTP itself has no stateless ). If the reader has a poetic soul, it may say that each HTTP request is a perfect combination and has no home, just like a completely unknown ...... You know that. For those who lack poetry, the white point means that each HTTP request (each request and transfer page) is independent of all other content, does not know the client identity, and has no memory. Each request generates an independent process to complete a file service, seemingly tiny but important task, and then automatically disappears (this call is heartless, it can be said "back to the processable state 」).
Even if you design a website into a very non-strict one-way navigation (page 1 leads to page 2, page 2 leads to 3, and so on ), PTTP assistance never knows and does not care whether page 2 browsed by a person is from page 1. Therefore, you cannot set the variables on page 1 to be imported to the page through HTML. You can use HTML to display the form and enter some information in the form. However, once the information is transferred to another page or program in some other ways, the variable disappears.
This is why the form processing technology like PHP is imported. PHP can retrieve variables that are transferred from one page to another for further use. The PHP function is very good at this type of data transfer function, so that you can complete the tasks of various Web sites more quickly and easily.
HTML forms are the most useful way for websites to transmit data from a webpage. There are many more persistent ways to maintain the status methods across many webpages, such as cookies and sessions, we will introduce these features in chapter 27th. This chapter focuses on the more basic techniques used to transmit information between webpages, namely, using HTTP, GET, and POST methods to dynamically generate webpages and process data.
ASP programmers may say "PHP is really bad !」 They make the session variables for ASP amazing. This is not to cut off the bubble, but Microsoft is planning to use cookies to store session variables, but this opens the door to all potential problems.
GET parameters
The GET method regards parameters as URI [Uniform Resource Indicator, consistent Resource Indicator; some people prefer to use URI (Uniform Resource Indicator, consistent Resource locator)] to query a part of a string, transfer from one page to another. When used for form processing, GET uses the question mark (?) As a separator, append the variable name and value to the specified URL in the ACTION attribute and submit all content to the Web server for processing ).
This is an example of using the HTML window of getask(put the file in team_select.html ):
<HTML>
<HEAD>
<TITLE> a get example, part 1 </TITLE>
</HEAD>
<BODY>
<Fopm action = http: // localhost/baseball. php METHOD = "GET">
<P> Root, root, root for the: <BR>
<Select name = "Team" SIZE = 2>
When the user selects and presses the Submit button, the browser joins and joins these elements in the following order without spaces in the middle:
URL enclosed in quotation marks (http: // localhost/baseball. php) after a single word ACTION)
Question mark (?) Indicates the following characters to form a GET string
NAME variable, equal sign, and combined VALUE (Team = Cubbies)
The "&" symbol and the next pair "NAME = VALUE" (Submit = Submit); as long as the length limit of the server query string is allowed, the use & separated name-value combinations can be repeated many times.
This will constitute a URL string:
(Http: // locahost/baseball. php? Team = Cubbies & Submit = Select)
The string becomes the address space of the new request sent to the browser. After the preceding form is submitted, the PHP script (baseball. php) GET the GET variables from the end of the request string and perform corresponding operations on these variables. in the following example, insert one of the two values into the text string.
The following code is used by PHP to process the form processing part of the original HTML form:
<HTML>
<HEAD>
<TITLE> a get example, part 2 </TITLE>
<Style type = "text/css">
<! --
BODY {font-size: 24pt ;}
-->
</STYLE>
</HEAD>
<BODY>
<P> Go,
<? Php echo $ _ GET ['team'];? >
!
</BODY>
</HTML>
At last, you should see Go, Cubbies with the big words on the webpage.
The GET method in form processing has many advantages over the POST method: it creates a truly new and completely different URL query string. In this way, the user can mark this page as a bookmark (this can boost morale when the development team is down ). The results obtained from the form using the POST method cannot be recorded as bookmarks.
However, if you can use the GET parameter to complete the desired function does not mean you should use it. for most form processing programs, the shortcomings of the GET method are too serious, so that the original official HTML 4.0 specification does not support using it. These disadvantages include:
GET is not suitable for login, because it is completely visible on the screen when the user name and password are used as the hidden surface of the Accessed surface and stored in the memory of the client browser.
Each GET submission is recorded in the Web server log, and the data set is also included.
Because GET will allocate data to the server environment variable, the URL length is limited. You can imagine what a URL with a very long GET time looks like, but in fact no one wants to use this method to try to pass an article in HTML format of three hundred words.
At the beginning of the official HTML specification, the length of the query string is limited to 255 characters. Although the restriction on the length of the query string is relaxed later, the use of a long string is really a self-seeking headache.
After a lot of arguments, W3 still resumes using the GET method in form processing, mainly because of the bookmarking function. Although the GET method is still a preset selection item for form processing, we recommend that you only use the GET method where there are no sub-bands. Consider two advantages and two disadvantages. the most suitable use of GET to process forms is actually "search box )」. The GET method is used in a non-searchable form handler unless it is mandatory. Otherwise, you can use the POST method instead.
A better GET style URL usage
The GET method for form processing is not recommended. However, the URL style related to it is very useful for website navigation, especially for websites with dynamic advertisements. for example, websites that are often constructed using PHP are appended with a variable format URL, it is very suitable for the content development system based on the sample version.
As shown in the following example, assume that you are running a Web site with rich information about solar vehicles. you have saved the lengthy and consistent format of information in the following pages:
Suspension_design.html
Windtunnel_testing.html
Friction_braking.html
However, when the size of a website increases, it takes a lot of time to manage the structure of such a simple Archive website, because some trivial changes must be repeated on each page. If the structure of these pages is very simple, you can use PHP to convert the website to a sample-based system.
You may decide to use a single sample to differentiate the text files of each topic (including information, photos, comments, and so on ):
Topic. php
Suspension_design. inc
Windtunnel_testing. inc
Friction_braking. inc
Or you may decide to process a larger and more special sample file:
Vehicle_structure. php
Tubular_frames. inc
Machical_systems. php
Friction_braking. inc
Electrical_systems. php
Solar_array. inc
Racing. php
Race _ strategy. inc
A simple sample may be shown in this example (because we do not include the. inc text file we need, this file will not actually work ):
<HTML>
<HEAD>
<TITLE> Solar-car topics </TITLE>
<Style type = "text/css">
<! -
BODY {font: verdana; font-size: 12pt}
-->
</STYLE>
</HEAD>
<BODY>
<Table border = 0 CELLPADDING = 0 WIDTH = 100%>
<YR>
<! -Navbar, with Get-style URLs. -->
<Td bgcolor = "#4282B4" ALIGN = center valign = top width = 25%>
<P>
<A href = "machical_syatems. php? Name = friction_braking "> <B> Friction braking </B> </A>
<BR>
<A href = "machical_syatems.php? Name = steering "> <B> Steering </B> </A>
<BR>
<A href = "machical_systems. php? Name = Suspenion "> <B> Suspenion </B>
</A>
<BR>
<A href = "machical_systems. php? Name = tires "> <B> Tires and wheels </B>
</A>
<BR>
</P>
</TD>
<! -Main body of content->
<Td bgcolor = "# FFFFFF" ALIGN = left valign = top width = 75%>
<? Php include ("$ _ GET ['name']. inc ")? >
</TD> </TE> </TABLE>
</BODY>
<HTML>
Note that when you click it, the connection in the navigation column will be processed by the browser, just like submitting a GET process.
However, for this solution, you must manually change the code to make sure that each included file is in the correct HTML format, add a new connection to the navigation column and other similar content each time you add a new page to the website. Separate the form and content as usual. you can choose to use the database. If you use a database, the URL is similar to the following:
(Http: // localhost/topic. php? TopicID = 2)
It will point to a PHP sample version that handles database calls (using numeric variables instead of single words can query the database more quickly ). When a new topic is added to the database, the system adds links to the navigation column, so you do not need to manually generate all Web pages (here the word "all" is somewhat exaggerated, but it does save manpower and time ).
POST parameters
POST is a relatively good form processing method. it is especially suitable for situations where it is not used all at once (specifying some materials or functions that can be used together for a long time), for example, adding information to the database. When form data is transmitted to the processing program (PHP here), it is contained in the form body. The submitted materials are different and cannot be changed in the URL.
The POST method has the following advantages:
◎ It is safer than GET because it is in the URL query string, server log, or on the screen (if preventive measures are taken, for example, the password field is always expressed using the HTML password input format.
◎ The limit on the number of data that can be passed is looser (up to two thousand tuples, not more than two hundred characters ).
However, POST also has some disadvantages:
◎ The results cannot be marked as bookmarks.
◎ This method is incompatible with some firewall settings. for security considerations, the firewall will remove some form information.
In this book, we use the POST method to process forms, especially when using the SQL syntax to write files or INSERT data to the system. We use the GET method only when browsing and searching the website. In other words, we use the time to write data to the data storage location and display the webpage, all other forms in this chapter use the POST method.
Use both GET and POST methods
Do you know? PHP allows you to use GET and POST variables on the same page at the same time, so you can freely write dynamic forms!
But this immediately raises a problem: what if the same variable name is used in the GET and POST arrays (intentionally or for other reasons? If you set your php. if the register_globals command in the ini file is set to on, PHP stores ENVIRONMENT, GET, POST, COOLIE and SERVER variables in the $ GLOBAL Array. if there is a conflict, it will re-adjust the variable content according to the sequence you set. set the variable _ order option in ini. The latter will replace the former, so if you use the preset "EGPCS" value, POST will replace GET, and COOKIE will replace POST, you can adjust the alphabetic order in the file to control the replacement sequence, or even disable register_globals and use PHP's new ultra-Global Array. we will introduce it in the following sections.
Variable processing in PHP
PHP is very efficient in transferring data because developers decide to use a very convenient but (theoretically) somewhat complex design. When you use the GET or POST method to submit a dataset, PHP automatically determines the variable rows on the new page in an invisible way. Most other programming languages allow programmers to execute explicit and specific processing on their own pages. if they forget to specify or write an error, the information cannot be transmitted to the processing agent. In contrast, PHP is faster, simpler, and more user-friendly.
However, because such automatic variables are specified, you must always set a good NAME attribute for each INPUT control item. In fact, the NAME attribute is not mandatory for HTML, even if your form is missing, it can still work normally, but the data will have no function, because the NAME field attribute of these HTML forms will be the variable NAME of the form handler.
In other words, the following form:
<Form action = "<? Php echo $ _ SERVER ['php _ SELF '];? > "METHOD =" POST ">
<Input type = "text" NAME = "email">
<Input type = "submit" NAME = "Submit" VALUE = "Send">
</FORM>
The email text field will make the PHP variable $ _ OPET ['email '] when appropriate for table transfer (or if you use the old array variable $ HTTP_POST_VARS ['email'], even if you start register_globals with $ email), the transfer button will generate a variable $ _ POST ['submit '] for the next webpage, the name you use in the HTML form will become the variable form field for PHP form processing.
Another production and production HTML form must remember that if you want the form to display the initial words before entering, you must set the VALUE attribute, which is particularly useful in two types of forms: the form used to input data to the database and to transmit the data more than once. The latter often appears when the form needs to be re-displayed when an error occurs, for example, a form used for login will not be sent until a valid email address or other related information is entered.
For example, the following form (used as a pension trial form) is designed to be transmitted multiple times when a user fills in the information, the item you entered previously is automatically filled in. note the VALUE attribute in the form field of the following sample program.
<HTML>
<HEAD>
<TITLE> a post example: retirement savings worksheet </TIELE>
<Style type = custom text.css ">
<! --
BODY {font-size: 14pt}
. Heading {font-size: 18pt; color: red}
-->
</STYLE>
</HEAD>
<? Php
// This test, along with the Submit button value in the form below,
// Will check to see if the form is being rendered for the first time
// (In which case it will display with only the default annual gain
// Filled in)
If (! IsSet ($ _ POST ['Submit?]) | $ _ POST ['Submit?]! = 'Calculate ?) {
$ _ POST ['currentage'] = "";
$ _ POST ['retidage'] = "";
$ _ POST ['trigger'] = "";
$ Total = 7;
} Else {
$ AnnGain =$ _ POST ['anngain'];
$ Years = $ _ POST ['retireage']-$ _ POST ['currentage'];
$ YearCount = 0;
$ Total = $ _ POST ['countrib'];
While ($ YearCount <= $ Years ){
$ Total = round ($ Total * (1.0 + $ AnnGain/100) + $ _ POST ['trigger']);
$ YearCount = $ YearCount + 1;
}
}
? >
<BODY>
<Biv align = center id = Divl class = heading> A retirement-savings calculator
</DIV>
<P class = blurb> Fill in all the values (could t "Nest Egg") and see how much money you'll have for your retirement under different scenarios. you can change the values and resubmit the form as your times as you like. you must fill in the two "Age" variables. the "Annual return" variable has a default inflation-adjusted value (7% = 8% growth minus 1% inflation) which you can change to reflect your greater optimism or pessimism. </P>
<Form actiin = "" METHOD = "POST">
<P> Your age now: <input type = "text" SIZE = 5 NAME = "CurrentAge" VALUE = "">
<P> The age at which you plan to retire: <input type = "text" SIZE = 6
NAME = "RetireAge" VALUE = "<? Php echo $ _ POST ['retireage'];? > ">
<P> Annual contribution: <input type = "text" SIZE = 15 NAME = "Contrib" VALUE = "<? Php echo $ _ POST [? Contrib?]; ? > ">
<P> Annual return: <input type = "text" SIZE = 15 NAME = "AnnGain" VALUE = "<? Php echo $ annGain ;? > "> %
<BR>
<P> <B> nest egg </B>: <? Php ECHO $ Total ;? >
<P> <input type = "submit" NAME = "Submit" VALUE = "Calculate">
</FORM>
</BODY>
</HTML>
Figure 9-1 shows the results of the above program.
When you strengthen the form, the form handler should appear before the form is displayed. some people may make it wrong to design the form before the processing program, however, if you follow this method, you will understand the logic. you must first obtain the variable name and make a selection before displaying the form, this is more important if you have to direct users to different webpages in some situations by using the header () function, this decision must be made before any HTML output is displayed in the browser.
The above is the content in chapter 9 of the PHP Learning Guide. For more information, see The PHP Chinese website (www.php1.cn )!