PHP for Absolute Beginners - Jason Lengstorf, Thomas Blom Hansen

PHP for Absolute Beginners (eBook)

eBook Download: PDF
2014 | 2nd ed.
XVI, 256 Seiten
Apress (Verlag)
978-1-4302-6814-7 (ISBN)
Systemvoraussetzungen
46,99 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen

PHP is a server-side scripting language that enables you to develop dynamic sites that engage users in ways that are simply not possible using only HTML and CSS. PHP for Absolute Beginners takes a practical approach to teaching you how to build dynamic content for your website using PHP. You’ll quickly get started with practical PHP projects, learning how to build a dynamic image gallery. By the end of the book you’ll will have developed a personal blog complete with a password protected admin module.

PHP for Absolute Beginners won't swamp you with every detail of the full PHP language up front – instead, you’ll be introduced to a small, versatile subset of PHP and learn to use it creatively to develop dynamic web sites. In the process you will learn to use variables, control structures, functions, and objects to your advantage. You will learn how to plan and create databases and how to organize your PHP scripts beautifully. At the end of the book, you will be a confident PHP user, ready to take the next steps in creating great websites.



Jason Lengstorf is a 27-year-old turbogeek from Portland, OR. He started building websites in his late teens when his band couldn t afford to pay someone to do it, and he continued building websites after he realized his band wasn t actually very good. He s been a full-time freelance web developer since 2007, and expanded his business under the name Copter Labs, which is now a distributed freelance collective, keeping about 10 freelancers worldwide busy. He is also the author of PHP for Absolute Beginners and Pro PHP and jQuery.
PHP is a server-side scripting language that enables you to develop dynamic sites that engage users in ways that are simply not possible using only HTML and CSS. PHP for Absolute Beginners takes a practical approach to teaching you how to build dynamic content for your website using PHP. You’ll quickly get started with practical PHP projects, learning how to build a dynamic image gallery. By the end of the book you’ll will have developed a personal blog complete with a password protected admin module.PHP for Absolute Beginners won't swamp you with every detail of the full PHP language up front – instead, you’ll be introduced to a small, versatile subset of PHP and learn to use it creatively to develop dynamic web sites. In the process you will learn to use variables, control structures, functions, and objects to your advantage. You will learn how to plan and create databases and how to organize your PHP scripts beautifully. At the end of the book, you will be a confident PHP user, ready to take the next steps in creating great websites.

Jason Lengstorf is a 27-year-old turbogeek from Portland, OR. He started building websites in his late teens when his band couldn t afford to pay someone to do it, and he continued building websites after he realized his band wasn t actually very good. He s been a full-time freelance web developer since 2007, and expanded his business under the name Copter Labs, which is now a distributed freelance collective, keeping about 10 freelancers worldwide busy. He is also the author of PHP for Absolute Beginners and Pro PHP and jQuery.

