Check Email Matches Specified Domain(s) Or Reject

Discussion in 'PHP' started by Jordan260, Feb 3, 2013.

  1. #1
    Currently my email accepts all email domains. I would like to define which ones are allowed out of a list (solent.ac.uk, herts.ac.uk) rather than public ones such as @gmail.com etc. Whichever way is easiest for now, i'll try alternative ways for now, this is more just temporary.

    I am just editing a script I bought and wanted this small change. I know it must require more than just the code I produced below to be edited but thought i'd start with something.

    This is part of my registration.php page:

    <form method="post" action="<?php url_for('register'); ?>">[/SIZE]
     
    [SIZE=3]  <!--Email-->[/SIZE]
    [SIZE=3]  <p class="email field">[/SIZE]
    [SIZE=3]    <input tabindex="1" id="email" type="text" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" placeholder="email@example.com" autofocus />[/SIZE]
    [SIZE=3]    <a href="<?php url_for('login'); ?>" tabindex="2" class="action button register" title="Click to log-in with your existing account.">Login</a>[/SIZE]
    [SIZE=3]  </p>[/SIZE]
    [SIZE=3]
    HTML:


     
    Jordan260, Feb 3, 2013 IP
  2. inviz

    inviz Member

    Messages:
    5
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    43
    #2
    in your url_for('login'); use

    
    if(strpos(@solent.ac.uk, $_POST['email']) {
        // Continue email script
      }else{
      echo 'Sorry, you can't register with that email address!';
    }
    
    Code (markup):
     
    inviz, Feb 3, 2013 IP
  3. Jordan260

    Jordan260 Active Member

    Messages:
    201
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #3
    I ended up using this:

    $whitelist = array('solent.ac.uk', 'herts.ac.uk');
    list(, $domain) = array_pad(explode('@', $email, 2), 2, '');
    if (!in_array($domain, $whitelist)) {
    $app->flashNow('error', __('login_invalidemail'));
    PHP:
    Is it possible to require just the .ac.uk part instead of example.ac.uk. I want to allow all university emails only but obviously it would take hours finding every single university name and its extension ().

    Would be quicker to just define/restrict to:
    .ac.uk
    .edu
    and so on...
     
    Last edited: Feb 3, 2013
    Jordan260, Feb 3, 2013 IP
  4. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #4

    That can be a bit tricky in knowing what to do with subdomains. For instance, the following code produces the following output:

    <?php
     
    var_dump(getDomainExtension('example.an.cd'));
    var_dump(getDomainExtension('subdomain.example.an.cd'));
     
    function getDomainExtension($domain) {
        return implode('.', array_splice(explode('.', $domain), 1));
    }
    PHP:
    string(5) "an.cd"
    string(13) "example.an.cd"
    Code (markup):
     
    Alex Roxon, Feb 4, 2013 IP