我有这个简单的PHP代码,可生成随机代码并将其显示给用户。如果用户插入显示的代码(如验证码),我需要使用文本输入进行验证,但我将使用此脚本在验证后将用户重定向到注册表单页面。代码已生成,但我无法验证,也许我错过了代码中的某些内容?
NB:这不是垃圾邮件防护系统。正如评论中所说,有一个比这更好的有效解决方案来防止垃圾邮件。这是用户邀请系统的初始草稿,如果您想对此问题进行选择,请考虑这一点。
<?php
function randomCode() {
$code_variable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
srand((double)microtime() * 1000000);
$i = 0;
$rand_Code = '';
while ($i <= 7)
{
$num = rand() % 33;
$tmp = substr($code_variable, $num, 1);
$rand_Code = $rand_Code . $tmp;
$i++;
}
return $rand_Code;
}
$code = randomCode();
if(isset ($_POST['verify'])){
if($_POST['user_code'] == $code ){
echo 'Valid code.';
header('Location: index.php');
}
}
?>
<form method="post" action="">
<input type="text" name="verification_code" disabled value="<? echo $code;?>">
<input type="text" name="user_code">
<button type="submit" name="verify">Verify code</button>
</form>
答案 0 :(得分:0)
每次用户点击验证码按钮时,屏幕上都会显示有效代码。,页面将刷新,新的随机验证码代码将会显示生成并存储在$code
中。这就是$_POST['user_code']
永远不会等于$code
的原因。
一种解决方法是将验证码代码附加到URL本身,以便您可以通过将其与$_GET[...]
数据进行比较来验证用户输入的验证码的真实性。因此,您需要按以下方式更改表单,
<form method="post" action="?captcha=<?php echo $code; ?>">
<input type="text" name="verification_code" disabled value="<?php echo $code;?>">
<input type="text" name="user_code">
<button type="submit" name="verify">Verify code</button>
</form>
随后,以下列方式验证验证码,
// your code
if(isset ($_POST['verify'])){
if($_POST['user_code'] == $_GET['captcha'] ){
// your code
}
}
<强>旁注(S):强>
header(...);
语句之前输出任何内容,否则您会看到标头已发送错误。浏览this SO thread以获取更多相关信息。header(...);
不足以将用户重定向到其他网页,请在exit();
声明后使用header(...);
。答案 1 :(得分:0)
我已经添加了php mail()函数来向用户发送邀请代码。它工作正常,我把代码分成两部分。第一个管理用户邀请代码发送:
<form method="post" action="?invitation=<?php echo $code; ?>">
<label for="email">Email</label>
<input type="text" name="email">
<button type="submit" name="send_invitation">Send invitation code</button>
</form>
<?php
session_start();
ob_start();
if(isset($_POST['send_invitation'])){
function randomCode()
{
$code_variable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789";
srand((double)microtime() * 1000000);
$i = 0;
$rand_Code = '';
while ($i <= 7)
{
$num = rand() % 33;
$tmp = substr($code_variable, $num, 1);
$rand_Code = $rand_Code . $tmp;
$i++;
}
return $rand_Code;
}
$_SESSION['code'] = randomCode();
$_SESSION['tmp_mail'] = $_POST['email'];
if(isset($_POST['verify'])){
$to = $_POST['email']; // this is your Email address
$from = "noreply@domain.com"; // this is the sender's Email address
$subject = "Affiliate invitation code";
$message = $_SESSION['code'] . " " . " is your invitation code.";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
脚本的第二部分在另一个页面上,管理检查存储的代码与$ _SESSION ['code']变量和用户输入之间的代码匹配
<?php
session_start();
ob_start();
function redirect(){
header('refresh:3; url=affiliate/index.php');
exit;
}
if(isset($_POST['verify'])){
if($_POST['user_code'] == $_SESSION['code']){
echo "Valid invitation code. You can now register using $_SESSION['tmp_mail'] email address.";
} else { echo "Wrong code. Please enter a valid code or request a new one."; }
?>
<form method="post" action="?invitation=<?php echo $code; ?>">
<label for="Code">Invitation Code</label>
<input type="text" name="user_code">
<button type="submit" name="verify">Send invitation code</button>
</form>