Guide 03 / 4
PHP workshop · À table !
Produce a list and a recipe page from PHP arrays, search on the server side and check a form without yet saving any data.
Reproduce a working result, test it, then succeed at an exercise without copying.
01 · Building the dynamic pages
The five complete PHP files are provided at the bottom of the workshop. Also copy style.css from the CSS course into public/.
Step 1 · Preparing the folders
Keep the static version. Create public/ and includes/. Put each complete file at its exact path. The public pages load the functions and the data with require.
Example
mini-blog/
├── public/
│ ├── index.php
│ ├── recette.php
│ ├── proposer.php
│ └── style.css # copie du cours CSS
└── includes/
├── functions.php
└── recettes.php
Do not load app.js on this version: the PHP form makes a new request and its elements differ from those of the JavaScript course.
Step 2 · Running and reading a list
Start the PHP server from mini-blog/ then open index.php. The data in includes/recettes.php feeds the cards. Change a recipe's title in this array then reload.
Example
php -S 127.0.0.1:8000 -t public
Expected result: three cards, correct durations and a link to each recipe page.
Step 3 · Searching and opening a recipe
Search soupe via the GET form. Observe ?q=soupe in the address. Click the recipe to open its page and find the ingredients and steps as lists.
Example
index.php?q=soupe → 1 carte
recette.php?id=1 → Soupe de carottes
recette.php?id=999 → réponse 404
recette.php?id=abc → réponse 400
Expected result: the requested title matches the identifier; no PHP warning appears on screen.
Step 4 · Validating a submission
Open proposer.php. Send a title and a duration. The page displays either the errors or a confirmation. The submission does not yet join the list of recipes.
Example
Titre : Compote de pommes
Durée : 15
Attendu : proposition valide, aucun enregistrement.
The exercise performs no persistent write; sessions and the CSRF token will be introduced in the PDO workshop that really saves.
02 · Practising and checking the server
Test the expected cases and unexpected inputs.
Exercise · Find the quick recipes
In a copy of index.php, add a filter that keeps recipes of 20 minutes at most. Combine it with the title filter already there.
Example
Attendu sans mot-clé : Soupe de carottes et Salade de tomates.
Solution · Filtering the array
Insert this processing after the title filtering and before the PHP closing that precedes the doctype.
Example
$recettes = array_filter($recettes, function (array $recette): bool {
return $recette['duree_minutes'] <= 20;
});
The original list in recettes.php does not change: you only prepare the data for the current response.
Check · Text must not become HTML
Example
Entrée : <strong>Soupe</strong>
Durée : 20
Attendu : le texte est affiché littéralement.
You escape at display time, not before validating or before storing.
Check · Do not depend on the HTML form
With the browser tools, temporarily remove required, min or minlength and send an empty title or a negative duration. The PHP checks must still refuse the submission.
Example
Titre : a → erreur
Durée : -2 → erreur
Durée : 2.5 → erreur
?q[]=soupe → aucun avertissement PHP
The browser tools let you test your own project; server-side validation remains the reference.