100% found this document useful (12 votes)
46 views

[FREE PDF sample] (Ebook) An Introduction to PHP: Learn PHP 8 to Create Dynamic Websites by Mark Simon ISBN B0CSS5DBK4 ebooks

The document provides a comprehensive overview of various ebooks related to PHP programming, including titles such as 'An Introduction to PHP: Learn PHP 8 to Create Dynamic Websites' by Mark Simon and 'Learning PHP, MySQL, JavaScript, CSS & HTML5' by Robin Nixon. It includes links for downloading these ebooks in multiple formats and highlights the importance of PHP in creating dynamic websites. Additionally, it outlines the structure and content of the book, including chapters on setting up PHP, working with databases, and form processing.

Uploaded by

dhaduksiria61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (12 votes)
46 views

[FREE PDF sample] (Ebook) An Introduction to PHP: Learn PHP 8 to Create Dynamic Websites by Mark Simon ISBN B0CSS5DBK4 ebooks

The document provides a comprehensive overview of various ebooks related to PHP programming, including titles such as 'An Introduction to PHP: Learn PHP 8 to Create Dynamic Websites' by Mark Simon and 'Learning PHP, MySQL, JavaScript, CSS & HTML5' by Robin Nixon. It includes links for downloading these ebooks in multiple formats and highlights the importance of PHP in creating dynamic websites. Additionally, it outlines the structure and content of the book, including chapters on setting up PHP, working with databases, and form processing.

Uploaded by

dhaduksiria61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Download Full Version ebook - Visit ebooknice.

com

(Ebook) An Introduction to PHP: Learn PHP 8 to


Create Dynamic Websites by Mark Simon ISBN
B0CSS5DBK4

https://ebooknice.com/product/an-introduction-to-php-learn-
php-8-to-create-dynamic-websites-56083930

Click the button below to download

DOWLOAD EBOOK

Discover More Ebook - Explore Now at ebooknice.com


Instant digital products (PDF, ePub, MOBI) ready for you
Download now and discover formats that fit your needs...

Start reading on any device today!

(Ebook) Learning PHP, MySQL, JavaScript, CSS & HTML5,


Third Edition: A Step-by-Step Guide to Creating Dynamic
Websites by Robin Nixon ISBN 9781491949467, 1491949465
https://ebooknice.com/product/learning-php-mysql-javascript-css-
html5-third-edition-a-step-by-step-guide-to-creating-dynamic-
websites-4724548
ebooknice.com

(Ebook) PHP Programming: A Step-by-Step Guide to Learn, in


an Easy Way, the Fundamentals of PHP Programming Language
3nd Edition by Daniel Robinson ISBN B09QK59RH3
https://ebooknice.com/product/php-programming-a-step-by-step-guide-to-
learn-in-an-easy-way-the-fundamentals-of-php-programming-language-3nd-
edition-37577494
ebooknice.com

(Ebook) PHP 8 Quick Scripting Reference: A Pocket Guide to


PHP Web Scripting - Third Edition by Mikael Olsson ISBN
9781484266199, 1484266196
https://ebooknice.com/product/php-8-quick-scripting-reference-a-
pocket-guide-to-php-web-scripting-third-edition-24659536

ebooknice.com

(Ebook) PHP 8 Solutions: Dynamic Web Design and


Development Made Easy by David Powers ISBN 9781484271407,
1484271408
https://ebooknice.com/product/php-8-solutions-dynamic-web-design-and-
development-made-easy-35754080

ebooknice.com
(Ebook) Practical PHP 7, MySQL 8, and MariaDB Website
Databases A Simplified Approach to Developing Database-
Driven Websites - 2nd Ed by Adrian W. West, Steve
Prettyman ISBN 9781484238424, 1484238427
https://ebooknice.com/product/practical-php-7-mysql-8-and-mariadb-
website-databases-a-simplified-approach-to-developing-database-driven-
websites-2nd-ed-35162924
ebooknice.com

(Ebook) php architect's Guide to PHP Security by Ilia


Alshanetsky ISBN 9780973862102, 0973862106

https://ebooknice.com/product/php-architect-s-guide-to-php-
security-1970450

ebooknice.com

(Ebook) PHP 8 Solutions: Dynamic Web Design and


Development Made Easy - Fifth Edition by David Powers ISBN
9781484271414, 1484271416
https://ebooknice.com/product/php-8-solutions-dynamic-web-design-and-
development-made-easy-fifth-edition-35597858

ebooknice.com

(Ebook) Learn PHP 8: Using MySQL, JavaScript, CSS3, and


HTML5 by Steve Prettyman ISBN 9781484262399,
9781484262405, 1484262395, 1484262409
https://ebooknice.com/product/learn-php-8-using-mysql-javascript-
css3-and-html5-12040982

ebooknice.com

(Ebook) Node.js for PHP Developers: Porting PHP to Node.js


by Daniel Howard ISBN 9781449333607, 1449333605

https://ebooknice.com/product/node-js-for-php-developers-porting-php-
to-node-js-4078222

ebooknice.com
An Introduction
to PHP
Learn PHP 8 to Create
Dynamic Websites

Mark Simon
An Introduction
to PHP
Learn PHP 8 to Create
Dynamic Websites

Mark Simon
An Introduction to PHP: Learn PHP 8 to Create Dynamic Websites
Mark Simon
Ivanhoe VIC, VIC, Australia
ISBN-13 (pbk): 979-8-8688-0176-1 ISBN-13 (electronic): 979-8-8688-0177-8
https://doi.org/10.1007/979-8-8688-0177-8

