-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.php
executable file
·100 lines (78 loc) · 2.98 KB
/
helper.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
// Things to notice:
// This script holds the sanitisation function that we pass all our user data to
// This script holds the validation functions that double-check our user data is valid
// You can add new PHP functions to validate different kinds of user data (e.g., emails, dates) by following the same convention:
// if the data is valid return an empty string, if the data is invalid return a help message
// function to sanitise (clean) user data:
function sanitise($str, $connection)
{
if (get_magic_quotes_gpc())
{
// just in case server is running an old version of PHP with "magic quotes" running:
$str = stripslashes($str);
}
// escape any dangerous characters, e.g. quotes:
$str = mysqli_real_escape_string($connection, $str);
// ensure any html code is safe by converting reserved characters to entities:
$str = htmlentities($str);
// return the cleaned string:
return $str;
}
function validateEmail($field)
{
// Remove all illegal characters from email
$field = filter_var($field, FILTER_SANITIZE_EMAIL);
// Check to see if the email address conforms to the expected format
if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
// data was valid, return an empty string:
return "";
} else {
return "Email address is not valid ";
}
}
function validateDOB($field)
{
// parse the supplied date into array to get day, month, year out for checking
$date = date_parse($field);
// use the PHP checkdate() function to confirm the date is valid
if (checkdate($date['month'], $date['day'], $date['year'])) {
//echo "Date is OK";
return "";
} else {
return "Date is not valid";
}
}
// if the data is valid return an empty string, if the data is invalid return a help message
function validateString($field, $minlength, $maxlength)
{
if (strlen($field)<$minlength)
{
// wasn't a valid length, return a help message:
return "Minimum length: " . $minlength;
}
elseif (strlen($field)>$maxlength)
{
// wasn't a valid length, return a help message:
return "Maximum length: " . $maxlength;
}
// data was valid, return an empty string:
return "";
}
// if the data is valid return an empty string, if the data is invalid return a help message
function validateInt($field, $min, $max)
{
// see PHP manual for more info on the options: http://php.net/manual/en/function.filter-var.php
$options = array("options" => array("min_range"=>$min,"max_range"=>$max));
if (!filter_var($field, FILTER_VALIDATE_INT, $options))
{
// wasn't a valid integer, return a help message:
return "Not a valid number (must be whole and in the range: " . $min . " to " . $max . ")";
}
// data was valid, return an empty string:
return "";
}
// all other validation functions should follow the same rule:
// if the data is valid return an empty string, if the data is invalid return a help message
// ...
?>