我正在尝试实施代码来验证公司电子邮件。当用户输入公司和工作电子邮件时,他们都应该彼此兼容。例如,如果在QUT工作的用户在他/她作为QUT进入公司时向系统注册,那么电子邮件域必须是@ qut.edu.au。下面的代码显示了我实施的方法。但由于某种原因,代码中存在一个逻辑错误,其中包含"您必须输入有效的电子邮件"。(假设在公司名称未包含在域中时触发)。但它每次都会弹出它运行。任何帮助将受到高度赞赏。谢谢!
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/abp/core/init.php';
include 'includes/head.php';
include 'includes/navigation.php';
$email = ((isset($_POST['email']))?sanitize($_POST['email']):'');
$email = trim($email);
$password = ((isset($_POST['password']))?sanitize($_POST['password']):'');
$password = trim($password);
$company_name = ((isset($_POST['company_name']))?sanitize($_POST['company_name']):'');
$company_name = trim($company_name);
$errors = array();
**$domain = array_pop(explode('@', $email));**
if($_POST){
// form validation
if(empty($_POST['email']) || empty($_POST['password'])){
$errors[] = 'You must provide email and password.';
}else {
//validlate email
**if (strpos( $domain, $company_name) !== true) {**
$errors[] = 'You must enter a valid email.';
}else{
// check if email exist in the databse
$query = "SELECT * FROM users WHERE email=?";
$stmt = $db->prepare($query);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->store_result();
答案 0 :(得分:1)
您的代码读取
if(strpos(something, something) !== true) {
error message
}
strpos
永远不会只返回true
int或FALSE
,因此您总会收到错误消息。它也在docs。
正确的版本是:
if(strpos($haystack, $needle) === false) {
//errormessage
}
(另外,在评论中大量提及将电子邮件地址与公司名称匹配的概念并不是一个好主意)