forked from bradtraversy/php-crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_sanitizing_inputs.php
49 lines (42 loc) · 1.63 KB
/
11_sanitizing_inputs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/* --- Sanitizing Inputs -- */
/*
Data submitted through a form is not sanitized by default. We have methods to sanitize data manually.
*/
if (isset($_POST['submit'])) {
// $name = $_POST['email'];
// $email = $_POST['email'];
// htmlspecialchars() - Convert special characters to HTML entities
// $name = htmlspecialchars($_POST['name']);
// $email = htmlspecialchars($_POST['email']);
// filter_var() - Sanitize data
// $name = filter_var($_POST['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
// $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
// filter_input() - Sanitize inputs
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
// FILTER_SANITIZE_STRING - Convert string to string with only alphanumeric, whitespace, and the following characters - _.:/
// FILTER_SANITIZE_EMAIL - Convert string to a valid email address
// FILTER_SANITIZE_URL - Convert string to a valid URL
// FILTER_SANITIZE_NUMBER_INT - Convert string to an integer
// FILTER_SANITIZE_NUMBER_FLOAT - Convert string to a float
// FILTER_SANITIZE_FULL_SPECIAL_CHARS - HTML-encodes special characters, keeps spaces and most other characters
} ?>
<!-- Pass data through a form -->
<!-- php_self can be used for xss -->
<form action="<?php echo htmlspecialchars(
$_SERVER['PHP_SELF']
); ?>" method="POST">
<div>
<label>Name: </label>
<input type="text" name="name">
</div>
<br>
<?php echo $email; ?>
<div>
<label>Email: </label>
<input type="email" name="email">
</div>
<br>
<input type="submit" name="submit" value="Submit">
</form>