Use PHP to enhance your web pages. This book shows you how PHP programming works, and how to write and organize PHP code.
The book starts with steps to download and install a setup for a sample website that will form the basis for upcoming chapters. You start by writing PHP code and learn how to mix it with HTML and manage the code. From there, you will learn about dynamic content, along with a deep dive into form processing and sending email. Saving uploaded data and uploading files is discussed next. You will learn how to configure your PHP project and develop a library. You will then learn how to create an image catalog and manage data on your web page. By the end of the book, you will understand how to work with cookies, sessions, and logging in, followed by an example of creating a simple blog that reiterates the concepts developed in the previous chapters.
After reading this book, you will be able to configure, create, and manage your dynamic website.
What Will You Learn
- Know the basics of programming with the PHP language
- Use PHP to generate dynamic web pages
- Work with SQL databases
- Work with forms to upload text data and binary files
- Use tips and tricks to write clean and maintainable code
Who This Book Is For
Web developers and students learning to develop and maintain PHP code on their website
Author(s): Mark Simon
Edition: 1
Publisher: Apress
Year: 2024
Language: English
Commentary: Publisher PDF | Published: 08 March 2024
Pages: xxvi, 625
City: Berkeley, CA
Tags: PHP 8; Dynamic Web; SQL; HTML; Database; CSS; MySQL
Table of Contents
About the Author
About the Technical Reviewers
Acknowledgments
Introduction
Chapter 1: Introduction and Setting Up
How the Web Works
The Sample Project
The Tools
A Coding Editor
Web Server Setup
Using the Virtual Hosts Applications
Adding the Domain Name
Editing the httpd.conf File
Adding a Virtual Host
Fixing the php.ini File
Configuring PHP
Setting PHP Options
Some Useful Configuration Options
PHP Execution
File Uploads
Legacy Settings
Sessions
Error Reporting
Other Settings
Starting Up
The Project Configuration Files
Finishing Up
Chapter 2: Working with PHP
Before We Start
Grouping: Brackets and Friends
Data Types
How PHP Processes Files
Renaming the Files
PHP Scripts
Experimenting
Adding Dynamic Data to the Page
Displaying Data
Including Files
Preparing and Implementing Include
Headings and Titles
Including Variables in the Include Files
Setting the Title and Heading
The Navigation Block
The Links Array
Creating the List of Links
Displaying the Links
Highlighting the Current Page
Comments
Summary
Coming Up
Chapter 3: Email and Form Processing
Preparation
The Contact Form
The Plan of Attack
Reading the Form Data
Outlining the Plan
Reading the Form Data
Preparing the Data
Persisting Fields
Form Validation
JavaScript and HTML5 Validation
Planning for Validation
Required Fields
Checking an Email Address
Email Injection
Handling the Errors
Displaying the Errors
The Errors Paragraph
Printing Empty Arrays
Sending Email with the mail() Function
The Main Email Variables
The Email Headers
The Header Data
After Sending the Message
Alternative Parts
Writing the Conditional Blocks
Reorganizing the Code
Configuration
Moving the Code
Summary
Coming Up
Chapter 4: Uploading Data and Files
The Upload Form
The File Input
Settings
Preliminary Coding
The Data Variables
Processing POSTed Data and Preliminary Validation
Image Validation
The $_FILES Array
Using File Data
Checking an Image File
Keeping the File
The File Name
Lower Case
Replacing Spaces
Defining the Location
Moving the File
Creating Additional Copies
Resized Image Configuration
Generating the Resized Copies
Tidying Up
Clearing the Error Array
Clearing the Old Values
Summary
Coming Up
Chapter 5: Working with a Database
Something About Databases
Running SQL
MySQL and PHPMyAdmin
Browsing
The Setup Page
Creating the Database and User
Preparing the Images Table
Using the Database with PHP
Connection
Connection String
Creating a Connection
A Crash Course in Classes and Objects
Handling Connection Errors
exit or die
Adding Options
Error Reporting
Prepared Statements
Setting the PDO Options
Using the Alternative Script
Summary
Coming Up
Chapter 6: Adding Data to the Database
Connecting to the Database
SQL Injection
Prepared Statements
When to Worry About SQL Injection
Adding the Image Data to the Table
Line Breaks
Creating Line Break Functions
The Prepared SQL Statement
Executing with Data
Retrieving the New ID
Modifying the Name
Constructing the New Name
Zero-Padding a Number
Updating the Database
Keeping the File and Creating the Resized Copies
Refactoring the Code
The Function Outline
Scope
Writing the addImageData() Function
Writing the addImageFile() Function
Using the Code in the Future
Summary
Coming Up
Chapter 7: File Handling
Preparation
The ZIP File
The CSV File
Processing the Uploaded Zip File
Checking the Zip File
Unzipping the File
Reading Text Files
Removing the Header Row
Splitting CSV Data
Importing the Data
Checking the MIME Types
Checking the Images
Finishing Up
A Crash Course in HTTP(S)
Headers Already Sent
Redirecting to the Next Page
Starting Over
Summary
Coming Up
Chapter 8: The Image Gallery
Preparation
Fetching the Thumbnails
Fetching from the Database
Fetching a Gallery Page
Fetching the Rows
Generating the Gallery Images
Selecting the Page
The Navigation Block
Remembering the Page
Displaying a Larger Image
Fetching a Miscellaneous Single Image
Selecting a Random Row from the Database
Fetching a Selected Image
Displaying a Random Image on Other Pages
Summary
Coming Up
Chapter 9: Managing Data
The Image Pages
The Image List Page
Preparatory Code for the Image List
Something About HTML Tables
Generating the Table Data
The Image Editing Page
The Prepare Events
Fetching the Data to be Edited
Adjusting the Image Blocks
Adjusting the Buttons
Adjusting the Upload Form
Disabling Fields for Remove
Implementing Update and Delete
Deleting Data
Updating the Image Data
Updating the Text Data
Replacing the Image
Checking a Checkbox
Adding the Checkbox
Testing the Checkbox
Finishing Touches
Clearing the $id when Adding an Image
Fixing the Hidden Field
Restoring the Images
Summary
Coming Up
Chapter 10: Sessions and Logging In
Sessions
Session Settings
Starting a Session
Using Session Data
Managing Users
Encryption and Hashing
Password Hashing in PHP
The User Table
Adding a User
Method 1: Running the SQL Directly
Method 2: Adding a Password Using PHPMyAdmin
Method 3: Adding an Admin User with the Setup Page
Logging In
Processing Login
Successful Login
Switching Between Content
Showing the User’s Name
Logging Out
Restricting Access
Summary
Coming Up
Chapter 11: A Configuration System
The ini File Contents
Reading an ini File
Generating the Configuration Form
Generating the Config Table
Saving the Configuration
Reading the New Values
Writing the ini File
Using the Configuration Settings
Resizing Images
Adding Configuration to Administration
Summary
Coming Up
Chapter 12: A Content Management System
The Blog Pages
The Blog Table
Preparatory Blog Code
Adding a Single Blog Article
Uploading the Optional Image
Checking for an Optional Image
Keeping the Optional Image
Previewing the Image
Selecting an Existing Image
Selecting an Image from a Menu
Processing the Selected Image
Previewing the Selected Image
Selecting an Image from a Radio Group
Choosing Between Buttons and the Menu
Redirecting to the Blog List Page
Importing Blog Articles
Changes to the Insert Code
Starting the Import Code
Handling the Import File
Copying the Blog Import Files
Importing the Blog Data
Calling a Function with an Array of Parameters
Finishing the Import
Reading a Blog Article
Preliminary Code
Getting the Selected Article ID
Fetching the Article
Fixing the Article Line Breaks
Displaying the Blog Image
Managing the Blog Articles
Building the Article Table
Processing the Prepare Events
Displaying the Image
Finishing the Preparation
Updating an Article
Deleting an Article
The Visitor Blog List
Preparatory Code
Generating the Blog Article List
Summary
Appendix A: Adding Markdown to Your Blog Articles
The Markdown Language
Adding Markdown to the Database
Adding a Checkbox to the Form
Reading the Markdown Setting
Manipulating the Markdown Setting
Displaying the Markdown Article
Appendix B: Non-PHP Tricks
Toggle Background
Visible Passwords
Required Upload
Preview Image
Previewing a Referenced Image
The File Input
Light Box
Paging Page Number
The Hopping Kangaroo
Appendix C: PHP Versions
The array() and list() Language Constructs
The … Operator
mail() Function Additional Headers
Function Data Types
Named Parameters
Appendix D: Default Library Functions
Resizing Images
The resizeImage() Function
Interpreting the Resize Dimensions
Loading the Original Image
Getting Information About the Original Image
Loading the Image Data
Saving the Resized Image
Creating the Blank Copy and Copying the Original
Copying the Original into the Image Copy
Padding Adjustments for Distorted Copies
Image Shapes
Adjusting the Size and Origin
The Complete resizeImage Function (So Far…)
Options
Omitting the Destination Name
Using the Options Array
Setting a Background Color
Changing the Saved Image Type
Setting a Clear Padding Background
Trimming the Image Copy
Using Named Parameters (PHP 8)
Named Parameters in the resizeImage() Function
splitSize
unzip Function
Markdown to HTML
Anchors
Headings
Miscellaneous Functions
The MimeType() Function
The printr() Function
Index