CodeIgniter Forums
Checking for specific domain in email address? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Checking for specific domain in email address? (/showthread.php?tid=50427)



Checking for specific domain in email address? - El Forum - 03-26-2012

[eluser]Wonder Woman[/eluser]
Hi,

I'm trying to check if a user is registering with the correct email address, it must be of a specific domain... for example [email protected]

I found a script on the web and tried changing it with no luck, can you help me please?

Code:
function _valid_domain($str)
{
list($mailbox, $domain) = split('@', $str);
if (!(checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A'))) {
  $this->form_validation->set_message(__FUNCTION__, "Domain "{$domain}" not found.");
  echo $domain;
  return FALSE;
} else {
  if($domain == "mywebsite.com"){
   return TRUE;
  } else {
   return FALSE;
  }
}
}

Code:
$this->form_validation->set_rules('email', 'Email address', 'trim|required|xss_clean|valid_email|callback__valid_domain');



Checking for specific domain in email address? - El Forum - 03-26-2012

[eluser]Glazz[/eluser]
Something like:

Code:
$email = "[email protected]";
list($user, $domain) = explode('@', $email);
echo $domain;



Checking for specific domain in email address? - El Forum - 03-26-2012

[eluser]Wonder Woman[/eluser]
Yes that would work but I want it on a callback function and it just isn't working Sad

Code:
$this->form_validation->set_rules('email', 'Email address', 'trim|required|xss_clean|valid_email|callback_valid_domain');



Checking for specific domain in email address? - El Forum - 03-26-2012

[eluser]Glazz[/eluser]
Is your callback function getting called ?
Try placing an echo inside the function and see if it shows up, if not, the callback is not working.

Edit:

Your function needs to look something like:
Code:
function _valid_domain($email)
{
list($user, $domain) = explode('@', $email);
if ( $domain == 'mywebsite.com')
{
  return true;
} else {
  return false;
}
}



Checking for specific domain in email address? - El Forum - 03-26-2012

[eluser]Wonder Woman[/eluser]
I have fixed it thanks, didn't realise it had to be in the same Controller. Thanks Smile


Checking for specific domain in email address? - El Forum - 03-26-2012

[eluser]Glazz[/eluser]
Glad you figured it out =)

- g l a z z