PHP / MySQL函数导致变量,电子邮件检查

时间:2014-05-31 15:57:41

标签: php mysql function email

如何使用其他功能检查电子邮件的格式是否正确([符号数] @ [符号数]。[符号数])以及使用其添加时是否唯一insertUser? (所有信息都从.txt文件中读取)。

代码:

class User{

    function randomPassword($length = 8){
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }


    function insertUser($firstName, $lastName, $email){
    global $DB;
    $DB->query("INSERT INTO `users` (`fname`, `lname`, `password` , `email`) values ('$firstName', '$lastName', '" . randomPassword() . "', '$email') ");   

    }

在索引文件中:

$USER = new User(); 

     $data = readData();
     foreach($data as $nr => $user){
        $USER->insertUser($user['first_name'], $user['last_name'], $user['email']);
    }

2 个答案:

答案 0 :(得分:1)

请致电$this->randomPassword();

电子邮件验证

function checkEmail($email) {
  if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])
  ?*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$email)){
    list($username,$domain)=split('@',$email);
    if(!checkdnsrr($domain,'MX')) {
      return false;
    }
    return true;
  }
  return false;
}

答案 1 :(得分:1)

要插入随机字符串,您应该这样做:

将insertUser函数更改为:

function insertUser($firstName, $lastName, $email){
global $DB;
$DB->query("INSERT INTO `users` (`fname`, `lname`, `password` , `email`) values ('$firstName', '$lastName', '" . $this->randomPassword() . "', '$email') ");   

}