繁体   English   中英

只允许以@gmail.com 结尾的邮件

[英]Allow only mails with @gmail.com ending

Github 存储库用于将点 (.) 添加到 Gmail 地址,从而使用源自原始地址的随机地址在站点上多次注册。

该代码工作正常,但它适用于任何域(例如@house.com)

而且我需要将其限制为仅与 @gmail.com 一起使用(我在我的 HTML 中尝试过) <input type="email" pattern="^[a-zA-Z0-9]+@gmail\.com$">

但我更喜欢它是服务器端,我不知道该怎么做,我是 PHP 新手。

提前致谢。

PHP代码:

<?php
set_time_limit(0);

if(isset($_POST['email']))
{
    $mail = explode('@', $_POST['email']);
    $email = $mail[0];
    $domain = '@'.$mail[1];
    $email = ltrim($email);
    $domain = ltrim($domain);
    $email = rtrim($email);
    $domain = rtrim($domain);
    $email = stripslashes($email);
    $domain = stripslashes($domain);
    $email = htmlentities($email);
    $domain = htmlentities($domain);
    $res = addDOT($email);
    echo '<div class="box"><div class="title">Total: '.sizeof($res).'</div><textarea type="text">';
    foreach($res as $mcMails)
    {
        echo nl2br($mcMails.$domain).PHP_EOL;
    }
    echo '</textarea></div>';
}

function addDOT($str){ 
    if(strlen($str) > 1)
    {
        $ca = preg_split("//",$str); 
        array_shift($ca); 
        array_pop($ca); 
        $head = array_shift($ca); 
        $res = addDOT(join('',$ca)); 
        $result = array(); 
        foreach($res as $val)
        { 
          $result[] = $head . $val; 
          $result[] = $head . '.' .$val; 
        } 
        return $result; 
    } 
    return array($str); 
}
?>
<?php
set_time_limit(0);

if(isset($_POST['email']))
{
    if(isGmail($_POST['email'])){
     $mail = explode('@', $_POST['email']);
     $email = $mail[0];
     $domain = '@'.$mail[1];
     $email = ltrim($email);
     $domain = ltrim($domain);
     $email = rtrim($email);
     $domain = rtrim($domain);
     $email = stripslashes($email);
     $domain = stripslashes($domain);
     $email = htmlentities($email);
     $domain = htmlentities($domain);
     $res = addDOT($email);
     echo '<div class="box"><div class="title">Total:'.sizeof($res).'</div><textarea type="text">';
       foreach($res as $mcMails)
      {
        echo nl2br($mcMails.$domain).PHP_EOL;
      }
      echo '</textarea></div>';
  }
}

function addDOT($str){ 
    if(strlen($str) > 1)
    {
        $ca = preg_split("//",$str); 
        array_shift($ca); 
        array_pop($ca); 
        $head = array_shift($ca); 
        $res = addDOT(join('',$ca)); 
        $result = array(); 
        foreach($res as $val)
        { 
          $result[] = $head . $val; 
          $result[] = $head . '.' .$val; 
        } 
        return $result; 
    } 
    return array($str); 
}
/**
 * Check if an email is a Gmail address
 * @param string $email The email address to check
 * @return boolean
 */
function isGmail($email) {
    $email = trim($email); // in case there's any whitespace

    return mb_substr($email, -10) === '@gmail.com';
}
?>

使用 PHP 8+,您可以使用str_ends_with()

function isGmail($email) {
    return str_ends_with($email, '@gmail.com');
}

或以前的带有经典正则表达式的 PHP8+

function isGmail($email) {
    return preg_match("/@gmail.com\$/", $email);
}

或带有负偏移量的 strpos

function isGmail($email) {
    $pattern = '@gmail.com';
    return (false !== strpos($email, $pattern, -strlen($pattern)));
}

固定的。 我通过放置第二个条件修改了代码的第一个 if:

if(isset($_POST['email']) and (substr($_POST['email'], -10) == '@gmail.com'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM