我需要您的帮助/建议。
联系表格由HTML和PHP组成。数据验证工作正常,当未填写必填字段时不发送。但是,如果我们输入完整路径,如屏幕截图所示。该表格会发送一封没有任何详细信息的电子邮件(请参阅第二张屏幕截图)。有没有办法防止这种情况发生?
该网站以azure和PHP版本5.6.40托管。
full URL when press enter it sends an email without any information
<!DOCTYPE html>
<html lang="en"><!-- InstanceBegin template="/Templates/template.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="images/favicon.png" type="image/x-icon" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Get in Touch</title>
<!-- InstanceEndEditable -->
<!-- InstanceBeginEditable name="description" -->
<!-- InstanceEndEditable -->
<!-- Bootstrap -->
<link rel="stylesheet" href="css/bootstrap-4.3.1.css" type="text/css">
<!-- SmartMenus jQuery Bootstrap 4 Addon CSS -->
<link rel="stylesheet" href="css/jquery.smartmenus.bootstrap-4.css" type="text/css">
<!--Animate CSS-->
<link rel="stylesheet" href="css/animate.css" type="text/css">
<!-- Fonts -->
<link rel="stylesheet" href="https://use.typekit.net/erh1fsc.css" type="text/css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" type="text/css">
<!--Custom CSS-->
<link rel="stylesheet" href="css/custom.css">
<!--Cookies Consent CSS-->
<!-- <link rel="stylesheet" type="text/css" href="../css/cookieconsent.min.css" />-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Wruczek/Bootstrap-Cookie-Alert@gh-pages/cookiealert.css" type="text/css">
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-1199759-12"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-1199759-12');
</script>
</head>
<body>
<!-- body code goes here -->
<!-- Modal -->
<!-- InstanceBeginEditable name="content" -->
<section class="container py-5 my-5">
<form class="row justify-content-center mb-5" method="post" name="contact form" action="get-in-touch2.php" onsubmit="return validateForm()" role="form" data-toggle="validator">
<div class="col-md-5">
<div class="row">
<div class="col-md-6 form-group mb-3">
<label for="first_name" class="control-label">First name</label>
<input class="form-control" type="text" id="first_name" name="first_name" placeholder="John" required>
</div>
<div class="col-md-6 form-group mb-3">
<label for="last_name" class="control-label">Last name</label>
<input class="form-control" type="text" id="last_name" name="last_name" placeholder="Doe" required>
</div>
</div>
<div class="mb-3">
<label for="email" class="control-label">Your email</label>
<input class="form-control" type="email" id="email" name="email" placeholder="[email protected]" required>
</div>
<div class="mb-3">
<label for="phone_number" class="control-label">Phone number</label>
<input class="form-control" type="text" id="phone_number" name="phone_number" placeholder="Contact Number">
</div>
<div>
<input name="url" type="text" class="website"/>
</div>
</div>
<div class="col-md-7">
<div class="mb-3">
<label for="message">Your message</label>
<textarea class="form-control" rows="8" id="message" name="message" placeholder="Hi there, I would like to ..." required></textarea>
</div>
<div class="text-right">
<button class="btn btn-primary py-2 px-3" type="submit" role="button">Send</button>
</div>
</div>
</form>
<script>
bootstrapValidate('#first_name', 'required:Please fill out this field!')
bootstrapValidate('#last_name', 'required:Please fill out this field!')
//bootstrapValidate('#phone_number', 'required:Please fill out this field!')
bootstrapValidate('#email', 'email:Enter a valid email address')
bootstrapValidate('#message', 'required:Please fill out this field!')
</script>
</section>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery-3.3.1.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/popper.min.js"></script>
<script src="js/bootstrap-4.3.1.js"></script>
<!--Custom Js-->
<script src="js/custom.js"></script><br>
<!-- SmartMenus jQuery plugin -->
<script type="text/javascript" src="js/jquery.smartmenus.js"></script>
<!-- SmartMenus jQuery Bootstrap 4 Addon -->
<script type="text/javascript" src="js/jquery.smartmenus.bootstrap-4.js"></script>
<!-- bootstrap validation -->
<script type="text/javascript" src="js/bootstrap-validate.js" async></script>
<!--Moving Letters -->
<script type="text/javascript" src="https://tobiasahlin.com/js/anime.min.js"></script>
<!--Wow JS-->
<script type="text/javascript" src="js/wow.js" async></script>
<!--Wow Activation-->
<script type="text/javascript" src="js/wow.js"></script>
<script>
new WOW().init();
</script>
<!--Modal Required Items-->
<script>
bootstrapValidate('#firstNameRAD', 'required:Please fill out this field!')
bootstrapValidate('#lastNameRAD', 'required:Please fill out this field!')
bootstrapValidate('#phoneNumberRAD', 'required:Please fill out this field!')
bootstrapValidate('#countryRAD', 'required:Please fill out this field!')
bootstrapValidate('#emailRAD', 'email:Enter a valid email address!')
bootstrapValidate('#messageRAD', 'required:Please fill out this field!')
</script>
<!--Banner Type-->
<script src="js/typed.js"></script>
<script>
var typed = new Typed('#typed', {
strings:["Actionable", "Adaptable", "AI-Driven"],
backSpeed: 100,
typeSpeed: 160,
loop: true,
shuffle: false,
backDelay: 100,
});
</script>
<!--Cookie Consent-->
<script src="https://cdn.jsdelivr.net/gh/Wruczek/Bootstrap-Cookie-Alert@gh-pages/cookiealert.js" async></script>
<!--Video Playback-->
<script>
videoElement.addEventListener('canplaythrough',function(){
this.play();
});
</script>
</body>
<!-- InstanceEnd --></html>
<?php
// Library Inclusion for Azure Websites (DO NOT EDIT)
// Composer must be installed under WebApp Extensions
// SwiftMailler Extension must be installed from WebApp Console
include_once "../vendor/autoload.php";
$formurl = "http://website.com/get-in-touch.html";
$errorurl = "http://website.com/error.html";
$thankyouurl = "http://website.com/thank-you.html";
// Catch Statement for Error Debugging (Do not remove)
try {
/* Sending Message */
$submit = $_POST['submit'];
$firstName = $_POST['first_name'];
$lastName = $_POST['last_name'];
$email = $_POST['email'];
$country = $_POST['country'];
$contact = $_POST['phone_number'];
$message = $_POST['message'];
/*
* Create the body of the message (a plain-text and an HTML version).
* $text is your plain-text email
* $html is your html version of the email
* If the receiver is able to view html emails then only the html
* email will be displayed
*/
// Plaintext Email to be inserted here (REQUIRED)
//$subject = "Website Form";
$text .= 'First Name: '.$firstName.'';
$text .= "\n";
$text .= 'Last Name: '.$lastName.'';
$text .= "\n";
$text .= 'Email Address: '.$email.'';
$text .= "\n";
$text .= 'Contact No.: '.$contact.'';
$text .= "\n";
$text .= "\n --------------------------------------------------------------------- \n";
$text .= 'Message: '.$message.'';
// Insert From email address here
// First is email address followed by User Name
$from = array('[email protected]' => 'Website Contact Form');
// Insert Email recipients address here
// First is email address followed by User Name
$to = array(
'[email protected]'=>'Admin'
);
// Email Subject here
$subject = 'Website Contact Form';
// Login credentials (DO NOT EDIT)
$username = '[email protected]';
$password = 'p@ssword';
// Setup Swift mailer parameters (DO NOT EDIT)
$transport = Swift_SmtpTransport::newInstance('smtp.office365.com', 587, 'tls');
$transport->setUsername($username);
$transport->setPassword($password);
$swift = Swift_Mailer::newInstance($transport);
// Create a message (DO NOT EDIT)
$message = new Swift_Message($subject);
// Attach the body of the email (DO NOT EDIT)
$message->setFrom($from);
$message->setBody($html, 'text/html');
$message->setTo($to);
$message->addPart($text, 'text/plain');
// Sends out message if successful
if ($recipients = $swift->send($message, $failures))
{
// This will let us know how many users received this message
echo "Message has been sent";
header("Location: $thankyouurl");
}
// Error Message if not successful (DO NOT REMOVE)
else
{
echo "Something went wrong - ";
print_r($failures);
}
}
// Shows the Exceptions from the Try Catch Statement (DO NOT REMOVE)
catch(Exception $e) {
echo $e->getMessage();
}
# spam protection
// if the url field is empty
if(isset($_POST['url']) && $_POST['url'] == ''){
// then send the form to your email
mail( '[email protected]', 'Contact Form', print_r($_POST,true) );
}
// otherwise, let the spammer think that they got their message through
?>
您需要检查php中的表单提交。
这可以通过]实现>
if (isset($_POST['submit'])) { //put your try catch code here . }
在上面的代码中,只有在单击提交按钮之后,才会发送电子邮件。
原因可能是JavaScript验证由于某些原因而无法正常工作。但是,您需要在PHP(在服务器端)上验证表单,以防止未经验证就处理该表单。