I want to check the registered users is student by validating the registered email. I have a table which contain multiple education email domain name such as @mmu.edu.my, @taylor.edu.uk and etc. First the controller should get all available name from database and validate the user email. Then will assign different roles according their university. But the question is how to check the email with each of the available domain name in database.
In my controller i had get the registered user email.
$user_email=$user_info->email;
This is example of the email
alex_9237502834@taylor.edu.uk
Is they any reference for me to learn how to validate?
so there are multiple domains i guess? and you need to check the email domain belongs to one of them or not.
so first of all get a query by which you will you will get all the domain.
then
$user_email = explode('@',$entered_email);
$user_email_domain = $user_email[1];
foreach($sql_returned as $sql)
{
if($sql->email == $user_email_domain)
{
do what you want
}
else
{
do something else
}
}
I'd just cycle through the valid domains as an array, like this:
<?php
$validDomains = array('mmu.edu.my', 'taylor.edu.uk');
function validEmail($email, $validDomains) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$email_parts = explode('@', $email); // [0] will be ben, [1] will be mmu.edu.my
// Valid email address. Does it match anything in our domain whitelist?
foreach($validDomains as $validDomain) {
if (strtolower($validDomain) == $email_parts[1]) {
return true;
}
}
}
return false; // No matches
}
var_dump( validEmail('ben@mmu.edu.my', $validDomains) ); // TRUE
var_dump( validEmail('ben@randomdomain.com', $validDomains) ); // FALSE
var_dump( validEmail('clearlynotanemail', $validDomains) ); // FALSE
var_dump( validEmail('jow@taylor.edu.uk', $validDomains) ); // TRUE
First split the email using "@" sign ..
$user_email=$user_info->email;
$splitted = explode('@',$user_email);
if($splitted[1] == "mmu.edu.my")
{
// attach role here
}
elseif(....)
{
}