Bài giảng Phát triển ứng dụng nguồn mở: Bài 2.3 - Đoàn Thiện Ngân
Số trang: 65
Loại file: pdf
Dung lượng: 135.20 KB
Lượt xem: 14
Lượt tải: 0
Xem trước 7 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Bài 2.3 cung cấp kiến thức về PHP Object Oriented Proramming. Trong chương này người học sẽ tìm hiểu những nội dung chính sau: Arrays $_POST - $_GET, Cookies - Sessions, PHP Object oriented solutions. Mời các bạn tham khảo để biết thêm các nội dung chi tiết.
Nội dung trích xuất từ tài liệu:
Bài giảng Phát triển ứng dụng nguồn mở: Bài 2.3 - Đoàn Thiện Ngân Bài 2.3: PHP Object Oriented Proramming GV: ĐOÀN THIỆN NGÂN Đoàn Thiện Ngân Bài 2.3 - 1/65 Nội dung • Arrays $_POST - $_GET • Cookies - Sessions • PHP Object oriented solutions. Đoàn Thiện Ngân Bài 2.3 - 2/65 Tài liệu tham khảo 1. Bắt buộc: PHP Manual. http://www.php.net/docs.php 2. PHP Objects, Patterns, and Practice, 3rd Edition, Matt Zandstra, Apress, 2010. 3. Learning PHP Design Patterns, William Sanders, O'REILLY, 2013 Đoàn Thiện Ngân Bài 2.3 - 3/65 $_GET và $_POST • $_GET: array of variables passed to the current script via the URL parameters (the HTTP GET method). testpost.html Feedback Form Please complete this form to submit our feedback: Test GET by Hyperlink Name: Mr. Mrs. Ms. Đoàn Thiện Ngân Bài 2.3 - 5/65 testpost.html (tt) Email Address: Response: This is... excellent okay boring Comments: Đoàn Thiện Ngân Bài 2.3 - 6/65 action.php if (count($_POST) > 0) { print array POST:; print_r($_POST); echo ,'...Display in details array $_POST'; foreach ($_POST as $key => $value) print ('' . $key . ' = ' . $value); } if (count($_GET) > 0) { print 'array GET:'; print_r($_GET); echo ,'...Display in details array $_GET'; foreach ($_GET as $key => $value) print ('' . $key . ' = ' . $value); } Đoàn Thiện Ngân Bài 2.3 - 7/65 Cookies và Sessions • How to maintain “state” as the user traverses a multipage Web site. • HTTP is a stateless technology, • HTTP has no built-in method for tracking a user or remembering data from one page of an application to the next. • E-commerce applications, user registration and login systems, and other common online services rely on being able to follow the same user from page to page. • Fortunately, maintaining state is quite simple with PHP. This part discusses the two primary methods for tracking data: cookies and sessions. Đoàn Thiện Ngân Bài 2.3 - 8/65 What Are Cookies • Cookies are simply a way for a server to store information on the user’s computer. By doing so, the server can remember the user over the course of a visit or through several visits. • Think of a cookie like a name tag: we tell the server our name, and it gives we a name tag. Then it can know who we are by referring back to the name tag. This brings up another point about the security issues involved with cookies. • Cookies have gotten a bad rap because users believe cookies allow a server to know too much about them. However, a cookie can only be used to store information that we give it, so it’s as secure as we want it to be. Đoàn Thiện Ngân Bài 2.3 - 9/65 Cookies between Server and Client Đoàn Thiện Ngân Bài 2.3 - 10/65 Creating Cookies • An important thing to understand about cookies is that they must be sent from the server to the client prior to any other information. • This means a script should send cookies before any print statement, before including an external file that contains HTML, and so forth. • Should the server attempt to send a cookie after the Web browser has already received HTML—even an extraneous white space—an error message will result and the cookie won’t be sent. This is by far the most common cookie-related error. • Cookies are sent using the setcookie() function: setcookie(name, value); setcookie('CookieName', 'This is the cookie value.'); Đoàn Thiện Ngân Bài 2.3 - 11/65 Creating Cookies • We can continue to send more cookies to the browser with subsequent uses of the setcookie() function setcookie('name2', 'some value'); setcookie('name3', 'another value'); • Finally, when creating cookies, we can— as we’ll see in this example—use a variable for the name or value attribute of our cookies: setcookie($cookie_name, $cookie_value); Đoàn Thiện Ngân Bài 2.3 - 12/65 Reading from Cookies • The setcookie() function places cookie data in the $_COOKIE array. • To retrieve a value from a cookie, refer to the cookie name as the index of this array. • For Ex, with the line setcookie('user', 'trout'); we would use $_COOKIE['user']. • Unless we change the cookie’s parameters (as we’ll see later in this chapter), the cookie will automatically be accessible to every other page in our Web application. • We should understand, however, that a cookie is never accessible to a script immediately after it’sĐoàn been sent. Thiện Ngân Bài 2.3 - 13/65 Adding Parameters to a Cookie • Although passing just the name and value arguments to the setcookie() function will suffice for most of our cookie uses, we ought to be aware of the other arguments available. • The function setcookie() can take up to five more parameters, each of which limits the operation of the cookie: setcookie(name, value, expiration, path, domain, secure, httponly); Đoàn Thiện Ngân Bài 2.3 - 14/65 Adding Parameters to a Cookie • The expiration argument is used to set a specific length of time for a cookie to exist. • If it isn’t specified, the cookie will continue to be functional until the user closes the browser. • Normally, we set the expiration time by adding a particular number of minutes or hours to the current time (using the time() function). This line of code sets the expiration time of the cookie to be one hour (3600 seconds) from the current moment: setcookie(name, value, time() ...
Nội dung trích xuất từ tài liệu:
Bài giảng Phát triển ứng dụng nguồn mở: Bài 2.3 - Đoàn Thiện Ngân Bài 2.3: PHP Object Oriented Proramming GV: ĐOÀN THIỆN NGÂN Đoàn Thiện Ngân Bài 2.3 - 1/65 Nội dung • Arrays $_POST - $_GET • Cookies - Sessions • PHP Object oriented solutions. Đoàn Thiện Ngân Bài 2.3 - 2/65 Tài liệu tham khảo 1. Bắt buộc: PHP Manual. http://www.php.net/docs.php 2. PHP Objects, Patterns, and Practice, 3rd Edition, Matt Zandstra, Apress, 2010. 3. Learning PHP Design Patterns, William Sanders, O'REILLY, 2013 Đoàn Thiện Ngân Bài 2.3 - 3/65 $_GET và $_POST • $_GET: array of variables passed to the current script via the URL parameters (the HTTP GET method). testpost.html Feedback Form Please complete this form to submit our feedback: Test GET by Hyperlink Name: Mr. Mrs. Ms. Đoàn Thiện Ngân Bài 2.3 - 5/65 testpost.html (tt) Email Address: Response: This is... excellent okay boring Comments: Đoàn Thiện Ngân Bài 2.3 - 6/65 action.php if (count($_POST) > 0) { print array POST:; print_r($_POST); echo ,'...Display in details array $_POST'; foreach ($_POST as $key => $value) print ('' . $key . ' = ' . $value); } if (count($_GET) > 0) { print 'array GET:'; print_r($_GET); echo ,'...Display in details array $_GET'; foreach ($_GET as $key => $value) print ('' . $key . ' = ' . $value); } Đoàn Thiện Ngân Bài 2.3 - 7/65 Cookies và Sessions • How to maintain “state” as the user traverses a multipage Web site. • HTTP is a stateless technology, • HTTP has no built-in method for tracking a user or remembering data from one page of an application to the next. • E-commerce applications, user registration and login systems, and other common online services rely on being able to follow the same user from page to page. • Fortunately, maintaining state is quite simple with PHP. This part discusses the two primary methods for tracking data: cookies and sessions. Đoàn Thiện Ngân Bài 2.3 - 8/65 What Are Cookies • Cookies are simply a way for a server to store information on the user’s computer. By doing so, the server can remember the user over the course of a visit or through several visits. • Think of a cookie like a name tag: we tell the server our name, and it gives we a name tag. Then it can know who we are by referring back to the name tag. This brings up another point about the security issues involved with cookies. • Cookies have gotten a bad rap because users believe cookies allow a server to know too much about them. However, a cookie can only be used to store information that we give it, so it’s as secure as we want it to be. Đoàn Thiện Ngân Bài 2.3 - 9/65 Cookies between Server and Client Đoàn Thiện Ngân Bài 2.3 - 10/65 Creating Cookies • An important thing to understand about cookies is that they must be sent from the server to the client prior to any other information. • This means a script should send cookies before any print statement, before including an external file that contains HTML, and so forth. • Should the server attempt to send a cookie after the Web browser has already received HTML—even an extraneous white space—an error message will result and the cookie won’t be sent. This is by far the most common cookie-related error. • Cookies are sent using the setcookie() function: setcookie(name, value); setcookie('CookieName', 'This is the cookie value.'); Đoàn Thiện Ngân Bài 2.3 - 11/65 Creating Cookies • We can continue to send more cookies to the browser with subsequent uses of the setcookie() function setcookie('name2', 'some value'); setcookie('name3', 'another value'); • Finally, when creating cookies, we can— as we’ll see in this example—use a variable for the name or value attribute of our cookies: setcookie($cookie_name, $cookie_value); Đoàn Thiện Ngân Bài 2.3 - 12/65 Reading from Cookies • The setcookie() function places cookie data in the $_COOKIE array. • To retrieve a value from a cookie, refer to the cookie name as the index of this array. • For Ex, with the line setcookie('user', 'trout'); we would use $_COOKIE['user']. • Unless we change the cookie’s parameters (as we’ll see later in this chapter), the cookie will automatically be accessible to every other page in our Web application. • We should understand, however, that a cookie is never accessible to a script immediately after it’sĐoàn been sent. Thiện Ngân Bài 2.3 - 13/65 Adding Parameters to a Cookie • Although passing just the name and value arguments to the setcookie() function will suffice for most of our cookie uses, we ought to be aware of the other arguments available. • The function setcookie() can take up to five more parameters, each of which limits the operation of the cookie: setcookie(name, value, expiration, path, domain, secure, httponly); Đoàn Thiện Ngân Bài 2.3 - 14/65 Adding Parameters to a Cookie • The expiration argument is used to set a specific length of time for a cookie to exist. • If it isn’t specified, the cookie will continue to be functional until the user closes the browser. • Normally, we set the expiration time by adding a particular number of minutes or hours to the current time (using the time() function). This line of code sets the expiration time of the cookie to be one hour (3600 seconds) from the current moment: setcookie(name, value, time() ...
Tìm kiếm theo từ khóa liên quan:
Phát triển ứng dụng nguồn mở Bài giảng Phát triển ứng dụng nguồn mở Mã nguồn mở Object Oriented Proramming PHP Object Oriented Proramming PHP Object oriented solutionsGợi ý tài liệu liên quan:
-
Đề tài nguyên lý hệ điều hành: Nghiên cứu tìm hiểu về bộ nhớ ngoài trong hệ điều hành Linux
19 trang 223 0 0 -
Xây dựng công cụ nhận dạng khuôn mặt theo thời gian thực hiện trên nền hệ điều hành mã nguồn mỡ
7 trang 207 0 0 -
Câu hỏi ôn tập trắc nghiệm Hệ điều hành Linux
15 trang 65 0 0 -
Bài giảng Mã nguồn mở: Bài 3 - ThS. Phan Thanh Toàn
29 trang 58 0 0 -
Bài giảng Mã nguồn mở: Bài 1 - ThS. Phan Thanh Toàn
25 trang 56 0 0 -
Xây dựng SLD của dữ liệu không gian cho webGIS mã nguồn mở bằng CSS trong GeoServer
6 trang 41 0 0 -
Giới thiệu về Zabbix, hệ thống giám sát thường xuyên tài nguyên của máy chủ
7 trang 35 0 0 -
11 trang 31 0 0
-
90 trang 30 0 0
-
66 trang 29 0 0