Home > Software design >  Allow only mails with @gmail.com ending
Allow only mails with @gmail.com ending

Time:07-20

This Github repository serves to add a dot (.) to a Gmail address and thus register on a site multiple times with random addresses derived from the original.

The code works fine, but it works with any domain (e.g. @house.com)

And I need to limit it to only work with @gmail.com (I tried this in my HTML) <input type="email" pattern="^[a-zA-Z0-9] @gmail\.com$">

But I prefer it to be server side, I have no idea how to do it, I am new in PHP.

Thanks in advance.

PHP Code:

<?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 ><div >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); 
}
?>

CodePudding user response:

<?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 ><div >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';
}
?>

CodePudding user response:

With PHP 8 you can use str_ends_with().

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

Or a prior PHP8 with a classic regex

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

or strpos with a negative offset

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