I have a contact form in my website rexhin.al where visitors can contact me. It has 3 fields only: name, phone number, and message.
This is my function for sanitizing input data which will be written in a database. I checked the php manual and a few questions here at stackoverflow and I came to this solution. Is this a safe way for sanitizing data? Are the functions in the correct order? Does the order really matter?
spl_autoload_register(function ($class) {
include '../classes/' . $class . '.class.php';
});
/*
beje qe nqs ekziston ip bej update vtm time dhe ++$count te ajo ip;
*/
$db = DB::get_instance();
function sanatize($input) {
$db = DB::get_instance();
//mysqli real escape string for all vars.
//preg_replace for whitespaces, tabs, new lines.
//strip html tags.
//convert html tags.
//strip slashes.
//htmlentities: htmlentities — Convert all applicable characters to HTML entities.
//nuk duhet sepse kemi strip tags.
//trim string
$trimed_string = trim($input);
//filter string using php FILTER_SANITIZE_STRING filter.
$filtered_string = filter_var($trimed_string, FILTER_SANITIZE_STRING);
//remove slashes
$no_slash_string = stripslashes($filtered_string);
//convert special characters to HTML entities
$conv_string = htmlspecialchars($no_slash_string);
//strip html tags
$stripped_tags_string = strip_tags($conv_string);
//replace whitespaces
$filtered_string = preg_replace('#[\s]+#', ' ', $stripped_tags_string);
$safe_string = $mysqli_escaped_string = $db->mysqli->real_escape_string($filtered_string);
return $safe_string;
}
//send message
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(isset($_POST["name"]) && isset($_POST["tel"]) && isset($_POST["message"])) {
$name = sanatize($_POST["name"]);
$tel = intval(sanatize($_POST["tel"]));
//sepse intval ja heq zeron.
$message = trim(sanatize($_POST["message"]));
$time = time();
//name validation.
//only letter and spaces.
if(!preg_match('/^[a-zA-Z\s]+$/', $name)) {
echo "name should contain only letters.";
} else if(strlen($_POST["name"]) < 3) {
echo "name should be three chars min.";
} else if(!preg_match('/^[1-9][0-9]*$/', $tel)) {
echo "your phone number should contain only numbers.";
} else if(strlen($tel) != 9) {
echo "your phone number must be 10 digits.";
} else if(in_array(substr($tel, 0, 3), array(066, 067, 068, 069))) {
echo "your phone number must begin with 066, 067, 068 or 069.";
} else if(strlen($message) == 0) {
echo "message should be 10 letters min.";
} else {
//insert into db.
$query = "insert into `messages` (name, tel, message, time) VALUES ('$name', '$tel', '$message', '$time')";
$db->mysqli->query($query);
echo "sent";
}
}
}