验证电子邮件时,随机数会生成两次

时间:2019-05-27 23:31:17

标签: php email-verification

我是PHP新手。我目前正在进行电子邮件验证。我的代码应生成一个随机数,然后通过电子邮件发送给用户,并在用户输入时进行验证。

这是我的代码:

<?php

require 'PHPMailer/PHPMailerAutoload.php';

session_start();

// initializing variables
$email = $_SESSION ['email'];
$user_code = "";
$errors = array();

// generate a four digit random number
$gen_code = strval (rand (10000, 99999));

// send code to user email

// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'register');

// REGISTER USER
if (isset($_POST['email_confirm'])) {

  // receive all input values from the form
  $user_code = mysqli_real_escape_string($db, $_POST['code']);

  // check whether both codes match
  if ($user_code != $gen_code) { array_push($errors, "The codes do not match"); }
  else {
    // set isConfirmed == true
    if (count($errors) == 0) {
        $query = "UPDATE user_database SET isConfirmed = true WHERE email = '$email'";
        mysqli_query($db, $query);
        $_SESSION['email'] = $email;
        header('location: user_details.php');
    }
  }
}
?>

这里email_confirm是我的提交按钮的名称,code是文本框的名称。

第一次加载页面时一切正常。我收到一封带有随机整数的电子邮件。

单击提交按钮时问题开始。我收到另一封具有不同编号的电子邮件,并且我已经输入的号码与我从电子邮件收到的号码不相同。

请帮助

1 个答案:

答案 0 :(得分:0)

如果这是一个更简单且实验性的应用程序,则应在将gen_code发送到用户确认电子邮件后不久将其存储在此会话中。否则,将代码存储在db中,并在应用程序收到电子邮件确认POST请求时将其检索,并将用户发送的代码与会话或db的存储位置进行比较。

            public class Method {
                public int[][] getBool(int[][] array, int y, int x, int y2, int x2){
                    int[][]map=array;
                    map[y][x]=5;

                    int[][]right,left,down,up;
                    right=left=down=up=new int[20][34];

                    int counter = 0;

                    if  (map[y2][x2]==5) {
                        for (int i=0; i<map.length; i++){
                            for (int k=0; k<map[0].length; k++){
                                    System.out.print(map[i][k]+" ");
                            }
                            System.out.println(" ");
                        } 
                        System.out.println("\n\n\n");
                        return map;
                    }
                    Method r = new Method();
                    if  (map[y][x+1]==1) {
                        counter++;  
                        right=r.getBool(map, y, x+1, y2, x2);
                    }
                    if  (map[y][x-1]==1) {
                        counter++;
                        left=r.getBool(map, y, x-1, y2, x2);
                    }
                    if  (map[y+1][x]==1) {
                        counter++;
                        down=r.getBool(map, y+1, x, y2, x2);
                    }
                    if  (map[y-1][x]==1) {
                        counter++;
                        up=r.getBool(map, y-1, x, y2, x2);
                    }

                    if  (counter==0) {
                        int[][]temp=new int[20][34];
                        return temp;
                    }

                    Method c = new Method();

                    if  ((c.check(right)<=c.check(left))&&(c.check(right)<=c.check(up))&&(c.check(right)<=c.check(down)))
                        return right;
                    else if ((c.check(left)<=c.check(right))&&(c.check(left)<=c.check(up))&&(c.check(left)<=c.check(down)))
                        return left;
                    else if ((c.check(up)<=c.check(right))&&(c.check(up)<=c.check(left))&&(c.check(up)<=c.check(down)))
                        return up;
                    else
                        return down;
                }
                public int check(int[][]check) {
                    int counter = 0;
                    for (int i=0; i<check.length; i++){
                        for (int k=0; k<check[0].length; k++){
                                if(check[i][k]==5)
                                    counter++;
                        }
                    }
                    if  (counter==0)
                        counter = Integer.MAX_VALUE;
                    return counter;
                }
            }