15.3 AJAX 633 Making Asynchronous Requests 636 Complete Control Over AJAX 642 Cross-Origin Resource Sharing (CORS) 643
Total Page:16
File Type:pdf, Size:1020Kb
Visit : https://hemanthrajhemu.github.io Join Telegram to get Instant Updates: https://bit.ly/VTU_TELEGRAM Contact: MAIL: [email protected] INSTAGRAM: www.instagram.com/hemanthraj_hemu/ INSTAGRAM: www.instagram.com/futurevisionbie/ WHATSAPP SHARE: https://bit.ly/FVBIESHARE xxii TABLE OF CONTENTS 12.2 PHP Error Reporting 506 The error_reporting Setting 507 The display_errors Setting 507 The log_error Setting 508 12.3 PHP Error and Exception Handling 509 Procedural Error Handling 509 Object-Oriented Exception Handling 509 Custom Error and Exception Handlers 512 12.4 Regular Expressions 513 Regular Expression Syntax 513 Extended Example 516 12.5 Validating User Input 519 Types of Input Validation 519 Notifying the User 520 How to Reduce Validation Errors 521 12.6 Where to Perform Validation 524 Validation at the JavaScript Level 528 Validation at the PHP Level 531 12.7 Chapter Summary 536 Key Terms 536 Review Questions 537 Hands-On Practice 537 References 540 Chapter 13 Managing State 541 13.1 The Problem of State in Web Applications 542 13.2 Passing Information via Query Strings 544 13.3 Passing Information via the URL Path 546 URL Rewriting in Apache and Linux 546 13.4 Cookies 547 How Do Cookies Work? 548 https://hemanthrajhemu.github.io TABLE OF CONTENTS xxiii Using Cookies 550 Persistent Cookie Best Practices 550 13.5 Serialization 552 Application of Serialization 554 13.6 Session State 554 How Does Session State Work? 557 Session Storage and Configuration 558 13.7 HTML5 Web Storage 561 Using Web Storage 561 Why Would We Use Web Storage? 563 13.8 Caching 563 Page Output Caching 565 Application Data Caching 565 13.9 Chapter Summary 567 Key Terms 567 Review Questions 568 Hands-On Practice 568 References 572 Chapter 14 Web Application Design 573 14.1 Real-World Web Software Design 574 Challenges in Designing Web Applications 574 14.2 Principle of Layering 575 What Is a Layer? 575 Consequences of Layering 577 Common Layering Schemes 579 14.3 Software Design Patterns in the Web Context 585 Adapter Pattern 585 Simple Factory Pattern 589 Template Method Pattern 591 Dependency Injection 594 https://hemanthrajhemu.github.io xxiv TABLE OF CONTENTS 14.4 Data and Domain Patterns 595 Table Data Gateway Pattern 596 Domain Model Pattern 597 Active Record Pattern 601 14.5 Presentation Patterns 604 Model-View-Controller (MVC) Pattern 604 Front Controller Pattern 607 14.6 Chapter Summary 608 Key Terms 608 Review Questions 608 Hands-On Practice 609 References 610 Chapter 15 Advanced JavaScript & jQuery 613 15.1 JavaScript Pseudo-Classes 614 Using Object Literals 614 Emulate Classes through Functions 615 Using Prototypes 617 15.2 jQuery Foundations 619 Including jQuery in Your Page 620 jQuery Selectors 621 jQuery Attributes 624 jQuery Listeners 628 Modifying the DOM 629 15.3 AJAX 633 Making Asynchronous Requests 636 Complete Control over AJAX 642 Cross-Origin Resource Sharing (CORS) 643 15.4 Asynchronous File Transmission 644 Old iframe Workarounds 645 https://hemanthrajhemu.github.io TABLE OF CONTENTS xxv The FormData Interface 646 Appending Files to a POST 648 15.5 Animation 649 Animation Shortcuts 649 Raw Animation 651 15.6 Backbone MVC Frameworks 654 Getting Started with Backbone.js 655 Backbone Models 655 Collections 657 Views 657 15.7 Chapter Summary 660 Key Terms 660 Review Questions 660 Hands-On Practice 661 References 664 Chapter 16 Security 665 16.1 Security Principles 666 Information Security 666 Risk Assessment and Management 667 Security Policy 670 Business Continuity 670 Secure by Design 673 Social Engineering 675 16.2 Authentication 676 Authentication Factors 676 Single-Factor Authentication 677 Multifactor Authentication 677 Third-Party Authentication 678 Authorization 681 https://hemanthrajhemu.github.io xxvi TABLE OF CONTENTS 16.3 Cryptography 681 Substitution Ciphers 683 Public Key Cryptography 686 Digital Signatures 689 16.4 Hypertext Transfer Protocol Secure (HTTPS) 690 Secure Handshakes 690 Certificates and Authorities 691 16.5 Security Best Practices 694 Data Storage 694 Monitor Your Systems 698 Audit and Attack Thyself 700 16.6 Common Threat Vectors 701 SQL Injection 701 Cross-Site Scripting (XSS) 703 Insecure Direct Object Reference 707 Denial of Service 708 Security Misconfiguration 709 16.7 Chapter Summary 712 Key Terms 713 Review Questions 713 Hands-On Practice 714 References 716 Chapter 17 XML Processing and Web Services 718 17.1 XML Overview 719 Well-Formed XML 719 Valid XML 720 XSLT 723 XPath 725 17.2 XML Processing 727 XML Processing in JavaScript 727 XML Processing in PHP 729 https://hemanthrajhemu.github.io TABLE OF CONTENTS xxvii 17.3 JSON 734 Using JSON in JavaScript 734 Using JSON in PHP 736 17.4 Overview of Web Services 737 SOAP Services 738 REST Services 740 An Example Web Service 740 Identifying and Authenticating Service Requests 744 17.5 Consuming Web Services in PHP 745 Consuming an XML Web Service 746 Consuming a JSON Web Service 750 17.6 Creating Web Services 756 Creating an XML Web Service 757 Creating a JSON Web Service 764 17.7 Interacting Asynchronously with Web Services 767 Consuming Your Own Service 768 Using Google Maps 769 17.8 Chapter Summary 774 Key Terms 775 Review Questions 775 Hands-On Practice 775 References 780 Chapter 18 Content Management Systems 781 18.1 Managing Websites 782 Components of a Managed Website 782 18.2 Content Management Systems 784 Types of CMS 785 18.3 CMS Components 787 Post and Page Management 787 https://hemanthrajhemu.github.io Managing State 13 CHAPTER OBJEcTIVES In this chapter you will learn . ■ Why state is a problem in web application development ■ What cookies are and how to use them ■ What HTML5 web storage is and how to use it ■ What session state is and what are its typical uses and limitations ■ What server cache is and why it is important in real-world websites his chapter examines one of the most important questions in T the web development world, namely, how does one page pass information to another page? This question is sometimes also referred to as the problem of state management in web applications. State management is essential to any web application because every web application has information that needs to be preserved from request to request. This chapter begins by examining the problem of state in web applications and the solutions that are available in HTTP. It then examines the state management features that are available in PHP. 541 https://hemanthrajhemu.github.io 542 CHAPTER 13 Managing State 13.1 The Problem of State in Web Applications Much of the programming in the previous several chapters has analogies to most typical nonweb application programming. Almost all applications need to process user inputs, output information, and read and write from databases or other storage media. But in this chapter we will be examining a development problem that is unique to the world of web development: how can one request share information with another request? At first glance this problem does not seem especially formidable. Single-user desk- top applications do not have this challenge at all because the program information for the user is stored in memory (or in external storage) and can thus be easily accessed throughout the application. Yet one must always remember that web applications differ from desktop applications in a fundamental way. Unlike the unified single pro- cess that is the typical desktop application, a web application consists of a series of disconnected HTTP requests to a web server where each request for a server page is essentially a request to run a separate program, as shown in Figure 13.1. Desktop application Open Save Desktop application process Desktop memory Web server Browser application save.php Open Save Save page process open.php Other processes Web server memory Open page process FiGURE 13.1 Desktop applications versus web applications https://hemanthrajhemu.github.io 13.1 The Problem of State in Web Applications 543 Furthermore, the web server sees only requests. The HTTP protocol does not, without programming intervention, distinguish two requests by one source from two requests from two different sources, as shown in Figure 13.2. User X 1 GET index.php Web 2 GET product.php server … is for the server not really any different than ... User X 1 GET index.php Web server 2 GET product.php User Y FiGURE 13.2 What the web server sees https://hemanthrajhemu.github.io 544 CHAPTER 13 Managing State User X 1 Add product to shopping cart 2 Go to check out and pay for item Web in cart server FiGURE 13.3 What the user wants the server to see While the HTTP protocol disconnects the user’s identity from his or her requests, there are many occasions when we want the web server to connect requests together. Consider the scenario of a web shopping cart, as shown in Figure 13.3. In such a case, the user (and the website owner) most certainly wants the server to recognize that the request to add an item to the cart and the subse- quent request to check out and pay for the item in the cart are connected to the same individual. The rest of this chapter will explain how web programmers and web develop- ment environments work together through the constraints of HTTP to solve this particular problem. As we will see, there is no single “perfect” solution, but a vari- ety of different ones each with their own unique strengths and weaknesses. The starting point will be to examine the somewhat simpler problem of how does one web page pass information to another page? That is, what mechanisms are available within HTTP to pass information to the server in our requests? As we have already seen in Chapters 1, 4, and 9, what we can do to pass information is con- strained by the basic request-response interaction of the HTTP protocol.