验证URL中的电子邮件地址

时间:2017-04-19 13:04:09

标签: php preg-match email-validation

我将使用内部的电子邮件地址验证此网址。

允许这两个域:

  • https://www.example.com/secure/index.php?ID=john@example.com
  • https://www.example.com/secure/index.php?ID=john@example-test.com

允许在电子邮件地址中@之前的所有名称。

当用户在@之后插入另一个域时,如下所示:

https://www.example.com/secure/index.php?ID=john@gmail.com

他们会收到错误。我怎么能这样做?

3 个答案:

答案 0 :(得分:0)

试试这个:

$email = $_GET['ID']; // remember to filter this!
$regex = '#\w+@(?<domain>\w+\-?\w+\.\w+)#';

preg_match($regex, $email, $matches);
$domain = $matches['domain'];

if ($domain !== 'example-test.com') {
    // Unauthorised
}

查看此处的工作示例https://3v4l.org/SorhQ 如果需要,请参阅正则表达式并进行调整https://regex101.com/r/uDzOzm/1/

答案 1 :(得分:0)

您可以使用简单的爆炸方法来提取域名。看到代码。

$parts = explode("@", "johndoe@domain.com");
$domain = $parts[1];
if(!in_array($domain, array('domain.com')))
{
    //Redirect it wherever you want
}

答案 2 :(得分:0)

你可以这样做:

if (isset($_GET['ID'])) {        
    $domain_name = substr(strrchr($_GET['ID'], "@"), 1);        
    if ($domain_name != "example-test.com"){
        Forbidden....
    }
}
相关问题