Danh mục

PHP: The Good Parts: Delivering the Best of PHP- P3

Số trang: 20      Loại file: pdf      Dung lượng: 454.67 KB      Lượt xem: 14      Lượt tải: 0    
Hoai.2512

Hỗ trợ phí lưu trữ khi tải xuống: 15,000 VND Tải xuống file đầy đủ (20 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

PHP: The Good Parts: Delivering the Best of PHP- P3: Vượt qua tất cả các hype về PHP và thâm nhập vào sức mạnh thực sự của ngôn ngữ này. Cuốn sách này khám phá những tính năng hữu ích nhất của PHP và làm thế nào họ có thể đẩy nhanh quá trình phát triển web, và giải thích lý do tại sao thường được sử dụng PHP yếu tố thường được sử dụng sai hoặc misapplied. Bạn sẽ tìm hiểu thêm phần nào sức mạnh để lập trình hướng đối tượng, và làm thế nào để sử dụng...
Nội dung trích xuất từ tài liệu:
PHP: The Good Parts: Delivering the Best of PHP- P3GET method (which still uses the URL as its vehicle). Any information that is sent alongthe query string in a key/value pair is subsequently loaded into the $_GET array in thecalled page. http://www.mynewwebsite.com/access.php?login=1&logname=“Fred”This URL example has two keys in the query string, one called login and the othercalled logname. When the access.php file is called, you can manipulate these values, ifyou so desire. All you have to do is reference the key within the associative array andthen it’s yours. Consider this code as part of the access.php file. $login = $_GET[login] ; $logname = $_GET[logname] ; if ($login == 1) { echo Welcome . $logname ; } else { echo login failed... please try again . $logname ; }The advantage to using the $_GET superglobal is that you can access information thatis established on one page and use it in a called file. It’s important to understand that the $_GET array is refreshed on each page call, so you have to pass it on to each page being called further down the call stack. It’s different than the session concept in this regard.$_POSTThe $_POST superglobal array is almost identical to the $_GET array in that it can passvalues effectively from one page to the next; the difference lies in the method of passingthe information. The $_POST array does not use the query string in the URL of the calledfile as the transport method, but instead uses the server’s Hypertext Transfer Protocol(HTTP) POST method. This is seen most often in submitted forms on web pages, butit can be used independently of the HTML tag. Also, since it uses the POSTmethod on the server and information is not sent as part of the URL, the informationis less visible to the web user. So there is another modicum of security, even if it’s notfoolproof. The following code will use the tag in a small web page and then showyou how to manage the $_POST array in the called PHP file. please enter your name: Integration with Web Pages | 23When the user clicks the submit button, page2.php is called. The code for this filefollows: back$_REQUESTThe final superglobal array that we will look at in this chapter is known as theREQUEST array. This is an all-inclusive array for each of the other requesting types ofarrays, namely, $_COOKIE, $_GET, and $_POST. The drawback here is that each named keyshould be unique, otherwise $_REQUEST[lname] could draw from any of the variousarrays. Take a look at this code as an example: please enter your last name: This code sets a cookie, submits a form via the POST method, and when the form isposted, the code sends some values along the URL via the GET method. Now, chancesare that you will not have code this diverse, but it is being shown here to demonstratea possible stumbling block when using the REQUEST array. The tag is callingpage2.php as its action destination. Here is the code for that file: backWhen this page is displayed, the following text is shown, assuming the user entered“MacIntyre” for the lname form input field.24 | Chapter 2: Casing the Joint the full name from GET: peter smith the full name from POST: Simon MacIntyre the Request array -> array(3) { [fname]=> string(5) “Simon” [lname]=> string(9) “MacIntyre” [mname]=> string(4) “Beck” } backAs you can see, even though we have set the values in the GET and POST arrays dif-ferently, we have named the keys identically, so the REQUEST array by default givesprecedence to the POST array. Also notice in this code that we didn’t have to actuallyretrieve the cookie value from the first code listing—it is automatically forwarded tothe REQUEST array when a subsequent file is called. You can control the overall environment of superglobal arrays with the php.ini directive known as variables_order. The setting on my server is GPC. This means that the arrays are loaded and created in GET, POST, and COOKIE order, with the latter elements taking precedence over the former if they are similarly named. The “G” stands for GET, the “P” for POST, and the “C” is for cookie. If you remove one of the letters in GPC, save the .ini file and restart your server. The array rep- resented by that letter will not be created in the superglobal space on the web server. Integration with Web Pages | 25 CHAPTER 3 Functions (Doing It Once)PHP uses functions, much like any other programming language, and it certainly is toyour advantage to get to know how to make the most of them.PHP defines functions in two ways: those that return a value and those that do not.Functions should stand alone from other segments of code as much as possible. Therules for defining a function are fairly simple; you designate a function using its reservedword, giving it a unique name beginning with a letter or an underscore character, fol-lowed by any number of letters, underscores, or numbers. Round brackets (()) followthe function name—these are used to pass in any parameters that govern the function(more on that later). Finally, use curly braces ({}) to surround any code that is to becontained within the function.Here is a sample function: function MyFunction ( ) { echo This is being displayed because MyFunction has ...

Tài liệu được xem nhiều: