我有一个非常基本的PHP网站,我想要一个简单的垃圾邮件保护来阻止垃圾邮件提交。 我发现one,我喜欢这是一个基本的4字符输入。易于阅读,占用空间小。垃圾邮件保护需要在PHP窗体 - 不知道如何实现
但它说要使用validate.php提交操作。 我目前的表单行为是调用实际包含在页面加载(<?php include 'includes/mailer.php'; ?>
)中的mailer.php(<form id="contact-form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" class="validate-form">
)。
我可以有两个'行动'吗?如果不是,我该如何实现这个验证码的使用?
当我尝试在结束?>
前加入刚刚在mailer.php开幕<?php
后session_start(); if(isset($_POST["captcha"])&&$_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"]) { echo "Correct Code Entered"; //Do you stuff
然后} else { die("Wrong Code Entered"); }
,整个网站只显示“输入错误的代码”的负担。
编辑:
我无法和理解,我需要把代码的各个部分如何调整它,所以它与现有的邮件脚本工作。
我未修改的index.php主要由以下部分组成:
<?php
include 'includes/mailer.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/bootstrap-responsive.css">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<section id="section-home">
<header>
<!-- header content -->
<!-- navigation -->
</header>
</section>
<section class="banner full-width-container">
<div class="container">
<!-- other body content -->
<div id="contact">
<div id="contact-form-message"><?php print $output; ?></div>
<form id="contact-form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" class="validate-form">
<p><span style="color:#f7403a;">Fill in the form below to have an expert contact you</span></p>
<div class="form-left">
<div class="control-group">
<label for="name" class="assistive control-label">Name:</label>
<div class="controls">
<input type="text" name="name" id="name" value="Your Name" class="replace-default required type-text" />
</div>
</div>
<div class="control-group">
<label for="email" class="assistive control-label">Email: </label>
<div class="controls">
<input type="text" name="email" id="email" value="Your email address" class="replace-default required type-text" />
</div>
</div>
</div><!-- end form-left -->
<div class="form-right">
<div class="control-group">
<label for="subject" class="assistive control-label">Subject: </label>
<div class="controls">
<input type="text" name="subject" id="subject" value="Subject" class="replace-default required type-text" />
</div>
</div>
<div class="control-group">
<label for="telephone" class="assistive control-label">Telephone number </label>
<div class="controls">
<input type="text" name="telephone" id="telephone" value="Your phone number" class="replace-default required type-text" />
</div>
</div>
</div><!-- end form-right -->
<div class="control-group">
<label for="message" class="assistive control-label">Message: </label>
<div class="controls">
<textarea name="message" id="message" class="replace-default required type-textarea full-width" rows="5" cols="20">The type of enquiry (e.g. Motor Accident) and a brief message</textarea>
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" id="submit" name="submit" class="btn btn-stacks" value="Send Message"/>
<div id="sending-message"><img src="img/ajax-loader.gif" alt="" /></div>
</div>
</div>
</form>
</div>
</div>
</section>
<footer class="full-width-container" id="footer-section">
<div class="container">
<!-- footer content -->
</div>
</footer>
<!-- ============================================== -->
<script src="js/modernizr-1.7.min.js"></script>
<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script src="js/jquery.flexslider-min.js"></script>
<script src="js/waypoints.min.js"></script>
<script src="js/jquery.scrollTo-1.4.3.1-min.js"></script>
<script src="js/custom.js"></script>
</body>
</html>
而且我未修改mailer.php包括:
<?php
function cleanInput($input){
$input = trim($input);
if(strlen($input)>0){
$input = htmlspecialchars(stripslashes($input));
}
return $input;
}
$name = '';
$email = '';
$subject = '';
$message = '';
$telephone = '';
$output = '';
if (isset($_POST['submit']) || isset($_GET['ajax']) == 'true'){
//set up for form fields
$name = cleanInput($_POST['name']);
$email = cleanInput($_POST['email']);
$subject = cleanInput($_POST['subject']);
$telephone = cleanInput($_POST['telephone']);
$message = cleanInput($_POST['message']);
$output ='';
$regex = "/^([a-z0-9\\+_\\-]+)(\\.[a-z0-9\\+_\\-]+)*@([a-z0-9\\-]+\\.)+[a-z]{2,6}$/ix";
//do some basic validation
if($name == '' || $name == 'Full Name'){ $output = '<li class="alert alert-error">Please enter your name.</li>'; }
if (!preg_match($regex, $email) || $email == 'Email address') {
$output .= '<li class="alert alert-error">Please check that your email address is valid</li>';
}
if($subject == '' || $subject == 'Subject'){ $output .= '<li class="alert alert-error">Please enter a subject</li>'; }
if($telephone == '' || $telephone == 'Contact number'){ $output .= '<li class="alert alert-error">Please enter a contact number</li>'; }
if($message == '' || $message == 'Your Query'){ $output .= '<li class="alert alert-error">Please enter a message</li>'; }
//if there are errors, add them to the list here
if ($output!=''){
$output = '<div class=""><ul class="unstyled">' . $output . '</ul></div>';
}
//if no errors, try to send the form
else {
/*Put the email address to send to here*/
$to = "[email protected]";
$headers = 'From: [email protected]' . "\r\n";
$headers .= 'Cc: '. $email . "\r\n";
$headers .= 'Bcc: [email protected]' . ', ' . '[email protected]' . ', ' . '[email protected]' . "\r\n";
$subject = $subject;
$body = "Name: $name\n\n"
. "Email: $email\n\n"
. "Subject: $subject\n\n"
. "Message: $message"
;
$messageOK = (mail($to, 'Web Enquiry from the landing page for: ' . $subject, $body, $headers));
//check if the mail was sent or not
if ($messageOK){
$output = '<div class="alert alert-success"><p>Thank you for getting in touch. We will be in contact soon.</p></div>';
}
else {
$output = '<div class="alert alert-error"><p>We could not send your message. Please try again.</p></div>';
}
}
//if ajax is being used, output the message
if (isset($_GET['ajax']) == 'true'){
print $output;
}
}
?>
的任何信息,可以帮助我了解什么是需要使用此验证码将不胜感激
为什么不把该代码放在'mailer.php'的顶部? if条件的第一个参数是检查验证码变量的帖子,如果刚刚在页面上加载的话,肯定不会发生。此外,你可以用'$ error ='代替'die('错误的代码输入')'代码不正确!';',然后在验证码图像后面,你可以执行'echo isset($ error)? $ error:'';'。只需在顶部声明'$ error = null'。 – Ohgodwhy 2014-09-04 23:55:40
好吧,“//你的东西”是否意味着我的表单邮件的其余部分在那之后和'其他'之前?另外,当你说“稍后在验证码图像之后”时,我不确定你指的是哪个位置。你在说HTML格式吗?与“只需在顶部声明$ error = null”相同......声明如何?什么顶部?对不起 - 我对PHP没有最好的理解。 – 2014-09-05 00:20:13