NodeWest-Dan
New Member
Has anyone used the "required" tags when making an input/form? Does it prove to be useful? Would it be better to validate with php?
Sent from my iPhone using Tapatalk
Sent from my iPhone using Tapatalk
This. Bolding for importance: never ever ever ever EVER trust the client to do the validation, no matter what you're validating. Client-side validation is purely for user experience purposes (such as instant feedback and marking invalid fields before the form is submitted), not for security.Do your checks on the server side no matter what. You can't trust anything the client sends you. Client side checks are just icing on the cake.
<?php
ob_start();
$name = trim($_POST['name']);
$email = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$site_owners_email = 'email@example'; // Replace this with your own email address
$site_owners_name = 'Example'; // replace with your name
# errors
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "Only letters and spaces are allowed";
}
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}
if (!$error) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = $subject;
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->Body = $comments;
$mail->Send();
$url = "https://www.example.com";
header ("Location: $url");
} # end if no error
ob_end_flush();
?>
You should really use htmlspecialchars rather than htmlentities. htmlentities is known to cause problems with Unicode, even when the encoding parameter is set.Do this, it changes html into entities:
$comments = htmlentities($comments, ENT_QUOTES | ENT_IGNORE, "UTF-8");
That code definitely won't work. $error is never set, so it will always process the form.Any recommendations on a php server side check? I'm not necessarily asking for code, but I can't seem to find much of a guide that I can get to work.
This is what I currently have
<?php
ob_start();
$name = trim($_POST['name']);
$email = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$site_owners_email = 'email@example'; // Replace this with your own email address
$site_owners_name = 'Example'; // replace with your name
# errors
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "Only letters and spaces are allowed";
}
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "Invalid email format";
}
if (!$error) {
require_once('phpMailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->From = $email;
$mail->FromName = $name;
$mail->Subject = $subject;
$mail->AddAddress($site_owners_email, $site_owners_name);
$mail->Body = $comments;
$mail->Send();
$url = "https://www.example.com";
header ("Location: $url");
} # end if no error
ob_end_flush();
?>
What framework would you recommend? I saw phpmailer.Perhaps you could use some framework that handles the form checking more easily.
Myself I use the required tag in addition to server-side check, so the useless data won't reach the server if the client supports the tag. Just as the others said though, never trust the client!