☰ Year plan · Communication Technology

The AND gate on a web page

Week 10 · Unit 2 Topic 3: AND gate simulation as a PHP web page: design, code and explanation, the four cases · 3 lessons
3

Tracing four cases, and making the page safer

ICT Secondary 3 Term 1, Unit 2, Topic 3: pp. 32–39
GoalTrace the four cases of the AND page, then make the page safer by accepting only the values on and off.
New words
allowed list · the values the page accepts; anything else is replaced by a safe valuein_array · checks whether a value is in a list=== · compares two values strictly: same value and same typetrace · to follow the code with given inputs and write down the result
Explanation

Now trace the page with the four choices and also with no Apply at all. The lamp is lit only for on and on. The page works, but it trusts what it receives. A browser can send any text, not only the options we offered: a visitor, or another program, could send maybe or ON. The page copies the text without checking, so the result depends on text we never planned for. A safer version keeps a list of the allowed values, on and off. If the received value is in the list it is used, otherwise it is replaced by off. The check uses === so that only exactly on and off are accepted, and ?? gives an empty text when a value is missing. The function lamp does all this and returns the two accepted choices and the lamp. One more slip of the book’s page is worth knowing: if the lamp itself is a clickable picture, clicking it sends the form without the Apply button’s name, so the page forgets the choices and shows the start state.

Value receivedThe safer page uses
onon
offoff
maybeoff
ONoff
(missing)off
Worked examples
list1list2The page says
offoffThe lamp is unlit.
onoffThe lamp is unlit.
offonThe lamp is unlit.
ononThe lamp is lit.
(no Apply)(no Apply)The lamp is unlit.
The four cases and the first visit, as a table of what the page shows. Run on the page of this lesson.
Php
1function lamp(array $post): string {
2    $allowed = ["on", "off"];
3    $first = "off";
4    $second = "off";
5    $state = "unlit";
6    if (isset($post['apply'])) {
7        $first = in_array($post['list1'] ?? "", $allowed, true) ? $post['list1'] : "off";
8        $second = in_array($post['list2'] ?? "", $allowed, true) ? $post['list2'] : "off";
9        $state = ($first === "on" && $second === "on") ? "lit" : "unlit";
10    }
11    return "$first $second $state";
12}
13
14echo lamp([]), "\n";
15echo lamp(['apply' => 'Apply', 'list1' => 'on', 'list2' => 'on']), "\n";
16echo lamp(['apply' => 'Apply', 'list1' => 'on', 'list2' => 'off']), "\n";
17echo lamp(['apply' => 'Apply', 'list1' => 'maybe', 'list2' => 'on']);
Output
off off unlit
on on lit
on off unlit
off on unlit
The safer function. The output lines show the first choice, the second choice and the lamp, for a first visit, for on and on, for on and off, and for the text maybe in the first list.
Read the last output line: the text maybe is not on the allowed list, so it is replaced by off, and the lamp stays unlit even though the second list said on.
Class activity

Trace the safer function on paper for these calls: Apply with list1 = off and list2 = on; Apply with ON and ON; Apply with only list2 = on; and a call with no Apply. Write the returned text for each.

Practice
1
Why does the first visit show an unlit lamp?
2
Which operator does the safer function use to compare values?
3
What does the safer function return for off and off with Apply pressed?
4
Why does the page not trust the lists?