【发布时间】:2015-08-28 16:10:58
【问题描述】:
我有一个 php 联系表单,当用户提交表单时,我和用户都会收到一封确认电子邮件。我想做的是生成一个随机确认号,并通过电子邮件发送给用户和我以供参考。这是我必须生成随机数的代码:
<p>Your confirmation number is: <strong><?php echo mt_rand(100000,999999);?></strong>. Keep this for your records.</p>
数字显示在表单上,但我的问题是我不知道如何输出此数字以便可以通过电子邮件发送。请原谅我不是程序员,但我确实有一些非常基本的 php 知识。因此,如果有人可以帮助我解决这个问题,我将不胜感激。
这是我学校网站上的表格:http://www.inrf.uci.edu/facility/services/service-request/
这也是我表单中的一些 php 代码:
// name
if(trim($_POST['contactName']) === '') {
$nameError = '<span class="error">Please enter your name.</span>';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
// email
if(trim($_POST['email']) === '') {
$emailError = '<span class="error">Please enter your email address.</span>';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = '<span class="error">You entered an invalid email address.</span>';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
// body ------------------------------------------
if(!isset($hasError)) {
$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = 'myemail@email.com';
}
$subject = '[Foundry Form] From '.$name;
$body .= "Name: $name \n\n";
$body .= "Email: $email \n\n";
$body .= "Phone: $phone \n\n";
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: . $email';
$headers .= "\r\n" . 'BCC: myotheremail@email.com' . "\r\n";
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
}
?>
<div id="bodyPage" class="clear">
<!------- start body ------->
<section id="contentPage">
<h1 class="title"><?php the_title(); ?></h1>
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div class="thanks">
<p><strong>Thank You for Submitting a Service Request!</strong></p>
<p>An INRF Staff Member will contact you shortly.</p>
<p>If you do not receive a confirmation e-mail within 1-2 business days, you may contact us directly at (949) 824-2819 or <a href="mailto:info@inrf.uci.edu">info@inrf.uci.edu</a>.</p>
<p>We look forward to serving your research needs!</p>
<hr style="border:1px dashed #CCC;" />
<?php
echo "<Strong>Your Service Request information</strong><br>";
echo "(You may print this page for your records)<br><br>";
echo "<u>Contact Information</u><br>";
echo "Name: ".$name;
echo "<br>";
echo "Email: ".$email;
echo "<br>";
?>
</div>
<?php } else { ?>
<?php the_content(); ?>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="error">Sorry, an error occured. See below.<p>
<?php } ?>
<?php if($captchaError != '') { ?><span class="error"><?=$captchaError;?></span><?php } ?>
<script type="text/javascript">var RecaptchaOptions = { theme : 'white'};</script>
<form action="<?php the_permalink(); ?>" id="foundryForm" method="post">
<strong>Contact</strong> <br />
<br />
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><label for="contactName">*Name:</label></td>
<td><input type="text" size="40" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
<?php if($nameError != '') { ?>
<span class="error">
<?=$nameError;?>
</span>
<?php } ?></td>
</tr>
<tr>
<td><label for="email">*Email:</label></td>
<td><input type="text" size="40" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="required requiredField email" />
<?php if($emailError != '') { ?>
<span class="error">
<?=$emailError;?>
</span>
<?php } ?></td>
</tr>
</table>
<br /><br />
<?php
require_once('scripts/recaptchalib.php');
$publickey = "6LfR0eESAAAAAFtSdYmpqqVnVwP8ZMHCr--BIu5f"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
<p><input type="submit"></input></p>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
<?php } ?>
【问题讨论】:
-
代码太多。你能把它限制在实际相关的范围内吗?
-
很抱歉:
-
当两个不同的人得到相同的确认号时会发生什么?
-
我的问题本来是修辞的,但我很好奇从那个范围内选择重复的几率,所以我写了一个小测试程序。事实证明,平均而言,您只需选择 1189 次即可获得重复的数字。这该死的太低了。如果确认号码不是用户必须通过电话说出的东西,那么使用 UUID 之类的东西可能会更好。