验证 URL 中的电子邮件地址

Validate an email address in the URL

我将使用其中的电子邮件地址验证此 URL。

允许这两个域:

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

当用户在 @ 之后插入另一个域时,像这样:

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

他们会得到一个错误。我该怎么做?

试试这个:

$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/

您可以使用简单的explode方法提取域名。看代码。

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

你可以做到:

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