0

ello There, i want to ask, how can i customize this function to allow only certain email domain.

like for instance A & B wants to register,
A email = test@ymail.com
B email = test@onlyalloweddomain.com

how can i validate that?

here's my function

function validate_values() {
        // Create the array which contains the Language variable
        $error = array();

        // Define the Language variable for each type of error
        if($this->verify_if_user_exist() !== 0) {
            $error[] .= 'user_exists';
        }
        if($this->verify_if_email_exists() !== 0) {
            $error[] .= 'email_exists';
        }
        if(empty($this->username) && empty($this->password) && empty($email)) {
            $error[] .= 'all_fields';
        }
        if(strlen($this->password) <= 2) {
            $error[] .= 'password_too_short';
        }
        if(!ctype_alnum($this->username)) {
            $error[] .= 'user_alnum';
        }
        if(strlen($this->username) <= 2 || strlen($this->username) >= 33) {
            $error[] .= 'user_too_short';
        }
        if(!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
            $error[] .= 'invalid_email';
        }
        if($this->verify_captcha() == false) {
            $error[] .= 'invalid_captcha';
        }

        return $error;
    } 

THANKS!!

4
Contributors
10
Replies
68
Views
3 Years
Discussion Span
Last Post by Tpojka
Featured Replies
  • You can use bunch of php inbuilt functions. Here is an example of [preg_match()](http://www.php.net/manual/en/function.preg-match.php). if (preg_match('|@onlyalloweddomain.com$|', $registrantMailAddress; { // continue with registration } I found it on [sof](http://stackoverflow.com/questions/6416027/php-preg-match-for-ending-tag). Read More

  • 1
    cereal 1,524   3 Years Ago

    Not even because, according to the [RFC3696](http://tools.ietf.org/html/rfc3696#section-3) an email like this one: test\@mydomain.com@evil.org is still a valid email address. Read More

  • isset(in_array(end(explode('@', $mail)), array('disallowed.tld', 'notwanted.tld'))) ? 'noPe' : 'yuP'; //or if (in_array(end(explode('@', $mail)), array('disallowed.tld', 'notwanted.tld'))) { echo 'We don\'t want your registration here.'; } else { // define('ADMIN', $mail); } Read More

1

You can use bunch of php inbuilt functions. Here is an example of preg_match().

if (preg_match('|@onlyalloweddomain.com$|', $registrantMailAddress;
{
    // continue with registration
}

I found it on sof.

0

Why not try this out?

<?php
    $denied_hostnames = array("gmail.com", "yahoo.com");

    foreach ($denied_hostnames as $hn)
    {
        if (strstr($_POST['email'], "@" . $hn))
        {
            $errors[] = "Sorry bud! We don't accept " . $hn . " email addresses";
        }
    }

You could also do:

<?php
    $allowed_hostnames = array("mydomain.com", "mydomain.net");

    $good_hostname = false;

    foreach ($allowed_hostnames as $hn)
    {
        if (strstr($_POST['email'], $hn))
        {
            // Makin' it true here! :P
            $good_hostname = true;
        }else{
            if ($good_hostname==false)
            {
                // Keepin' it false here!
                $good_hostname = false; // Set it to false anyway
            }
        }
    }

    if (($good_hostname==true)==false)
    {
        $errors[] = "denied_email";
    }

Edited by matrixdevuk: Added new method

0

For my last post, you may aswell remove the worthless 'if'.

<?php
    $allowed_hostnames = array("mydomain.com", "mydomain.net");

    $good_hostname = false;
    foreach ($allowed_hostnames as $hn)
    {
        if (strstr($_POST['email'], $hn))
        {
            // Makin' it true here! :P
            $good_hostname = true;
        }else{
            // TIMEEEEEE TO REST!
        }
    }

    if (($good_hostname==true)==false)
    {
        $errors[] = "denied_email";
    }

Hope this helped. If it did, I'd appreciate a rate up.

0

@matrixdevuk be careful with this solution, because strstr() will check only for the first occurence of the searched pattern, so if I submit something like this:

mydomain.net@evil.org

it will pass this check.

Docs: http://php.net/strstr

1

Not even because, according to the RFC3696 an email like this one:

test\@mydomain.com@evil.org

is still a valid email address.

Votes + Comments
Helped me with better understanding of mailing services
0

I actually meant to put "@" . $hn in my post.
You could easilly check how many @'s there are in the string, or better yet, remove all \'s.

0

Hi ignnniter, just a suggestion: strpos can lead to the same problem of the strstr approach, because it will check only for the first occurrence.

Tpojka suggested preg_match which is a better solution because you can use a pattern to return the last at-sign segment in the email i.e. the domain part. Here's an example:

<?php

$whitelist = array(
        '@some.tld',
        '@mine.tld'
    );

function checkMailDomain($mail, $whitelist)
{
    if(preg_match('/@(?!.*@).*+/', $mail, $matches) == 1)
    {
        return in_array($matches[0], $whitelist) ? true:false;
    }

    return false;
}

$mail = 'example\@some.tld@evil.org';
echo checkMailDomain($mail, $whitelist) === true ? $mail .' is allowed': $mail .' is not allowed';

$mail = 'example@mine.tld';
echo checkMailDomain($mail, $whitelist) === true ? $mail .' is allowed': $mail .' is not allowed';

It will output:

example\@some.tld@evil.org is not allowed
example@mine.tld is allowed

The above pattern /@(?!.*@).*+/ works like this: match the at-sign and all the following text that is not followed by another at-sign.

Reference: http://www.rexegg.com/regex-disambiguation.html

Bye!

Edited by cereal

2
isset(in_array(end(explode('@', $mail)), array('disallowed.tld', 'notwanted.tld'))) ? 'noPe' : 'yuP';
//or
if (in_array(end(explode('@', $mail)), array('disallowed.tld', 'notwanted.tld')))
{
    echo 'We don\'t want your registration here.';
}
else
{
    // define('ADMIN', $mail);
}
Votes + Comments
good
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.