intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Bài giảng Phát triển ứng dụng nguồn mở: Bài 2.3 - Đoàn Thiện Ngân

Chia sẻ: 5A4F5AFSDG 5A4F5AFSDG | Ngày: | Loại File: PDF | Số trang:65

50
lượt xem
5
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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.

Chủ đề:
Lưu

Nội dung Text: Bài giảng Phát triển ứng dụng nguồn mở: Bài 2.3 - Đoàn Thiện Ngân

  1. 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
  2. Nội dung • Arrays $_POST - $_GET • Cookies - Sessions • PHP Object oriented solutions. Đoàn Thiện Ngân Bài 2.3 - 2/65
  3. 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
  4. $_GET và $_POST • $_GET: array of variables passed to the current script via the URL parameters (the HTTP GET method).
  5. 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
  6. testpost.html (tt) Email Address: Response: This is... excellent okay boring Comments: Đoàn Thiện Ngân Bài 2.3 - 6/65
  7. 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
  8. 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
  9. 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
  10. Cookies between Server and Client Đoàn Thiện Ngân Bài 2.3 - 10/65
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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()+3600); Đoàn Thiện Ngân Bài 2.3 - 15/65
  16. Adding Parameters to a Cookie • The path and domain arguments are used to limit a cookie to a specific folder in a Web site (the path) or to a specific domain. • Using the path option, we could limit a cookie to exist only while a user is in a specific subfolder of the domain: setcookie(name,value,time()+3600,'/subfolder/'); • Cookies are already specific to a domain, so the domain argument might be used to limit a cookie to a subdomain, such as forum.example.com. setcookie(name, value, time()+3600, '', 'forum.example.com'); Đoàn Thiện Ngân Bài 2.3 - 16/65
  17. Adding Parameters to a Cookie • The secure value dictates that a cookie should only be sent over a secure HTTPS connection. A value of 1 indicates that a secure connection must be used, whereas 0 indicates that a secure connection isn’t necessary. • we could ensure a secure cookie transmission for e- commerce sites: setcookie('cart', '82ABC3012', . time()+3600, ', 'shop.example.com', 1); • We must pass all the values in order. If there’s no need to specify (or limit) the path, we use empty quotes. With the path argument, we can also use a single slash (/) to indicate the root folder (i.e., no path restriction). By doing so, we maintain the proper number of arguments and can still indicate that an HTTPS connection is necessary. Đoàn Thiện Ngân Bài 2.3 - 17/65
  18. Adding Parameters to a Cookie • The final argument—httponly—was added in PHP 5.2. It can be used to restrict access to the cookie (for example, preventing a cookie from being read using JavaScript) but isn’t supported by all browsers. • Let’s add an expiration date to the existing page so that the user’s preferences will remain even after they’ve closed their browser and then returned to the site later. setcookie(name, value, time()+10000000, '/', '', 0); setcookie(name, value, time()+10000000, '/', '', 0); Đoàn Thiện Ngân Bài 2.3 - 18/65
  19. Deleting a Cookie • Although a cookie automatically expires when the user’s browser is closed or when the expiration date/time is met, sometimes we’ll want to manually delete the cookie as well. • setcookie() can take up to seven arguments, but only one is required— the name. If we send a cookie that consists of a name without a value, it will have the same effect as deleting the existing cookie of the same name. • To delete the username cookie, we code setcookie('name', ''); or setcookie('name', FALSE); Đoàn Thiện Ngân Bài 2.3 - 19/65
  20. Deleting a Cookie • As an added precaution, we can also set an expiration date that’s in the past: setcookie('username', FALSE, time() - 600); • The only caveat when it comes to deleting a cookie is that we must use the same argument values that were used to set the cookie in the first place (aside from the value and expiration). For example, if we set a cookie while providing a domain value, we must also provide that value when deleting the cookie: setcookie('user','lary',time() + 3600,'','a.com'); setcookie('user', '', time() - 600, '', 'a.com'); Đoàn Thiện Ngân Bài 2.3 - 20/65
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
10=>1