-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm_Sanitation_Validation.php
57 lines (51 loc) · 2.3 KB
/
Form_Sanitation_Validation.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
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html>
<!-- This is more advanced now-->
<head>
</head>
<body>
<form method="post" action="<?php htmlspecialchars($_SERVER['PHP_SELF']) ?>">
<label for="username">Username: </label>
<input name="username" type="text">
<br>
<label for="age">Age: </label>
<input name="age" type="text">
<br>
<label for="email">Email: </label>
<input name="email" type="text">
<br>
<input name="submit1" type="submit" value="Send">
</form>
<hr>
<h1>Output:</h1>
<?php
// PHP code here:
if($_SERVER["REQUEST_METHOD"] === "POST") {
if(isset($_POST["submit1"])) {
// Sanitize = Removes characters
// Validate = Returns empty string if it doesn't pass
// Works best when in combination of both
$sanitized_username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_SPECIAL_CHARS);
$sanitized_age = filter_input(INPUT_POST, "age", FILTER_SANITIZE_NUMBER_INT);
$sanitized_email = filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL);
$validated_age = filter_input(INPUT_POST, "age", FILTER_VALIDATE_INT);
$validated_email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
// filters user input
// 3 arguments:, Input post or input get, variable to filter, types of filter
echo "<h1>Hello {$sanitized_username}, You are {$sanitized_age} years old, You're email is {$sanitized_email}, This is Sanitization</h1>";
echo "<hr>";
if(empty($validated_age)) {
echo "<h1>Invalid Number</h1>";
} else {
echo "<h1>You Are {$validated_age} Years Old</h1>";
}
if(empty($validated_email)) {
echo "<h1>Invalid Email</h1>";
} else {
echo "<h1>You're Email is {$validated_email}</h1>";
}
}
}
?>
</body>
</html>