Copyright © 2024 by Mark Simon


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Smriti Srivastava
Development Editor: Laura Berendson
Coordinating Editor: Shaul Elson
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY
10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com,
or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance
Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for
reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub (https://github.com/Apress). For more detailed information,
please visit https://www.apress.com/gp/services/source-code.
Paper in this product is recyclable
To Inge and Maria, who helped me to put things in context.
Table of Contents
About the Author������������������������������������������������������������������������������xvii

About the Technical Reviewers���������������������������������������������������������xix

Acknowledgments�����������������������������������������������������������������������������xxi

Introduction�������������������������������������������������������������������������������������xxiii

Chapter 1: Introduction and Setting Up������������������������������������������������1


How the Web Works����������������������������������������������������������������������������������������������2
The Sample Project�����������������������������������������������������������������������������������������������4
The Tools���������������������������������������������������������������������������������������������������������������5
A Coding Editor������������������������������������������������������������������������������������������������6
Web Server Setup�������������������������������������������������������������������������������������������������7
Using the Virtual Hosts Applications��������������������������������������������������������������10
Adding the Domain Name������������������������������������������������������������������������������11
Editing the httpd.conf File�����������������������������������������������������������������������������13
Adding a Virtual Host�������������������������������������������������������������������������������������15
Fixing the php.ini File������������������������������������������������������������������������������������18
Configuring PHP��������������������������������������������������������������������������������������������������21
Setting PHP Options��������������������������������������������������������������������������������������23
Some Useful Configuration Options���������������������������������������������������������������24
The Project Configuration Files���������������������������������������������������������������������27
Finishing Up��������������������������������������������������������������������������������������������������������29

v
Table of Contents

Chapter 2: Working with PHP�������������������������������������������������������������31


Before We Start���������������������������������������������������������������������������������������������������32
Grouping: Brackets and Friends��������������������������������������������������������������������32
Data Types�����������������������������������������������������������������������������������������������������33
How PHP Processes Files������������������������������������������������������������������������������������34
Renaming the Files����������������������������������������������������������������������������������������35
PHP Scripts����������������������������������������������������������������������������������������������������36
Adding Dynamic Data to the Page�����������������������������������������������������������������39
Displaying Data���������������������������������������������������������������������������������������������42
Including Files�����������������������������������������������������������������������������������������������������44
Preparing and Implementing Include������������������������������������������������������������46
Headings and Titles���������������������������������������������������������������������������������������������48
Including Variables in the Include Files���������������������������������������������������������49
Setting the Title and Heading������������������������������������������������������������������������51
The Navigation Block������������������������������������������������������������������������������������������51
The Links Array����������������������������������������������������������������������������������������������53
Creating the List of Links�������������������������������������������������������������������������������54
Displaying the Links��������������������������������������������������������������������������������������58
Highlighting the Current Page�����������������������������������������������������������������������59
Comments�����������������������������������������������������������������������������������������������������������62
Summary������������������������������������������������������������������������������������������������������������64
Coming Up�����������������������������������������������������������������������������������������������������������65

Chapter 3: Email and Form Processing����������������������������������������������67


Preparation���������������������������������������������������������������������������������������������������������68
The Contact Form������������������������������������������������������������������������������������������������68
The Plan of Attack������������������������������������������������������������������������������������������72

vi
Table of Contents

Reading the Form Data���������������������������������������������������������������������������������������73


Outlining the Plan������������������������������������������������������������������������������������������75
Reading the Form Data���������������������������������������������������������������������������������������76
Preparing the Data�����������������������������������������������������������������������������������������77
Persisting Fields��������������������������������������������������������������������������������������������78
Form Validation���������������������������������������������������������������������������������������������������80
JavaScript and HTML5 Validation������������������������������������������������������������������80
Planning for Validation�����������������������������������������������������������������������������������81
Required Fields����������������������������������������������������������������������������������������������82
Checking an Email Address���������������������������������������������������������������������������84
Email Injection�����������������������������������������������������������������������������������������������85
Handling the Errors����������������������������������������������������������������������������������������89
Sending Email with the mail() Function��������������������������������������������������������������92
The Main Email Variables������������������������������������������������������������������������������93
The Email Headers����������������������������������������������������������������������������������������95
After Sending the Message���������������������������������������������������������������������������������99
Alternative Parts��������������������������������������������������������������������������������������������99
Writing the Conditional Blocks��������������������������������������������������������������������102
Reorganizing the Code��������������������������������������������������������������������������������������104
Configuration�����������������������������������������������������������������������������������������������104
Moving the Code������������������������������������������������������������������������������������������105
Summary����������������������������������������������������������������������������������������������������������106
Coming Up���������������������������������������������������������������������������������������������������������108

Chapter 4: Uploading Data and Files������������������������������������������������109


The Upload Form�����������������������������������������������������������������������������������������������109
The File Input����������������������������������������������������������������������������������������������������112
Settings�������������������������������������������������������������������������������������������������������113

vii
Table of Contents

Preliminary Coding��������������������������������������������������������������������������������������������114
The Data Variables���������������������������������������������������������������������������������������114
Processing POSTed Data and Preliminary Validation�����������������������������������116
Image Validation������������������������������������������������������������������������������������������������118
The $_FILES Array���������������������������������������������������������������������������������������118
Using File Data��������������������������������������������������������������������������������������������119
Checking an Image File�������������������������������������������������������������������������������122
Keeping the File������������������������������������������������������������������������������������������������125
The File Name����������������������������������������������������������������������������������������������126
Lower Case��������������������������������������������������������������������������������������������������128
Replacing Spaces����������������������������������������������������������������������������������������128
Defining the Location����������������������������������������������������������������������������������129
Moving the File��������������������������������������������������������������������������������������������131
Creating Additional Copies��������������������������������������������������������������������������������132
Resized Image Configuration�����������������������������������������������������������������������134
Generating the Resized Copies��������������������������������������������������������������������135
Tidying Up���������������������������������������������������������������������������������������������������������136
Clearing the Error Array�������������������������������������������������������������������������������136
Clearing the Old Values��������������������������������������������������������������������������������137
Summary����������������������������������������������������������������������������������������������������������137
Coming Up���������������������������������������������������������������������������������������������������������138

Chapter 5: Working with a Database������������������������������������������������139


Something About Databases�����������������������������������������������������������������������������141
Running SQL������������������������������������������������������������������������������������������������144
MySQL and PHPMyAdmin����������������������������������������������������������������������������144
Browsing�����������������������������������������������������������������������������������������������������146
The Setup Page�������������������������������������������������������������������������������������������146

viii
Table of Contents

Creating the Database and User�����������������������������������������������������������������������148


Preparing the Images Table�������������������������������������������������������������������������151
Using the Database with PHP���������������������������������������������������������������������������153
Connection���������������������������������������������������������������������������������������������������154
Connection String����������������������������������������������������������������������������������������154
Creating a Connection���������������������������������������������������������������������������������155
A Crash Course in Classes and Objects�������������������������������������������������������156
Handling Connection Errors�������������������������������������������������������������������������158
exit or die�����������������������������������������������������������������������������������������������������159
Adding Options��������������������������������������������������������������������������������������������160
Setting the PDO Options������������������������������������������������������������������������������162
Using the Alternative Script�������������������������������������������������������������������������163
Summary����������������������������������������������������������������������������������������������������������163
Coming Up���������������������������������������������������������������������������������������������������������164

Chapter 6: Adding Data to the Database�������������������������������������������165


Connecting to the Database������������������������������������������������������������������������������166
SQL Injection�����������������������������������������������������������������������������������������������������167
Prepared Statements�����������������������������������������������������������������������������������169
When to Worry About SQL Injection�������������������������������������������������������������171
Adding the Image Data to the Table������������������������������������������������������������������172
Line Breaks��������������������������������������������������������������������������������������������������174
Creating Line Break Functions��������������������������������������������������������������������175
The Prepared SQL Statement����������������������������������������������������������������������179
Executing with Data������������������������������������������������������������������������������������181
Retrieving the New ID����������������������������������������������������������������������������������182
Modifying the Name������������������������������������������������������������������������������������������183
Constructing the New Name������������������������������������������������������������������������184
Zero-Padding a Number������������������������������������������������������������������������������184

ix
Table of Contents

Updating the Database��������������������������������������������������������������������������������185


Keeping the File and Creating the Resized Copies��������������������������������������186
Refactoring the Code����������������������������������������������������������������������������������������187
The Function Outline�����������������������������������������������������������������������������������187
Scope�����������������������������������������������������������������������������������������������������������189
Writing the addImageData() Function����������������������������������������������������������190
Writing the addImageFile() Function�����������������������������������������������������������193
Using the Code in the Future�����������������������������������������������������������������������196
Summary����������������������������������������������������������������������������������������������������������196
Coming Up���������������������������������������������������������������������������������������������������������197

Chapter 7: File Handling�������������������������������������������������������������������199


Preparation�������������������������������������������������������������������������������������������������������200
The ZIP File��������������������������������������������������������������������������������������������������201
The CSV File�������������������������������������������������������������������������������������������������202
Processing the Uploaded Zip File����������������������������������������������������������������������203
Checking the Zip File�����������������������������������������������������������������������������������204
Unzipping the File����������������������������������������������������������������������������������������206
Reading Text Files����������������������������������������������������������������������������������������211
Importing the Data���������������������������������������������������������������������������������������217
Checking the MIME Types����������������������������������������������������������������������������219
Finishing Up������������������������������������������������������������������������������������������������������223
A Crash Course in HTTP(S)��������������������������������������������������������������������������224
Headers Already Sent����������������������������������������������������������������������������������226
Redirecting to the Next Page�����������������������������������������������������������������������227
Starting Over�����������������������������������������������������������������������������������������������������230
Summary����������������������������������������������������������������������������������������������������������234
Coming Up���������������������������������������������������������������������������������������������������������235

x
Table of Contents

Chapter 8: The Image Gallery�����������������������������������������������������������237


Preparation�������������������������������������������������������������������������������������������������������238
Fetching the Thumbnails�����������������������������������������������������������������������������������240
Fetching from the Database������������������������������������������������������������������������240
Fetching a Gallery Page�������������������������������������������������������������������������������242
Fetching the Rows���������������������������������������������������������������������������������������246
Generating the Gallery Images��������������������������������������������������������������������248
Selecting the Page��������������������������������������������������������������������������������������252
The Navigation Block�����������������������������������������������������������������������������������258
Remembering the Page�������������������������������������������������������������������������������263
Displaying a Larger Image��������������������������������������������������������������������������������267
Fetching a Miscellaneous Single Image������������������������������������������������������267
Selecting a Random Row from the Database����������������������������������������������272
Fetching a Selected Image��������������������������������������������������������������������������274
Displaying a Random Image on Other Pages����������������������������������������������������277
Summary����������������������������������������������������������������������������������������������������������281
Coming Up���������������������������������������������������������������������������������������������������������283

Chapter 9: Managing Data����������������������������������������������������������������285


The Image Pages����������������������������������������������������������������������������������������������286
The Image List Page������������������������������������������������������������������������������������������291
Preparatory Code for the Image List������������������������������������������������������������292
Something About HTML Tables��������������������������������������������������������������������294
Generating the Table Data���������������������������������������������������������������������������298
The Image Editing Page������������������������������������������������������������������������������������305
The Prepare Events�������������������������������������������������������������������������������������306
Fetching the Data to be Edited��������������������������������������������������������������������309

xi
Table of Contents

Implementing Update and Delete����������������������������������������������������������������������315


Deleting Data�����������������������������������������������������������������������������������������������316
Updating the Image Data�����������������������������������������������������������������������������319
Checking a Checkbox����������������������������������������������������������������������������������������325
Adding the Checkbox�����������������������������������������������������������������������������������326
Testing the Checkbox����������������������������������������������������������������������������������329
Finishing Touches���������������������������������������������������������������������������������������������333
Clearing the $id when Adding an Image������������������������������������������������������333
Fixing the Hidden Field��������������������������������������������������������������������������������334
Restoring the Images����������������������������������������������������������������������������������334
Summary����������������������������������������������������������������������������������������������������������335
Coming Up���������������������������������������������������������������������������������������������������������337

Chapter 10: Sessions and Logging In�����������������������������������������������339


Sessions������������������������������������������������������������������������������������������������������������340
Session Settings������������������������������������������������������������������������������������������341
Starting a Session���������������������������������������������������������������������������������������343
Using Session Data��������������������������������������������������������������������������������������346
Managing Users������������������������������������������������������������������������������������������������348
Encryption and Hashing������������������������������������������������������������������������������349
Password Hashing in PHP���������������������������������������������������������������������������351
The User Table���������������������������������������������������������������������������������������������352
Adding a User����������������������������������������������������������������������������������������������353
Logging In���������������������������������������������������������������������������������������������������������359
Processing Login�����������������������������������������������������������������������������������������360
Successful Login�����������������������������������������������������������������������������������������364
Switching Between Content������������������������������������������������������������������������365
Showing the User’s Name���������������������������������������������������������������������������367

xii
Table of Contents

Logging Out�������������������������������������������������������������������������������������������������������370
Restricting Access��������������������������������������������������������������������������������������������371
Summary����������������������������������������������������������������������������������������������������������374
Coming Up���������������������������������������������������������������������������������������������������������375

Chapter 11: A Configuration System������������������������������������������������377


The ini File Contents�����������������������������������������������������������������������������������������377
Reading an ini File��������������������������������������������������������������������������������������������380
Generating the Configuration Form�������������������������������������������������������������������385
Generating the Config Table������������������������������������������������������������������������������387
Saving the Configuration�����������������������������������������������������������������������������������392
Reading the New Values������������������������������������������������������������������������������392
Writing the ini File���������������������������������������������������������������������������������������394
Using the Configuration Settings����������������������������������������������������������������������396
Resizing Images������������������������������������������������������������������������������������������������399
Adding Configuration to Administration������������������������������������������������������������404
Summary����������������������������������������������������������������������������������������������������������405
Coming Up���������������������������������������������������������������������������������������������������������406

Chapter 12: A Content Management System������������������������������������407


The Blog Pages�������������������������������������������������������������������������������������������������408
The Blog Table���������������������������������������������������������������������������������������������������410
Preparatory Blog Code��������������������������������������������������������������������������������������413
Adding a Single Blog Article������������������������������������������������������������������������������418
Uploading the Optional Image���������������������������������������������������������������������������426
Checking for an Optional Image������������������������������������������������������������������428
Keeping the Optional Image������������������������������������������������������������������������430
Previewing the Image����������������������������������������������������������������������������������437

xiii
Table of Contents

Selecting an Existing Image������������������������������������������������������������������������������439


Selecting an Image from a Menu����������������������������������������������������������������440
Processing the Selected Image�������������������������������������������������������������������445
Previewing the Selected Image�������������������������������������������������������������������448
Selecting an Image from a Radio Group������������������������������������������������������450
Choosing Between Buttons and the Menu��������������������������������������������������455
Redirecting to the Blog List Page����������������������������������������������������������������456
Importing Blog Articles��������������������������������������������������������������������������������������457
Changes to the Insert Code�������������������������������������������������������������������������457
Starting the Import Code�����������������������������������������������������������������������������459
Handling the Import File������������������������������������������������������������������������������459
Copying the Blog Import Files����������������������������������������������������������������������462
Importing the Blog Data�������������������������������������������������������������������������������464
Calling a Function with an Array of Parameters������������������������������������������470
Finishing the Import������������������������������������������������������������������������������������472
Reading a Blog Article���������������������������������������������������������������������������������������473
Preliminary Code�����������������������������������������������������������������������������������������474
Getting the Selected Article ID���������������������������������������������������������������������476
Fetching the Article�������������������������������������������������������������������������������������478
Fixing the Article Line Breaks����������������������������������������������������������������������481
Displaying the Blog Image���������������������������������������������������������������������������483
Managing the Blog Articles�������������������������������������������������������������������������������487
Building the Article Table�����������������������������������������������������������������������������492
Processing the Prepare Events��������������������������������������������������������������������495
Updating an Article��������������������������������������������������������������������������������������504
Deleting an Article���������������������������������������������������������������������������������������507

xiv
Table of Contents

The Visitor Blog List������������������������������������������������������������������������������������������509


Preparatory Code�����������������������������������������������������������������������������������������511
Generating the Blog Article List�������������������������������������������������������������������513
Summary����������������������������������������������������������������������������������������������������������516

Appendix A: Adding Markdown to Your Blog Articles����������������������521

Appendix B: Non-PHP Tricks�������������������������������������������������������������535

Appendix C: PHP Versions����������������������������������������������������������������549

Appendix D: Default Library Functions���������������������������������������������557

Index�������������������������������������������������������������������������������������������������613

xv
About the Author
Mark Simon has been involved in training
and education since the beginning of his
career. He started as a teacher of mathematics
and moved into IT consultancy and training.
He has worked with and trained in several
programming and coding languages and
currently focuses on web development and
database languages. When not involved in
work, Mark can be found listening to or playing
music, reading, or just wandering about.

xvii
About the Technical Reviewers
Jeff Friesen is a freelance software developer
and educator conversant in multiple operating
systems, programming languages, and other
technologies. His specialty is Java Standard
Edition.

Satej Sahu is a Senior Software Data Architect


at the Boeing Company. In his current role,
he is responsible for architecting enterprise
systems with a focus on system integration,
data pipelines, ETL, data architecture,
machine learning, large language models, and
MLOps. He has worked in both service- and
product-based companies in startups and
MNCs and has been in the roles of Principal
Software/Enterprise Architect at Honeywell
and Solution Architect at Adidas previously. He has experience in full-
stack development in PHP environments using enterprise frameworks
leveraging cloud-native solutions. He is a certified AWS Architect and
holds leading industry certifications in a variety of databases, data,
architecture, and Linux environments. He has been an active member
of the Association for Computing Machinery (ACM) since 2013 and is
also the coauthor of the book PHP 8 Basics: For Programming and Web
Development from Apress.

xix
Acknowledgments
Many of the images used for the sample site come from Wikimedia
Commons (https://commons.wikimedia.org/wiki/Main_Page),
while other photographs are my own and can be used freely for the
purpose. The background pattern comes from Pattern Cooler
(https://patterncooler.com/).
The text, alas, is my own, and I am unable to shift the blame.

xxi
Introduction
When the World Wide Web was invented, it was designed for scientists to
share information with other scientists, which is to say it wasn’t meant to
be exciting. Things have, thankfully, come a long way since then.
At the time, pages were statically created—that is, each page was
crafted one at a time. Fortunately, the web server was designed to be
extensible, and it is possible to interface the server with external software.
The standard way to do this is to use the Common Gateway Interface, or
CGI to its friends.
In the early 1990s, Rasmussen Lerdorf developed a collection of
CGI programs for his Personal Home Page. This grew into what became
Personal Home Page Tools (PHP Tools, for short), version 1. Others began
to get involved, and this grew into version 2.
In the late 1990s, Zeev Suraski and Andi Gutmans rewrote things and
developed version 3, renaming it to PHP: Hypertext Preprocessor. This, in
turn, grew into PHP 4, based on the new Zend Engine, named after Zeev
and Andi. PHP 4 is the earliest version mentioned on the official website,
http://php.net.
Since then, things have moved on. PHP 5 improved on the syntax,
overhauled support for Object-Oriented Programming, and introduced
PHP Data Objects, PDO, to access the database. PHP has always been able
to work with databases, but PDO does it in a much more organized way.
PHP 6 was supposed to add a number of new features, in particular
better Unicode support, but it was taking so long that the improvements
were gradually added to PHP 5, and PHP 6 was absorbed into it.

xxiii
Introduction

All of this is, hopefully, ancient history. PHP 5 has long been retired, as
has PHP 7, which vastly improved performance, including a new JIT (Just
in Time) compiler, and added significant improvements to the syntax.
The current version of PHP is version 8 point something, and PHP 7
and below are regarded as old. The main differences you’ll see include
improved performance, more improvements to the syntax, and significant
steps toward better consistency in the language.
This book, of course, works with PHP 8. Unfortunately, a significant
number of web hosts, quite possibly including yours, are slow to make the
move from a previous version, since that risks breaking some sites which
rely on older features. We’ll allow for that by showing alternative code to
work with older versions.
PHP was never originally meant to be a programming language, and it
shows. Coming from a mixed environment results in some features which
are more like other programming languages and some features which
are more like working with a Unix-like shell. Functions have inconsistent
naming and usage, and the whole appears to be a growing collection of
ideas rather than a planned and organized language.
Things are getting better, but it will still drive many experienced
programmers to distraction. Nevertheless, it all makes some sort of sense,
and it’s not hard to get into the spirit of things.

What’s in the Book


This book is all about developing for the Web. At some point, you’re
expected to have access to a web server and, for the data, a database. You
won’t want to experiment with a live server, so Chapter 1 focuses on setting
up your own development server as well as the tools you’ll need to write
and work with your code.

xxiv
Introduction

To get to know the ideas better, we’ll be working on a sample site, the
files for which you’ll download and install. They have the basic outline,
layout, and styles ready to go, and it will be your job to finish the site with
the PHP you’ll be learning.
You’ll then start writing the code. Chapter 2 looks at how you actually
write PHP and how you can organize your code. In the chapter, you’ll get
the basic principles of PHP programming. Chapter 3 puts the coding into
practice and looks at working with a form and sending an email. Forms
will be a fundamental technique working with PHP, as they will enable you
to send data and maintain it on the web server.
Chapter 4 will concentrate on uploading files to the web server. These
will include image files. The images will eventually be used in an image
gallery, so you’ll also look at manipulating these images to get them ready.
A core part of the website is about the data. In Chapter 5, you’ll look at
connecting to the database and getting the database ready for the back end
of the site. From there, you’ll learn how to add data safely in Chapter 6.
In Chapter 7, you’ll look at working with ZIP files and processing CSV
files. This will enable you to do a bulk upload of data and images for the
image gallery.
The image gallery itself is developed in Chapter 8. You’ll develop skills
in reading the database, fetching images, and preparing HTML content
from the database data.
In Chapter 9, you’ll learn about maintaining the data. Not only will you
be inserting and reading data, you’ll also be editing and deleting some of it.
This gives you complete control over the content of the database.
In Chapter 10, the focus is on security. You’ll learn about how PHP
maintains continuity for individual users and how we manage passwords
and logging in. Chapter 11, on the other hand, focuses on configuring
the site and develops a configuration page which manipulates a
configuration file.

xxv
Introduction

In Chapter 12, you do it all again—well, much of it, anyway, but


with some improvements. The goal of the chapter is to develop a simple
blogging system which allows you to add and maintain articles and links
them to new or existing images. It’s a project to consolidate what you’ve
learned in the book.
The blog articles are simple text articles, but you can extend the
blogging system using markdown to produce richer formatting. This is
covered in Appendix A.
The appendixes also cover some of the other techniques used to
enhance the site, as well as some details of the differences in PHP versions.
Finally, there’s a discussion of some of the additional custom functions
used in the book.
You may have come into this with some existing knowledge of
programming, such as with JavaScript or some other language not
connected to web development. On the other hand, you may have had
no programming experience whatsoever. We won’t assume that you have
programming skills, but you will develop them through the book. You’ll
learn not only the specifics of PHP programming but the principles of
programming in general, so you’ll be in a good position to develop your
skills further.

xxvi
CHAPTER 1

Introduction
and Setting Up
Before you get started with learning PHP, you will need to have a working
development environment. Ultimately, PHP will be running from your web
server, either on the Internet or on an intranet. However, for development
(and training) purposes, it makes sense to set up a local web server. This
can be any machine on your network, but it’s very easy to set up everything
you need on your own desktop or laptop computer.
The good thing about web development is that it is culturally agnostic.
It doesn’t matter whether you develop on Macintosh, Windows, or even
Linux: the product will be the same. What does matter is that you have all
the right software in the right place.

For the impatient:


If you’re anxious to get going, you can ignore all of this and skip over
to the “Web Server Setup” section. It’s very important and will make
the rest of the book much easier to work with.

© Mark Simon 2024 1


M. Simon, An Introduction to PHP, https://doi.org/10.1007/979-8-8688-0177-8_1
Chapter 1 Introduction and Setting Up

How the Web Works


When you open a page in your web browser, typically there’s a lot of
activity before you actually see anything. The process is something
like this:
• You enter a URL in your browser’s address bar.

Modern browsers will hide part of the URL—notably


the https:// protocol. However, it’s all there when you
highlight it and copy it.

• The domain name needs to be resolved to an IP


address.

The domain name is something like sample.example.


com. That could be anywhere, so the browser needs to
search for it in the following locations.

• The hosts file is on the browser’s computer and has a


hard-coded list of domain names and IP addresses. The
one you’re looking for is almost certainly not there, but
it doesn’t hurt to look.

• If the hosts file doesn’t have the domain name, the


browser then looks in a distributed database called the
Domain Name System (DNS to its friends).

Presumably, it’s found a matching IP address. If it


hasn’t, you’ll get an error message.

• The browser then makes a connection to the server at


the IP address.
The server may well have many services to run, and
each has been assigned a port number to identify
which service you want.

2
Chapter 1 Introduction and Setting Up

For a live web server, the port number is almost always


port 80, so that’s the default. That may be included in
the URL, but you probably won’t see that. For a testing
or development server, it’s often a different number,
which will need to be specified.

• The web server typically manages multiple sites at the


same IP address in various directories. It then uses
the domain name to choose which directory has what
you want.

The multiple domains on a single server are referred to


as virtual hosts.

• If a page (or some other resource) has been specified,


the server will try to satisfy your request. If it hasn’t,
the server will assume that you want a default page,
typically called index.php or index.html.

Of course, if it can’t be found, you’ll get a response


telling you as much.

• If the request is for a PHP page, which is the whole


point of this book, then the server will pass the file on
to the PHP processor for further processing. You’ll see
more of this later.

The result, with or without additional PHP processing,


will be sent back to your computer, where it’s up to the
browser to work out what to do with it.

For learning and development purposes, we can create a simplified


environment which works the same way, but stays on your own machine.
You’ll see this in the setup section.

3
Chapter 1 Introduction and Setting Up

The Sample Project


For learning purposes, we’ll work on a sample site about Australia, because
why not? You can see what the site will eventually look like here:
https://down-under.net
When you’ve finished the job, it should look like Figure 1-1.

Figure 1-1. The Sample Project Site: Australia Down Under

4
Chapter 1 Introduction and Setting Up

The sample project is what you might call a “brochure site.” If you
can imagine what a color glossy brochure would look like if it was
implemented on the Web, it would be something like this.
Here are some of the things you would expect of a brochure site:

• An introduction to whatever it is the brochure is about

• A contact form to allow visitors to get in touch

• A collection of photos

• A collection of articles

Not much else. We’re not trying to sell anything—managing online


commerce is a tricky and risky business and left to another book. We’re not
starting discussions—that would also involve managing and registering
users as well as moderating user content. Again, best left to another book.
You can quite easily adapt the site to any other context. It could be your
amateur theater group, or hobby group, or advertising your flower shop or
some sort of club. You’ll probably need to put some work into the CSS to
give it a more suitable appearance, of course, but any decent designer can
do that for you.
The point is that you’ll have the skills to generate and maintain the
content of the site in any way you want.

The Tools
Presumably, you’ve already got a computer. It doesn’t matter what the
operating system is, as long as you can install whatever software you need.

5
Chapter 1 Introduction and Setting Up

The main tools you’ll need are

• A working web server, which is described in the next section.

• A web browser. Almost any current web browser will


do, but it’s probably a good idea in web development
to test your results in more than one browser. For PHP
development, we won’t stretch the browser features
too far.

• A good coding editor.

You’ll also need a copy of the sample project, as we’ll describe in the
Web Server Setup section.

A Coding Editor
Most of the files for a website are text files. There are also images and the
database, but all of your actual coding will involve text.
That means you can edit these files with any old text editor. More
realistically, you’ll want to use a text editor designed for writing code. Here
are the features you’ll want:

• There is a view of the project folder, making it easy to


switch between files.

• The coding editor supports syntax highlighting, which


recognizes the type of document you’re editing (HTML,
CSS, JavaScript, etc.) and highlights the parts of the
language.

6
Chapter 1 Introduction and Setting Up

You may already have your preferred coding editor. If you don’t, you
can try one of these:

• Pulsar: https://pulsar-edit.dev/

This is the successor to the Atom Text Editor.

• VSCodium: https://vscodium.com/

This is the open source of Microsoft’s Visual Studio


Code (https://code.visualstudio.com/), without
Microsoft’s specific telemetry and proprietary features.

Both are free, cross-platform, and open source. They also have
additional extension packages available to customize your editor.
If you have another coding editor already, that will do as well.

Web Server Setup


If you’re going to work with PHP, you’ll need to have a web server running
PHP and a database. If you’re doing this on Linux, you’ll probably have
that already, though there’s still going to be some setting up to do.
If you’re doing this on Macintosh or Windows, you’ll need to download
and install a web server for development. Fortunately, it’s easy to install.

If you are doing this on Linux, you may prefer not to use the
preinstalled web server and database for learning or development.
One of the following recommended packages also has a version
for Linux.

7
Random documents with unrelated
content Scribd suggests to you:
Ja muutamana päivänä selvisi hänelle eriskummainen seikka: hän
rakasti
Simone Morangea — aivan yksinkertaisesti sanoen!

»No jopa jotakin!» huudahti hän. »Tästäpä tulee hyvä juttu! Ja


entä tuo entinen…»

Niin, Simonen lemmenseikkailu, se ei suonut hänelle lepoa eikä


rauhaa! Seuraavana päivänä hän havaitsi joutuneensa Morangen
huvilaan, aikomattaan tai nimenomaan päättämättään sinne mennä.
Simone istui yksin kukkasparvekkeella. Hänellä oli kädessään
välipitsi, jonka kaunis köynnös vielä oli kesken… mutta hän ei
virkannut, vaan hänen katseensa oli suunnattu kohden kaukaisuutta,
jonka peitti opaalinhohteisen meren ja helmenvärisen taivaan
erottava sumuviiru. Jacques oli jo jonkun aikaa ollut selvillä siitä, että
tuon nuoren tytön olennossa oli tapahtunut pieni muutos. Hän oli
kenties kauniimpi vielä kuin ennen, vielä viehättävämpi, mutta hän oli
käynyt salaperäisemmäksi. Mitä hän ajattelikaan, istuessaan nyt
aamuhetken kirkkaudessa niin ajatuksiinsa vaipuneena, ettei ollut
kuullut Jacquesin lähestyvän? Nuorukainen tunsi sydäntänsä
kouristavan.

— Ah, huudahti hän melkein tietämättään, — olen vakuutettu, että


te vielä ajattelette häntä…

Simone säpsähti:

— Häntä… ketä sitten? kysyi hän, posket punoittavina.

— Ketä? Tietysti häntä, jota tuo meidän juttumme… tarkoitan


teidän juttunne… koskee! Tiedätte kyllä, mitä tarkoitan!
— Miksi kysytte minulta tuota asiaa?

Hillittömästi huudahti nuorukainen:

— Siksi että olen mustasukkainen.

— Te?

— Niin, juuri minä. Ettei minulla ole siihen oikeutta, se ei lainkaan


muuta asiaa. Olen mustasukkainen ja kärsin — en saa mielestäni
tuota juttua, josta te puhuitte minulle… Antaisin henkeni —
kuuletteko, henkeni — saadakseni tietää — kaikki!

Yhä kiihkeämpi puna levisi Simonen kauniille kasvoille, mutta


kuten hyväntekeväisyysbasaarissakin hän kohotti Jacquesia kohden
silmänsä, joissa hymyili taivaan sini. Nuorukainen olisi halunnut
polvistua hänen eteensä.

— Simone… rukoili hän, tietämättä enää itsekään tuskansa


aihetta.

— Oi, vastasi tyttönen, hymyilevänä vieläkin ja punaposkisena, —


oi kuinka vaikeata, kuinka vaikeata tuo on, jota minulta pyydätte!

Aivan hiljaa, kuin häpeissään, mumisi nuorukainen:

— Te rakastitte häntä?

— Niin kyllä, vastasi tyttö uljaasti.

Jacquesin katse sumeni tuskallisesta mielipahasta… Silloin kertoi


Simone alistuvasti tuon lemmenjuttunsa. Se oli tapahtunut maalla,
edellisenä vuonna. Siellä oli ollut eräs hänen veljensä ystävä, — jo
kolmenkymmenen vuoden vanha.
— Minusta hän oli kuin joku romaanin sankari. Ajattelin vain häntä,
uneksin hänestä. Hänen nimensä huulillani tiedustelin
päivänkakkaroilta hänen tunteitansa. Varmuuden vuoksi laskin aina
etukäteen lehdykät — ja niin vastasi minulle joka kukkanen: »Hän
rakastaa tulisesti!» Ja minä toistin itselleni: »Hän rakastaa minua, se
on totta, hän rakastaa minua!» Sitten romaanisankarini — meni
naimisiin. Saadessani sen tietää tulin niin järkytetyksi, että menetin
tajuntani. Selitettiin, että minua vaivasi verettömyys, ja minua
hoidettiin huolellisesti, — oikea syy pysyi salassa. En ollut samaa
ainesta kuin ystävättäreni, jotka juttelivat huolettomasti
mielitietyistänsä. Olin aina ajatellut: »Rakastan kerran vain — ja
annan rakastetulleni koko sydämeni». Mutta olin vielä niin nuori…
kenties luulin vain rakastavani… enhän silloin tiennyt…

Tuo huokauksin lausuttu tunnustus päättyi niin hiljaiseen


muminaan, että Jacques kumartui lähemmäksi kuullaksensa kaikki.

— Entä sitten? kysyi hän tuskaisesti.

— Sitten? Eihän siinä ollut muuta.

— Mutta entä tuo mies… mitä hän teki, mitä hän puhui?

Nuori tyttö hymyili hämmästyneenä.

— Hän? toisti hän. — Mitä hän sitten olisi voinut sanoa minulle?
Tuo kaikkihan tapahtui vain minun omassa järjettömässä
mielessäni… Eihän hän tiennyt mitään koko asiasta!

Jacques sulki Simonen molemmat kädet omiinsa, pusertaen


huulensa kiihkeästi niitä vastaan. Hän olisi halunnut osoittaa
nöyrtymystään, pyytää anteeksi ja huutaa ilonsa ilmoille! Mutta
Simone ei olisi ymmärtänyt häntä.

— Oi rakkaani, sanoi hän, — muistattehan, ettei kenenkään muun


pitänyt saada tietoa tästä nuoruutenne lemmentarinasta kuin
kihlattunne. Ettekö tahtoisi pysyä tuossa päätöksessänne?

Kuinka mielellään Simone sen teki! Hänen silmiensä hellä hohde,


suloinen liikutus, joka sai hänen huulensa värähtelemään, todisti sitä
tarpeeksi sanoittakin.

— Ettehän ajattele häntä enää koskaan… rakkakin Simone!

— Ja te, Jacques, ettehän te ajattele koskaan… tuota naista?

Hän sai kiihkeän kieltävän vastauksen.

— Kuinka haluaisin muistella häntä!… Oi, rakkaani, jos tietäisit…

Mutta noilla viattomilla huulilla näkyi hieman surumielinen hymyily,


ja hilpeille lapsenkasvoille tuli melkein kokeneen naisen ilme.

— Oi, mumisi pikku Simone, — minusta on sen parempi, mitä


vähemmän siitä tiedän!
RUUSUNEITI.

Kiinnitettyään muutamalla pistoksella vaalean silkkiruusun tummien,


melkein punaisenruskeitten lehtien keskelle, neiti Lise Doucette
kumartui puremaan poikki ompelulangan.

Tässä pienessä ullakkokamarissa ei ollut mitään sunnuntailevon


pakkoa, ja kun tänä lokakuun sunnuntaina kuultiin kellon lyövän
kolme Saint-Germain-des-Prés'issä, lopetti Lise-neiti juuri viidennen
ruusunsa. Neljä edellistä hohti valkoisella paperialustallaan tuoreina
ja raikkaina kuin oikeat kukat, ihmeellisinä ja uhkeina kuin unien
haavekuvat.

Kaukaisina nuoruutensa päivinä, jäätyänsä orvoksi äidistänsä,


sokean isän ja pienen veljen turvaksi, oli Lise-neiti työskennellyt
eräässä noista työpaikoista, jotka valmistavat tuhansittain kukkia
suuria kauppahuoneita varten. Nyt olivat Lise-neidin voimat jo
heikontuneet ja hän työskenteli omin päin kotonansa, valmistellen
mielikuvituksensa tuotteita… taiteellisia vaateruusuja, joihin ei ollut
tarpeen kalliita aineksia eikä työkaluja, vaan ainoastaan taitavat,
varovaiset sormet ja kaunista ja pehmeää silkkiä, monta eri väriä ja
väriasteikkoa: vaaleanpunaista, tummanpunaista, sinipunaista,
keltaista… vieläpä sinistäkin. Lise-neidin ihmeellisessä puutarhassa
eivät sinisetkään ruusut hämmästyttäneet — ne viehättivät
harvinaisuudellaan… Kukkien kupuun sirotteli Lise hiukan
tuoksupulveria… Kaiken naisellisen mielikuvituksensa, kaiken
pariisittaren aistikkaisuuden hän käytti näitä eriskummaisia tuotteita
luodessaan. Niillä ansaitsi hän ilman erinäistä vaivaa elatuksensa,
niin ettei hänen tarvinnut kurjuutta kärsiä.

Saatuaan tuon kauniin kukan valmiiksi, meni Lise-neiti akkunan


luo ja veti sen edestä musliiniuutimet, haluten samalla tavoin
kohottaa tuota harmaata harsoa, jolla sade verhosi esineet. Ottaen
sitten säiliöstä mustepullon ja muutaman arkin paksua sinertävää
paperia — naisten kirjepaperia — hän istuutui pöydän ääreen, mihin
vielä oli jäänyt silkkikankaan huituvia. Mutta hän ei kirjoittanutkaan,
vaan alkoi lukea uudelleen kirjettä, joka oli tullut hänelle viimeisen
edellisellä viikolla ja johon hänen olisi pitänyt vastata.

Tuo kirje alkoi tällä tavoin: »Rakas, viehkeä ystävättäreni, —


miksikä ette ole luonani, näillä ihanilla rantamilla, missä kukkii
sitruuna! Tai miksi en minä saa olla luonanne tuossa vanhassa
goottilaisessa tornissa, jossa te minulle kirjoitatte?! Oi, olen
näkevinäni hennon vartenne, siniset silmänne, vaalean tukkanne…»

Ehdottomasti kohotti Lise-neiti katseensa kirjeestä, suunnaten sen


poikki huoneen — joka ei ollut goottilaista tyyliä — kuvastimeen,
mihin äkkiä ilmaantui kuihtuneet kasvot, lumivalkea tukka…

Kaunis ei Lise-neiti ollut koskaan ollutkaan, mutta olihan hän ollut


nuori, ja nuoruuden raikkaus lähentelee melkein kauneutta. Nyt
olivat hänen silmänsä haalistuneet kuin kuihtuneet lemmikit, ja talvi
oli sirottanut luntansa vaaleaan tukkaan. Nyt oli Lise-neiti vanha.
»Ystävättäreni, te, jota en ole koskaan nähnyt ja joka sentään
olette haaveitteni ainaisena esineenä», — niin jatkui kirje, —
»sallikaa minun kuiskata korvaanne salaisuus, jonka kenties jo olette
arvannutkin… Minä rakastan teitä…»

Lise-neiti ei lukenut pitemmälle, vaan taittoi kirjeen kokoon. Hän


ajatteli tuon seikkailun alkukohtaa. Eräänä päivänä oli hän nähnyt
»Pienen Päivälehden» neljännellä sivulla tämänlaatuisen
ilmoituksen:

»Nuori yksinäisyydestä kärsivä taiteilija haluaa kirjevaihtoon


samassa tilanteessa olevan nuoren tytön kanssa. Nimen
ilmoittamista ei vaadita. Vastaus t. 1. toimistoon N:o 1209.»

Lise-neiti oli säälinyt tuota yksinäistä taiteilijaparkaa ja kuvitellut


mielessään hyväsydämistä nuorta tyttöä, joka vastaisi hänen
ilmoitukseensa. Lisellä oli vilkas mielikuvitus. Heti oli hän ollut
selvillä, että tuon nuoren tytön piti olla kaunis, ylhäinen ja köyhä, että
hän asuisi ylpeän isänsä kanssa vanhassa, puolittain
rappeutuneessa linnassa ja että hänen nimensä olisi Bérengère.
Ruusuja valmistellessaan oli Lise-neiti sommitellut kokoon kauniin
tarinan. Kuten hänen sormensa käsittelivät silkkiä ja musliinia,
askaroivat hänen ajatuksensa lausuttavien mielipiteiden, sanojen
keksinnässä.

Niin oli hän ajankuluksensa kirjoittanut tuon nuoren tytön kirjeen.


Huvin vuoksi oli hän sitten tullut panneeksi sen postiin. Ei aikaakaan
niin tuli vastaus »Päivälehden» välityksellä.

Taiteilija kiitti häntä, oli onnellinen ja ihastunut ja tunsi mielensä


virkistyneeksi. Hän rukoili Bérengèreä jatkamaan kirjeenvaihtoa,
olemaan hänen ystävänsä.
Niin olivat he joutuneet tuohon ihmeelliseen kirjeenvaihtoon, joka
jo oli jatkunut puolen vuoden ajan.

Bérengère kertoi Renélle — tuota mielenkiintoa herättävää nimeä


taiteilija käytti — yksinäisestä ja romanttisesta elämästään,
haaveista, joissa hän eleli harppuaan soitellessaan, lukiessaan
romaaneja, runoutta… kävellessään linnan vanhassa, yksinäisessä
puistossa. Joskin tuon vanhassa herraskartanossa elelevän ylhäisen
neidin haaveet lähinnä muistuttivat vaatimattomassa kamarissaan
asustavan kaksikymmenvuotisen työläisnaisen mielikuvia, niin ei
siitä koitunut haittaa, kun taiteilijallakaan ei liene ollut tarkkoja tietoja
ylhäisen naisen sielunelämästä. Hän puolestansa oli matkoilla. Hän
kirjoitti ystävättärellensä Egyptistä, Kreikasta, Italiasta. Ihastuksen
tunteittensa ohella hän tulkitsi myöskin kunnioitustaan, ystävyyttään.
Ja nyt oli hän puhunut rakkaudestakin…

Kirje oli sisältänyt hellän lemmentunnustuksen… itse asiassa


ensimmäisen, joka hienotunteisesti ja kunnioittavasti oli lausuttu tälle
viisaalle neitsyeelle, — sillä nuoruutensa päivinä oli hän ollut liian
köyhä, liiaksi velvollisuuksien rasittama saadaksensa kosijoita… ja
nyt oli hän jo ehtinyt ohi viidenkymmenen!

Lise-neiti oli hymyillyt. Hän oli tehnyt pilkkaa omasta itsestänsä,


mutta ollut sentään hieman liikutettu, hieman hyvillään. Oliko hän
sitten lukenut tuon rakkaudenkirjeen nuoren tytön uteliain silmin?
Eipä suinkaan, — mutta nuo taikasanat olivat kenties tunkeneet
hänen muinoiseen mieleensä, kaksikymmenvuotiaan mieleen, jonka
elämän huolet eivät olleet sallineet herätä eloon ja jota ei mikään
satuprinssi ollut tullut herättämään…

Valitettavasti oli Lise-neidin mielihyvä ollut varsin lyhytaikaista…


tuskalliset mietteet rupesivat pian vaivaamaan hänen sydäntänsä.
Tuo miesparka rakasti Bérengèreä. Eikö hän ollut velvollinen
ilmoittamaan hänelle, ettei Bérengèreä ollut olemassakaan —
muualla kuin vanhan työläisnaisen haaveellisessa mielessä…

Viikon päivät oli Lise-neiti jo harkinnut tuota asiaa. Tuollaisen


tunnustuksen laatiminen tuntui hänestä niin perin vaikealta. Kenties
hänestä sitäpaitsi tuntui ikävämmältä kuin itse aavistikaan; tehdä
loppu tuosta haavetarinasta, lakata kirjoittamasta Bérengèren
kirjeitä, jäädä Renén viestejä vaille.

Ja tämä lokakuun sunnuntai meni menojansa kuten edellisetkin


päivät, Lise Doucetten tarttumatta kynään. Ja illalla hän tunsi
olevansa niin väsynyt ja alakuloinen, että häntä melkein halutti jättää
siksensä pikettipeli, jolla hän joka ilta kävi huvittamassa vanhaa
ullakkonaapuriansa herra Petitbois'ia. Tuo miesparka, joka oli
hieman kyttyräselkäinen, hieman sairaalloinen, oli pakotettu
enimmäkseen pysyttelemään nojatuolissaan, ja illat kävivät hänestä
pitkiksi. Lise-neitiä vaivasi tuo petos, johon hän oli syypää, ja tuntien
halua uskoa salaisuutensa jollekin hän teki rohkean päätöksen:

»Minä kerron tuon asian herra Petitbois'ille, hän voi antaa minulle
hyvän neuvon.»

Erakkoelämäänsä viettäen oli herra Petitbois lukenut paljon,


ajatellut paljon. Lise-neidistä hän oli erehtymätön arvosteluissaan.
Niinpä ei hän salannutkaan häneltä mitään tuosta myöhään
kokemastaan rakkausjutusta. Käsittäen asiat perin vakavalta
kannalta, ei hän aikonut ollenkaan hangoitella »hyvää neuvoa»
vastaan, jonka hän arveli saavansa tuolta taatulta vilpin viholliselta.
Hän kiiruhtikin jo vakuuttamaan sitä etukäteen.
— Tuo erehdys ei saa enää jatkua, selitti hän. — Kirjoitan tuolle
nuorelle miehelle, että…

Mutta hänen suureksi ihmeeksensä herra Petitbois pudisti


päätänsä tyynesti ja harkitsevasti.

— Mutta… jollei tuo kirjoittaja olekaan mikään nuori mies? lausui


hän hitaasti.

Ja kun Lise-neiti jäi kuuntelemaan suu auki, mieli tykkänään


hämmennyksissä, hän jatkoi:

— Jos ei hän olekaan nuori mies, neiti Lise? Jos hän onkin
päinvastoin kiltti vanha ukkeli? Luonnon lapsipuoli, joka olisi hänkin
halunnut olla kaunis, voittaa rakkautta! — raajarikko, joka on
haaveillut avarasta maailmasta, matkoista… elämän onnea vaille
jäänyt olento, vanhus, jolla ei ole ollutkaan nuoruutta…

Tahtomattaan mumisi Lise-neiti:

— Kuten teillä…

— Kuten minulla, niin juuri.

Silloin heräsi Lise-neidin liikutetussa mielessä epäilys, joka äkkiä


kehittyi varmuudeksi.

— Onko mahdollista, naapurini, huudahti hän, — tuo nuori


taiteilija… olitte te!

— Niin olin, kunnon naapurini! Tuo typerä tuuma tuli päähäni


eräänä päivänä, kun aika kävi minusta ylen pitkäksi… Mutta taivas
tietköön, etten aavistanutkaan…
— Te olitte tuo kirjoittaja! toisti Lise-neiti, yhä vielä päästä pyörällä.
— Nuo kaukaisista maista tulevat kirjeet te kirjoititte muutaman
askeleen päässä ovestani. Te kerroitte Egyptistä ja Kreikasta, istuen
paikoillanne nojatuolissanne!

— Samaten kuin te kerroitte minulle linnastanne… Teidän


kirjeenne, hyvä neiti, olivat viehättäviä!

— Entä sitten teidän! Koulunkäyntimme ei ole mennyt hukkaan!

Herra Petitbois hymyili, hiukan surunvoittoisin ilmein.

— Mitä vanhoja houkkioita me olemmekaan, huokasi hän.

Mutta neiti Lise pani vastaan.

— Emmepä niinkään, hyvä naapuri! Tuo mielikuvituksen pila oli


viehättävää! Elämä oli riistänyt meiltä ilon rakastaa, olla
kaksikymmenvuotiaita. Elämää uhmaten me anastimme itsellemme
sittenkin tuon ilon… kuten vaateruusuni korvaavat minulle
keväimen…

Seurasi hetken vaitiolo. Seinäkello naksutteli hiljaa, säännöllisesti


kuin levollinen sydän.

Palaten sitten todellisuuteen herra Petitbois huudahti:

— Entä meidän pikettimme, Lise-neiti!

Ja niin he pelasivat tavallisen pikettipelinsä — hiukan haaveellinen


ilme katseessaan, valkoisesta tukastansa huolimatta.
HERTTUA JA MARKIISITAR.

(Aihe viuhkamaalauksista.)

Noudattakaamme rakkauden johtoa, se vie meidät oikeaan.

Puuterin ja kauneustäpläin, mielettömäin seikkailujen ja


loppumattomien oikeudenkäyntien aikakautena tapahtui, että
Troncantiquen herttua ja hänen ylhäinen naapurinsa markiisitar de
Souchevieille kävivät juhlallisesti oikeutta erään pähkinämetsän
omistamisesta.

Kumpiko heistä sitten oli oikeassa? Sitä olisi minun varsin vaikea
sanoa, niin sekaisin olivat oikeudenjakajat panneet kaikki asiat. Joko
markiisitar — tai kenties herttua. Jollei toinen, niin toinen, — siihen
viittasivat asiakirjat. Eräs syvämietteinen ihmisvihaaja, La
Rochefoucauld, joka varmaankin oli selvillä tämäntapaisista asioista,
on sanonut että riidat saataisiin aina nopeasti ratkaistuiksi, jos
vääryyttä olisi tapahtunut ainoastaan toiselta puolelta. Kuinka lienee
ollutkaan, — joka tapauksessa oli herttuan ja markiisittaren
oikeudenkäyntiä kestänyt jo melkein seitsemän vuotta.

Sillävälin kuin molempiin linnoihin sateli mitä ihmeellisimmillä


harakanvarpailla täyteentöhrittyjä asiakirjoja, odottivat linnojen
omistajat yhtäläisen pelon vallassa kumpikin oikeusjutun ratkaisua.
Tosin ei oikeuden päätös varmaankaan voinut tuottaa hävinneelle
kovinkaan suurta rahallista tappiota, mutta jos olisi kiistelty vaikka
nuppineulasta, olisi asiallisten kiihko ollut yhtä suuri, sillä
kysymyksessä oli, kummanko turhamielisyys tulisi tyydytetyksi.

Häviämisen pelko kävi lopuksi heidän mielessään niin paljoa


voimakkaammaksi kuin halu voittaa, että he keksivät keinon päästä
erilleen koko asiasta.

Päivänä muutamana Troncantiquen herttua, muka hyvyytensä


ohjaamana, pyysi markiisitarta puolisoksensa, ja tämä suvaitsi
suostua hänen pyyntöönsä, hänkin samaten herttaisen mielensä
taivuttamana. Pähkinämetsä oli tuleva yhteiseksi omaisuudeksi, ja
oikeusjuttu päättyi siten muitta mutkitta.

Tuo kihlaus, joka julkaistiin heti, antoi paljon juoruamisen aihetta


niille, jotka läheltä tai kaukaa olivat seuranneet oikeusjutun kulkua.

Lainvalvojat sulkivat tomuiset asiakirjansa.

»Ihmisparat!» sanoivat he säälivällä ymmärtämyksellä. »Menevät


avioliittoon voidaksensa sopia! Menettelevät samaten kuin tuo
mielipuoli, joka pelosta kastua hyppäsi jokeen!»

Herttuan ja markiisittaren tuttavat kohauttivat hartioitaan sanoen:

»Oikeudenkäynnit tulevat usein kalliimmiksi kuin ennakolta voi


aavistaakaan!»

Mutta syrjäisten mielestä oli asialle keksitty oivallinen ratkaisu.


— Kihlatuilla on, huomautti kreivitär de Languefine, —
kummallakin komea nimi ja uhkea omaisuus, molemmat ovat leskiä,
molemmat vanhoja ja rumia, ja he vihaavat kumpikin toisiansa. Mitä
muita edellytyksiä vaatisikaan enää sopusuhtainen avioliitto!

Mitä itse päähenkilöihin tulee, niin he välittivät varsin vähän noista


juoruista, tyytyivät valittamaan kohtalon vaatimuksia ja lykkäämään
mahdollisimman kauas lupauksensa toteuttamisen.

Kohtelias sulhasmies syytti kaupungissa valvottavia asioitaan,


herttainen morsian vetosi maalla hoidettaviin tehtäviinsä, suoden
siten toisillensa vuodesta vuoteen armonaikaa.

Jotteivät loukkaisi perinnäistapoja, he antoivat juhlallisen


lupauksen kirjoittaa toisillensa, ja niin he erosivat.

*****

Tapaamme Troncantiquen herttuan jälleen eräässä Faubourgin


hotellissa.

Istuen Ludvig XIII:n tyylisessä nojatuolissa, missä seinäverhot


muodostavat uhkean taustan hänen harmaalle tukallensa ja
värittömälle olennollensa, hän avaa hajamielisesti
vaakunakoristeisen kirjeen, joka hänelle juuri on tuotu, luo katseen
noihin naisen käsialalla täyteen kirjoitettuihin arkkeihin ja sanoo:

— Tästä saat työtä, rakas veljenpoikani!

Vanhuksen veljenpoika Cléante seisoo huoneennurkkauksessa,


taustana korkeat lyijyjuotteiset akkunat. Tuo kaksikymmenvuotinen
nuorukainen, jonka katseesta puhuu viattomuus, jolla on vaalea
puuteroitu tukka ja yllään pitsiröyhelöllä koristettu punasininen takki,
luo vanhaan saliin eloa. Veitikkamaisuuden ohella kuvastaa hänen
olentonsa miellyttämishalua, mutta ollen nuortean raikas kuin itse
kevät, hän muistuttaa La Tour'in piirroksia, Marivaux'in kynän
tuotteita.

— Kuinkahan pitkäksi arvoisan ystävättäreni nenä kävisikään, jos


hän tietäisi minkä verran huomiota minä kiinnitän noihin hänen
suuritöisiin epistoloihinsa! lisäsi herra de Troncantique.

Mutta kunnianarvoisen vanhuksen istuessa nenäliinaansa


leyhytellen, Cléante ottaa markiisittaren kirjeen ja lukee sitä syrjässä,
ikäänkuin saadakseen paremmin nauttia siitä tarpeeksensa.

Tuo kirje sisälsi herttaista juttelua, joka saattoi uskomaan, että


rouva de Souchevieille oli säilyttänyt mielensä nuorteuden otsansa
rypyistä huolimatta. Siinä lausuttiin vakavia asioita, kirjoitussävyn silti
käymättä opettavaiseksi, siinä laskettiin leikkiä, pintapuolisuuteen
erehtymättä. Siellä täällä oli jokin syvämietteinen lauselma, väliin
viittaus valtiollisiin oloihin — täytyyhän seurata aikaansa! — mutta
myöskin maalaiselämän kuvauksia, jotka henkivät rakkautta
kotiturpeeseen ja kotoiseen metsään, henkivät vehreyden tuoksua,
jonka olisi luullut Rousseaun teoksista joitakin ihmeellisiä teitä
joutuneen tuon vanhan naisen kirjelehtisiin. Ja lauseet olivat mitä
oivallisimmin muovailtuja, — luontevasti liittyivät niihin kärjekkäät
pilapuheet, viehkeät hellyydenilmaukset. Ihastuttava kerrassaan oli
tuo kirje, joka tuskin vuosisadankaan erottamana rouva de Sévignén
ajoista, kuului ajanjaksoon, jolloin rouva d'Epinay, rouva du Deffand
ja niin monet muut naiset käyttivät kynäänsä Voltairen tai Diderot'in
taituruudella. Cléante nautti joka lauseesta, hengittäen samalla
vaaleanpunaisen paperin sulotuoksua.
Oi, kuinka tuo viehkeä tuoksu olisi mielessä herättänyt kuvan
valkeasta kätösestä, hiukan itsepintaisesta kiharasta, suuresta
pitsiliinasta, joka otsaa kiertäen valui kaulalle, peittäen siveästi
nuoren, sykkivän poven… jollei olisi järkkymättömän varmasti
tiennyt, että kirjoittajatar oli yli kuudenkymmenen ikäinen,
harvahapsinen, keltakätinen vanhus!

Nuorukaisen täytyi nauraa omalle mielettömyydellensä.

— Olenko rakastumaisillani sen ikäiseen henkilöön? mietiskeli


hän. — Ei ole ajateltavissakaan sen suurempaa hulluutta!

— No, kysyi herttua, heräten torkahduksestaan, — oliko


markiisitar yhtä puhelias kuin tavallisesti?

Huolehtimalla tuosta kirjeenvaihdosta teet minulle palveluksen,


jota en ikinä unohda… Jospa voisit vapauttaa minut kaikesta
muustakin!

*****

Souchevieillen linnassa istui markiisitar mukavassa


nojatuolissaan, ympärillään joukko sieviä pikkukapineita ja lukien
»Uutta Héloïsea», samalla kuin hän tuon tuostakin virkistäytyi
nuuskalla, jota hän otti emaljirasiastaan.

Muutaman askeleen päässä hänestä nuori tyttö kirjoitti


ruusunpuisella kirjoituspöydällä. Hän kirjoitti nopeasti, nopeasti,
säestäen lauseittensa välimerkeillä jaoittelua siroilla päänliikkeillä,
ikäänkuin ihaillen noita kauniita mustia piirtoja, jotka
äänettömyydestään huolimatta sanoivat niin paljon. Sitten hänen
ruskeat silmänsä hakevat uusia ajatuksia helmenharmaitten
kirjokangasuudinten poimuista tai rakkaudenjumalista, jotka niissä
keinuvat sinisillä nauhoilla ruusujen keskellä… ja pian jatkaa hänen
kätösensä jälleen työtään.

Hän on kuudentoista vuoden vanha. Kullalla kirjailtu piukka puku


verhoaa hänen hempeää, solakkaa varttansa, ja kaulaa ympäröivä
sametti, leukaan ja vasemman silmän luo asetetut kauneustäplät
vaikuttavat luonnottoman tummilta hänen maidonvalkean ihonsa
rinnalla.

— Taivaan nimessä, Doris, rakas veljentyttäreni, kuinka keksitkään


noin paljon herttualle kerrottavia asioita! kysyi tuo vanha nainen
veltosti.

— Hyvä täti, enhän ole vielä jutellut hänelle niin pitkälti kuin hän
kirjoittaa teille, vastasi Doris hymyillen. — Herttuan täytyy olla teihin
kiihkeästi ihastunut, voidakseen sanoa teille niin viehkeällä tavalla
niin paljon miellyttäviä seikkoja, ja luulen… niin kyllä, minä luulen,
että hän tekee teidät onnelliseksi. Hän on herttainen mieleltään ja
lämminsydäminen!

— Hänellä on ovenvartijan sydän ja hempeä mieli kuin kuskilla,


rakkaani. Oi, jospa hän olisi neljäkymmentä vuotta nuorempi!

Yhä hymyillen, Doris neitonen sinetöi epistolansa, sitten hän


istuutui akkunan luo, katsellen haaveellisena taivaan pilviä… Hän
haaveksi saavansa, kuten täti, helliä kirjeitä… Troncantiquen
herttualta, joka oli neljäkymmentä vuotta nuorempi kuin
todellisuudessa.

*****
Mutta päivä lähestyi, jolloin kirkon siunauksen piti yhdistää
Troncantiquen ja Souchevieillen suvut.

Eräänä aamuna herttua palasi isiensä linnaan ja pyysi saada


odottaa markiisitarta pähkinämetsässä. Siellä halusi hän osoittaa
ylhäiselle morsiamelleen nöyrää ihastustaan.

Tämä vastasi suostuvansa tapaamiseen. Mutta lähestyessään


tuota paikkaa herttua huomasi — oi, mikä suunnaton
vastoinkäyminen! — luuvalon käyvän kimppuunsa ja vanhan arven,
kunniakkaan muiston Fontenoysta, rupeavan tuottamaan tulista
tuskaa.

— Cléante, rakas veljenpoikani, meille ei jää neuvoksi muuta kuin


että sinä menet pähkinämetsään, lausumaan ilmi mielipahani,
huudahti hän.

Nöyrästi lähti nuori mies matkaan.

Ihmeellistä! Hänen sydämensä sykki kiihkeästi, kun hän nyt odotti


saavansa nähdä hänet, joka oli niin usein askarruttanut hänen
mieltänsä. Nuhdellen hiljaa itseänsä tuosta mielettömyydestänsä,
hän ajatteli:

— Hyvä, että tästä tulee loppu. Nähtyäni markiisittaren ovat


haaveeni lopussa. Olen vakuutettu että hänellä on valkoinen tukka ja
että hän on vaivainen varreltansa. En edes hämmästyisi, vaikka hän
ontuisikin.

Cléante oli tuskin ehtinyt pähkinämetsään, kun hän jo näki


tumman puvun lähestyvän toisesta päästä käytävää, minne hän oli
suunnannut katseensa.
— Siinäpä se, ajatteli hän, — vaalea paperi muistutti korkeintaan
kahdeksantoista vuoden ikää, mutta tämä ruskeankeltainen puku
todistaa kuusikymmenvuotiaasta omistajattaresta!

Mutta se hänen täytyi myöntää, että tuo lähestyvä hento olento ei


ollut suinkaan köyryselkäinen eikä käynniltään epävakainen.

Hän lähestyi — lähestyi — tuo vanha nainen!

Onnettomuudeksi hän oli painanut päänsä alas, tarkaten tien kiviä,


eikä hänen kasvoistansa voinut nähdä muuta kuin valkoisen tukan,
joka pilkisti esiin viitan päähineen alta. Hänen tukkansa oli aivan
valkoinen, sitä ei käynyt epäileminenkään!… Kun Cléante vielä ehtisi
nähdä otsan ja poskien rypyt, silmien sammuneen katseen, suun
väsähtäneen ilmeen, silloin hän hymyilisi mielettömyydelleen.

Mutta hänen yhä silmäillessään tulijaa, tuntematon kohotti


päätänsä. Silloin Cléante näki, että hänellä oli loistavat silmät, sileä
iho, kuusitoistavuotiaan huulet… ja etteivät hänen tukkaansa olleet
vaalentaneet vuodet, vaan puuteri.

*****

Muutaman minuutin kuluttua määrätystä kohtaamishetkestä,


markiisitar lähti matkaan pähkinämetsälle, selittäen
päänpakotuksensa hellittäneen. Häntä huvitti piiloutuneena
metsikköön ottaa selkoa, miten tuo epäonnistunut kohtaaminen oli
kehittynyt.

— Mitä onkaan lapsiparka keksinyt sanoa tuolle sietämättömälle


ukkorähjälle! ajatteli hän.
Hiljaa hiipi rouva de Souchevieille läpi viidakon. Siellä hän
pysähtyi hämmästyneenä, luullen näkevänsä haavekuvan. Juuri
samaan aikaan saapui herttua sinne, aukoen oksia kuten hänkin.

Nuo kaksi entistä vihollista katsoivat toisiansa kasvoista kasvoihin,


perin hämmästyneinä. Sitten he kuin yhteisestä sopimuksesta
suuntasivat katseensa metsikön aukealle.

Siellä oli kaikki tyyntä ja nuorteata ja keväistä. Hyönteiset surisivat


puhkeamaisillaan olevien kukkasten ympärillä, jotka täyttivät ilman
sulotuoksullaan, aurinko lähetti pensaitten lomitse säteitään, jotka
loivat hohdetta vihreille lehdille, ja istuen vierekkäin käsi kädessä,
hymyilivät Cléante ja Doris toisillensa.

— Rakastan teitä, Doris… sanoi Cléante.

— Cléante, mumisi Doris, — rakastan teitä…

Ja kukkivissa pähkinäpuissa pitivät linnut laulajaisiansa…

Äkkiä heidän ajatuksensa siirtyivät heidän tärkeään asiaansa,


heidän oikeudenkäyntiä koskeviin etuihinsa.

— Nuo lapsukaiset ovat meidän perillisemme, toimittakaamme


heidät avioliittoon, antakaamme heille tuo pähkinämetsä ja
jättäkäämme muu kaikki siksensä, huudahtivat he yhteen ääneen.

Ja he lisäsivät liikuttavalla yksimielisyydellä:

— Jumalan kiitos — ylpeyteni on pelastettu… eikä minun tarvitse


mennä naimisiin!
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like