Skip to content

Commit dcf67a8

Browse files
committed
Add warnings and sanitization to user input example
1 parent e262842 commit dcf67a8

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

real-life-examples/user-input.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ layout: page
33
title: User Input
44
parent_title: Real life examples
55
permalink: /real-life-examples/user-input.html
6-
modification_time: 2015-08-05T12:00:28+00:00
6+
modification_time: 2025-05-13T16:54:28+02:00
77
---
88

99
These scripts allow you to present a form to the user, who can enter text and upload an image; these are displayed first
1010
in the browser, with the option to create a PDF file from the output. These scripts should only be considered the basis
1111
of a full script and will need adapting considerably. In particular, note that the uploaded image files may need to be
1212
deleted at some point.
1313

14+
<div class="alert alert-danger" role="alert" markdown="1">
15+
**Warning:** All user input passed to mPDF should be sanitized properly.
16+
17+
Examples below serve only as a preview what can be done and must not be used as such.
18+
19+
Also, for purposes of these examples, note that the file upload mechanics were simplified and do not solve data validation and/or verification.
20+
</div>
21+
1422
`example_userinput.php`
1523

1624
```html
@@ -49,15 +57,16 @@ if (($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image
4957

5058
$html = '<html>
5159
<body>
52-
<div>' . $_POST['text'] . '</div>
53-
<img src="' ."../tmp/" . $_FILES["file"]["name"] . '" />
60+
<div>' . htmlspecialchars($_POST['text']) . '</div>
61+
62+
<img src="' ."../tmp/" . htmlspecialchars($_FILES["file"]["name"]) . '" />
5463

5564
<form action="example_userinput3.php" method="post" enctype="multipart/form-data">
5665
<textarea style="display:none" name="text" id="text">'
57-
. $_POST['text']
66+
. htmlspecialchars($_POST['text'])
5867
. '</textarea>
5968
<input type="hidden" name="filename" id="filename"
60-
value="'. $_FILES["file"]["name"].'" />
69+
value="'. htmlspecialchars($_FILES["file"]["name"]) .'" />
6170
<input type="submit" name="submit" value="Create PDF file" />
6271
</form>
6372
</body>
@@ -77,8 +86,8 @@ $mpdf = new \Mpdf\Mpdf();
7786

7887
$html ='<html>
7988
<body>
80-
<div>'.$_POST['text'].'</div>
81-
<img src="' . "../tmp/" . $_POST['filename'] . '" />
89+
<div>' . htmlspecialchars($_POST['text']).'</div>
90+
<img src="' . "../tmp/" . htmlspecialchars($_POST['filename']) . '" />
8291
</body>
8392
</html>';
8493

0 commit comments

Comments
 (0)