我不是一個PHP程序員,但用它觸摸,足以把一個接觸的形式。不過,我想添加一個驗證碼字段,現在的作品,但形式不驗證它 - 所以它提出無論什麼PHP,試圖驗證碼添加到現有的形式
任何人可以幫助嗎?如果代碼是不好意思亂,在此先感謝
的,如果我的網頁
<?php session_start() ?>
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['name']) == '') {
$hasError = true;
} else {
$name = trim($_POST['name']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email'])))
{
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$message = stripslashes(trim($_POST['message']));
} else {
$message = trim($_POST['message']);
}
/*captcha 2*/
if(isset($_POST["captcha"])) {
$hasError = true;
} else {
if($_SESSION["captcha"]==$_POST["captcha"]) {
}
}
//CAPTHCA is valid; proceed the message: save to database, send by e-mail ...
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'email address'; //Put your own email address here
$emailTo = 'email address'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nMessage:\n $message";
$headers = 'From: website form <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' .
$email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
Code in the form:
[php]<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for contacting us. Your email was successfully sent and we will be in touch with you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
<div>
<p>
<label for="name">Name</label><br />
<input type="text" name="name" value="" id="name" class="required">
</p>
</div>
<div>
<p>
<label for="email">Email</label><br />
<input type="text" name="email" value="" id="email" class="required">
</p>
</div>
<div>
<p>
<label for="subject">Subject</label><br />
<input type="text" name="subject" value="" id="subject" class="required">
</p>
</div>
<div style="margin-bottom:25px;">
<p>
<label for="message">Message</label><br />
<textarea rows="5" name="message" value="" id="message" class="required"></textarea>
</p>
</div>
<div style="margin-bottom:25px;">
<img src="captcha.php" alt="captcha image">
<p>
<label for="captcha">(antispam code, 3 black symbols)</label><br />
<input type="text" name="captcha" maxlength="3" id="captcha" class="required">
</p>
</div>
<input type="submit" value="Send Message" name="submit" />
</form>
[/php]
請問您的文件有調用session_start()?沒有它,$ _SESSION ['catpcha']將是空的(並且可能會發出通知)。此外,請注意,您應該檢查存儲的驗證碼值是否不爲空。否則,用戶可以簡單地通過不加載圖像來通過驗證碼。 if(!empty($ _ SESSION ['captcha'])&& $ _SESSION ['captcha'] === $ captchaFromUser){valid} – Corbin 2011-12-20 19:56:03
檢查會話存儲的captcha值是否爲空?這是我上面發佈的代碼。順便說一句,$ captchaFromUser將是$ _POST ['captcha']。避免直接訪問$ _POST值而不檢查它們是否被首先設置通常會更好。如果他們沒有設置,那麼你會得到一個通知。 – Corbin 2011-12-20 20:02:30
對不起,我之前沒有看過代碼,所以我已經替換if(isset($ _ POST [「captcha」])) with if(!empty($ _ SESSION ['captcha'])&& $ _SESSION ['captcha'] === $ captchaFromUser) – user1108564 2011-12-20 20:08:24