【问题标题】:Validate an email address in the URL验证 URL 中的电子邮件地址
【发布时间】:2017-09-15 17:37:58
【问题描述】:

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

这两个域是允许的:

  • 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

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

【问题讨论】:

  • 1) 请更详细地解释您要达到的目标。 2)添加您尝试过的代码或至少您尝试过的方法

标签: php preg-match email-validation


【解决方案1】:

试试这个:

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

【讨论】:

  • 很高兴能帮上忙!
【解决方案2】:

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

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

【讨论】:

  • 他的问题有一个 preg_match 标签,他正在寻找一个正则表达式解决方案
【解决方案3】:

你可以做到的:

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

【讨论】:

    猜你喜欢
    • 2014-06-12
    • 1970-01-01
    • 2018-03-13
    • 2011-09-02
    • 2013-02-20
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    相关资源
    最近更新 更多