PHP Programming Cookbook I
Total Page:16
File Type:pdf, Size:1020Kb
PHP Programming Cookbook i PHP Programming Cookbook PHP Programming Cookbook ii Contents 1 PHP Tutorial for Beginners 1 1.1 Introduction......................................................1 1.1.1 Where is PHP used?.............................................1 1.1.2 Why PHP?..................................................2 1.2 XAMPP Setup....................................................3 1.3 PHP Language Basics.................................................5 1.3.1 Escaping to PHP...............................................5 1.3.2 Commenting PHP..............................................5 1.3.3 Hello World..................................................6 1.3.4 Variables in PHP...............................................6 1.3.5 Conditional Statements in PHP........................................7 1.3.6 Loops in PHP.................................................8 1.4 PHP Arrays...................................................... 10 1.5 PHP Functions.................................................... 12 1.6 Connecting to a Database............................................... 14 1.6.1 Connecting to MySQL Databases...................................... 14 1.6.2 Connecting to MySQLi Databases (Procedurial).............................. 14 1.6.3 Connecting to MySQLi databases (Object-Oriented)............................ 15 1.6.4 Connecting to PDO Databases........................................ 15 1.7 PHP Form Handling................................................. 15 1.8 PHP Include & Require Statements.......................................... 17 1.9 Object Oriented Concepts............................................... 19 1.9.1 PHP Classes................................................. 20 1.9.2 PHP Constructor Function.......................................... 20 1.10 Conclusion...................................................... 21 1.11 Download the source code.............................................. 21 PHP Programming Cookbook iii 2 Upload Script Example 22 2.1 Preparing the environment.............................................. 22 2.1.1 Installation.................................................. 22 2.1.2 PHP configuration.............................................. 22 2.2 Upload form...................................................... 23 2.2.1 Accepting only certain type of files..................................... 23 2.3 PHP upload script................................................... 24 2.3.1 A secure file upload script.......................................... 24 2.3.2 Considerations................................................ 27 2.4 Summary....................................................... 27 2.5 Download the source code.............................................. 27 3 Mail Function Example 28 3.1 Preparing the environment.............................................. 28 3.1.1 Installation.................................................. 28 3.1.2 sSMTP configuration............................................. 29 3.1.3 PHP configuration.............................................. 29 3.2 PHP mail sending script................................................ 29 3.2.1 Setting additional headers.......................................... 30 3.2.2 Setting additional parameters......................................... 30 3.3 Troubleshooting.................................................... 31 3.3.1 Set verbose mode............................................... 31 3.3.2 Login is rejected by SMTP server...................................... 31 3.3.3 Firewall is filtering outgoing traffic..................................... 31 3.3.4 Check PHP error log............................................. 31 3.4 Alternatives to sSMTP................................................ 31 3.5 Summary....................................................... 32 3.6 Download the source code.............................................. 32 4 Date Format Example 33 4.1 Preparing the environment.............................................. 33 4.1.1 Installation.................................................. 33 4.2 How should dates be stored?............................................. 33 4.3 PHP examples..................................................... 34 4.3.1 From time stamp to human-readable..................................... 34 4.3.2 From human-readable to time stamp..................................... 34 4.3.3 DateTime class................................................ 35 4.3.4 Increasing precision............................................. 35 4.3.5 Validating user introduced date........................................ 36 4.4 Summary....................................................... 37 4.5 Download the source code.............................................. 37 PHP Programming Cookbook iv 5 SoapClient Example 38 5.1 Preparing the environment.............................................. 38 5.1.1 Installation.................................................. 38 5.1.2 PHP configuration.............................................. 38 5.2 What is SOAP?.................................................... 39 5.3 PHP example of SOAP clients............................................ 39 5.3.1 Working directory structure......................................... 39 5.3.2 The server................................................... 40 5.3.3 The client................................................... 40 5.3.4 Using the client................................................ 42 5.4 Considerations.................................................... 44 5.5 Summary....................................................... 44 5.6 Download the source code.............................................. 44 6 Login Form Example 45 6.1 Preparing the environment.............................................. 45 6.1.1 Installation.................................................. 45 6.1.2 PHP configuration.............................................. 45 6.2 How should passwords be stored?.......................................... 45 6.2.1 Worse: plain text............................................... 46 6.2.2 Not bad: password hashing.......................................... 46 6.2.3 Better: password hashing + salting...................................... 46 6.2.4 Even better: Key Derivation Functions.................................... 46 6.3 Creating users..................................................... 47 6.4 Login......................................................... 48 6.4.1 Form..................................................... 48 6.4.2 Form handling................................................ 48 6.4.3 Login against database............................................ 49 6.5 Summary....................................................... 52 6.6 Download the source code.............................................. 52 7 Curl Get/Post Example 53 7.1 Preparing the environment.............................................. 53 7.1.1 Installation.................................................. 53 7.1.2 PHP configuration.............................................. 53 7.2 GET requests..................................................... 53 7.3 POST requests.................................................... 55 7.4 Encapsulating operations in a cURL class...................................... 56 7.5 Summary....................................................... 59 7.6 Download the source code.............................................. 59 PHP Programming Cookbook v 8 HTML Table Example 60 8.1 Getting Started.................................................... 60 8.1.1 Forms..................................................... 60 8.1.2 Session in php................................................ 61 8.1.3 Html Tables.................................................. 63 8.1.4 Other table tags worthy of mention..................................... 63 8.2 Summary....................................................... 63 8.3 Download the source code.............................................. 63 PHP Programming Cookbook vi Copyright(c) Exelixis MediaP.C., 2016 All rights reserved. Without limiting the rights under copyright reserved above, no part of this publication may be reproduced, stored or introduced intoa retrieval system, or transmitted, in any form or by any means(electronic, mechanical, photocopying, recording or otherwise), without the prior written permission of the copyright owner. PHP Programming Cookbook vii Preface PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. Originally created by Rasmus Lerdorf in 1994, the PHP reference implementation is now produced by The PHP Group. PHP originally stood for Personal Home Page, but it now stands for the recursive backronym PHP: Hypertext Preprocessor. PHP code may be embedded into HTML code, or it can be used in combination with various web template systems, web content management systems and web frameworks. PHP code is usually processed by a PHP interpreter implemented as a module in the web server or as a Common Gateway Interface (CGI) executable. The web server combines the results of the interpreted and executed PHP code, which may be any type of data, including images, with the generated web page. (Source: https://en.wikipedia.org/wiki/PHP) In this ebook, we provide a compilation of PHP based examples that will help you kick-start your own web projects. We cover a wide range of topics, from HTML tables and files uploading,