Contents at a Glance 3
Contents 225
About the Authors 234
About the Technical Reviewer 235
Acknowledgments 236
Introduction 4
Part I: PHP/MySQL Basics 5
Chapter 1: Setting Up a PHP Development Environment 6
Why You Need Apache, MySQL, and PHP 6
How PHP Works 7
Apache and What It Does 7
Storing Info with MySQL 7
Installing PHP, Apache, and MySQL 8
Installing XAMPP 8
Step 1: Download XAMPP 8
Step 2: Open the Installer and Follow the Instructions 8
Step 3: Test XAMPP to Ensure Proper Installation 13
Open the XAMPP Control Panel 13
What If Apache Isn’t Running? 14
Verify That Apache and PHP Are Running 15
Choosing a PHP Editor 15
Creating Your First PHP File 16
Running Your First PHP Script 17
Summary 18
Chapter 2: Understanding PHP: Language Basics 19
Embedding PHP Scripts 20
Using echo 21
What Is a Variable? 21
Storing Values in a Variable 21
A Variable Is a Placeholder 21
Valid PHP Variable Names 22
Displaying PHP Errors 22
Creating an HTML5 Page with PHP 23
Including a Simple Page Template 24
Returning Values 24
Including the Template 24
Commenting Your Code 25
Block and Single-Line Comments 25
Avoiding Naming Conflicts 25
The Object Operator 26
Using a StdClass Object for Page Data 26
Object Properties Are Like Variables 27
Page Views 27
Making a Dynamic Site Navigation 27
Passing Information with PHP 28
Accessing URL Variables 29
Using isset(?) to Test If a Variable Is Set 29
$_GET, a Superglobal Array 29
Including Page Views Dynamically 30
Concatenation 31
Strict Naming Convention 31
Displaying a Default Page 31
Validating Your HTML 32
Styling the Site with CSS 32
Declaring a Page_Data Class 33
Classes Make Objects 34
Highlighting Current Navigation Item with a Dynamic Style Rule 34
Taking a Closer Look at the Dynamic CSS 36
Summary 36
Chapter 3: Form Management 37
What Are Forms? 37
Setting Up a New PHP Project 38
Seeing for Yourself 40
Creating a Dynamic Navigation 40
Creating Page Views for the Form 41
Displaying Page Views on index.php 41
Spending Your Time Wisely: Conventions and Reuse 42
A Super-Simple Search Form 42
Trying Your Search Form 42
Forms Encode URL Variables 43
A Name Attribute Declares the Name of a URL Variable 43
The < input>
Understanding the Method Attribute 44
Named PHP Functions 44
The Basic Syntax for Functions 44
Using Function Arguments for Increased Flexibility 46
Creating a Form for the Quiz 46
Showing the Quiz Form 46
Using < select>
The POST Method 47
Using the $_POST Superglobal 47
$_POST Is an Array 48
If and Else Explained 50
Evaluating the Quiz Response 50
The Identical Comparison Operator 50
Curly’s Law: Do One Thing 51
Meaningful Names 51
Code Is Poetry 52
Styling Forms 52
Exercises 53
Summary 53
Chapter 4: Building a Dynamic Image Gallery with Image Upload 54
Setting Up a Dynamic Site 54
Prerequisites: A Folder with Some Images 54
Creating a Navigation 55
Creating Two Dummy Page View Files 55
Creating the Index File 55
Time to Test 56
Adding Two Style Sheets to One Page 56
Staying DRY 56
Improving the Page_Data Class with a Method 57
Is It a Function or a Method? 57
What Is $this? 57
Using the New Method 58
You Can Only Use Methods That Are Declared 58
Preparing a Function for Displaying Images 58
Iteration 59
Using a DirectoryIterator to Find Files in a Folder 59
Showing All Images 60
Creating a Form View 61
Showing a Form for Uploading Images 62
$_FILES 62
tmp_name 63
error 63
Uploading Files with PHP 63
Planning an Uploader Class 64
UML 64
Uploader Class Requirements 64
The Magic Method __ construct() 65
Saving the Uploaded File 66
Using the Uploader Class 67
What Could Go Wrong? 67
The Single Responsibility Principle 67
Summary 68
Chapter 5: Spicing Up Your Image Gallery with JavaScript and CSS 69
Client-Side vs. Server-Side Programming 69
Coding a Lightbox Gallery 70
Embedding an External JavaScript File 70
Preparing the Page_Data Class for JavaScript Files 70
Preparing the Page Template for JavaScript Files 71
Writing and Running an External JavaScript File 72
Using window.console.log() 72
JavaScript Arrays 73
Using var to Declare a Variable 73
Looping Through Array Items 73
Simple Progressive Enhancement 74
Using Event Listeners 74
The Event 75
The Event Handler 75
The Optional useCapture 75
Creating Markup for the Overlay and Big Image 75
document.querySelector(?) 76
Showing the Overlay 76
Hiding the Overlay and Resize Thumbnails 76
Only Showing Thumbnails to Capable Browsers 78
Getting an Array of HTML Elements with querySelectorAll(?) 78
Showing a Big Image 78
Using the MouseEvent Object 79
Toggling 80
Manipulating Attributes 80
Hiding the Big Image 81
Using a CSS Animation 81
Coding Challenge 82
Summary 82
Chapter 6: Working with Databases 83
The Basics of MySQL Data Storage 83
Manipulating Data with SQL 85
Developing a Database for the Poll 86
Creating a Database Using CREATE 86
The CREATE TABLE Statement 87
Understanding PRIMARY KEY 89
Understanding AUTO_INCREMENT 90
The INSERT Statement 90
The SELECT Statement 91
The UPDATE Statement 92
Coding a Database-Driven Site Poll 92
Separating Concerns with MVC 92
Planning the PHP Scripts 93
Creating the Poll Project 94
Making a Poll Controller 95
Making a Poll Model 95
Making a Poll View 96
Hooking Up Poll View with Poll Model 96
Coding Is Like Playing the Blues 97
Connecting to MySQL from PHP 98
PHP Data Objects (PDO) 98
Opening a Connection 98
Using a try-catch Statement 99
Using Constructor Arguments 100
Sharing the Database Connection with the Poll Model 102
Retrieving Data with a PDOStatement 103
PDO and PDOStatement Objects 103
The PDOStatement’s fetchObject(?) Method 104
Showing a Poll Form 104
Triggering a Custom Error Message 105
Updating a Database Table According to Form Input 105
Responding to User Input 106
Summary 107
Part II:A Blogging System 108
Chapter 7: Building the Entry Manager 109
Creating the blog_entry Database Table 109
Planning the PHP Scripts 110
Creating the Blog Site 111
Creating the Entry Manager Navigation 112
Loading Admin Module Controllers 113
Creating the Entry Input Form 114
Styling the Editor 115
Connecting to the Database 117
Using Design Patterns 117
The Table Data Gateway Design Pattern 118
Writing the Entry_Table Class 118
Processing Form Input and Saving the Entry 119
Which Button Was Clicked? 120
Security Alert: SQL Injection Attacks 121
Usability Alert: Blog Entries with Quote Characters 121
Solution: Prepared Statements 122
Summary 123
Chapter 8: Showing Blog Entries 124
Creating a Public Blog Front Page 124
Creating a Blog Controller 125
Getting Data for All Blog Entries 125
Using an SQL SUBSTRING Clause 127
Using an SQL Alias 127
Testing Your Model 127
Using print_r() for Inspecting an Object 128
Preparing a View for All Blog Entries 129
Hooking Up View and Model 130
Responding to User Requests 130
Getting Entry Data 132
Creating a Blog View 132
Displaying an Entry 133
Code Smell: Duplicate Code 133
Staying DRY with Curly 134
Refactoring with Curly 134
Using the Private Access Modifier 137
Summary 138
Chapter 9: Deleting and Updating Entries 139
Creating a Model for Administrative Links 139
Displaying Administrative Links 140
Populating Your Form with the Entry to Be Edited 141
Handling Entry Deletion 143
Deleting Entries from the Database 143
Responding to Delete Requests 144
Preparing a Model to Update Entries in the Database 145
Controller: Should I Insert or Update? 146
Communicating Changes 147
Step 1: Update Model 147
Step 2: Update Controller 148
Step 3: Update View 150
Insisting on a Title 151
Improving Editor Usability with Progressive Enhancement 152
Embedding Your External JavaScript 153
Showing a Warning If Title Is Empty 154
Other Usability Flaws 156
A Coding Challenge: Fix a Usability Flaw 156
Summary 157
Chapter 10: Improving Your Blog with User Comments and Search 158
Building and Displaying the Comment Entry Form 158
A Combined View 159
Creating a Comment Table in the Database 161
Using a Foreign Key 162
Building a Comment_Table Class 162
Staying DRY with Inheritance 163
Is-a Relationships 165
Using Inheritance in Your Code 165
Inserting New Comments into the Database 166
Testing the saveComment() Method 167
Retrieving All Comments for a Given Entry 168
Testing getAllById() 168
Creating a View for Listing Comments 169
Hooking Up View and Model to Display Comments 170
Inserting a Comment Through the Comment Form 170
Practice Makes Perfect 171
Searching for Entries 172
The Search View 172
Responding to a User Search 173
The Search Model 174
Searching with a LIKE Condition 175
Test Model 175
A Search Result View 176
Loading a Search Result View from the Controller 177
Exercise: Improving Search 177
Summary 177
Chapter 11: Adding Images to Blog Entries 178
Problem: Cannot Delete an Entry with Comments 178
Understanding Foreign Key Constraints 179
Deleting Comments Before Blog Entry 179
Improving Usability with WYSIWYG 180
Integrating TinyMCE 180
Creating an Image Manager 182
Showing a Form for Uploading Images 183
A Quick Refresher on the $_FILES Superglobal Array 184
Uploading an Image 185
What Could Possibly Go Wrong? 186
Updating the Uploader Class 186
Error: Restrictive Folder Permissions 187
Error: Upload File Too Big 189
Configuring via ini.php or .htaccess 190
Detecting Other Errors 191
Further Refinements 192
Displaying Images 193
Deleting Images 194
Using an Image in a Blog Entry 196
Improving Editor Usability 196
Summary 200
Chapter 12: Password Protection 201
Creating an admin_table in the Database 201
Encrypting Passwords with MD5 202
One-Way Encryption 202
Sufficient Security 202
Adding Administrators in the Database 203
Building an HTML Form 203
Saving New Administrators in the Database 204
Planning Login 205
Creating a Login Form 206
Hiding Controls from Unauthorized Users 207
Logging In a User 208
HTTP Is Stateless 208
Superglobals Revisited: $_SESSION 209
Persisting State with a Session 209
Logging Users Out 210
Allowing Authorized Users Only 211
Exercises 212
Summary 213
Chapter 13: Going Public with Your Blog 214
Web Host Requirements 214
Exporting and Importing Your Database 214
Preparing Your PHP Files for Upload 216
Uploading Files with FileZilla FTP 216
Summary 217
Index 218

Erscheint lt. Verlag 7.8.2014
Zusatzinfo XVI, 256 p. 48 illus.
Verlagsort Berkeley
Sprache englisch
Themenwelt Mathematik / Informatik Informatik Theorie / Studium
Mathematik / Informatik Informatik Web / Internet
ISBN-10 1-4302-6814-X / 143026814X
ISBN-13 978-1-4302-6814-7 / 9781430268147
Haben Sie eine Frage zum Produkt?
PDFPDF (Wasserzeichen)
Größe: 4,0 MB

DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasser­zeichen und ist damit für Sie persona­lisiert. Bei einer missbräuch­lichen Weiter­gabe des eBooks an Dritte ist eine Rück­ver­folgung an die Quelle möglich.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schränkt geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.

Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich
Discover tactics to decrease churn and expand revenue

von Jeff Mar; Peter Armaly

eBook Download (2024)
Packt Publishing (Verlag)
25,19