i have studied arrays a bit more and i realised just having $companyEmail by itself will not print anything but “Array” i need some kind of loop to print them all
anyway i originally wanted to use the array for validation purposes not to be printed
so i tried to add a loop in my code
$companyEmail = array();
$companyEmail[] = 'mytest.com';
$companyEmail[] = 'mytest.co.uk';
$companyEmail[] = 'mytest.org';
$email_regex = "/^[a-zA-Z0-9\\._%+-]+@[a-zA-Z0-9\\.-]+\\.[a-zA-Z]{2,4}$/";
foreach ($companyEmail as $key => $value)
$company_regex = "/@$value$/";
// check the username is allowed (not already taken and valid for whatever)
if (isset($_REQUEST['username'])) {
if (!preg_match($company_regex, $_REQUEST['username']) ||
!preg_match($email_regex, $_REQUEST['username'])) {
$validation_message .= "<li>Username must be a valid $companyName email address.</li>";
$validated = FALSE;
} else if (!check_user_name($conn, $_REQUEST['username'], $DEBUG)) {
$validation_message .= '<li>Username taken please try another.</li> ';
$validated = FALSE;
}
}
now this is where it gets really strange that code above recognises an email i put in with “mytest.org” but does not recognise an email i enter with “mytest.com” or “mytest.co.uk”
so it is recognising array[2] but not array[0] or array[1], basically it always only recognises the last array in the list im quite lost now
<?php
function emailInDomain($email, $domains) {
if (!is_array($domains)) {
trigger_error("Domain list must be supplied as an array.", E_USER_ERROR);
}
if (preg_match("/^([.0-9a-z_-]+)@(([0-9a-z-]+\\.)+[0-9a-z]{2,4})$/i", trim($email), $matches)) {
if (in_array($matches[2], $domains)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
$domains = array("test.com", "test.org");
$email1 = "test@test.org";
$email2 = "test@test.net";
if (emailInDomain($email1, $domains)) {
echo $email1." is a valid email.";
} else {
echo $email1." is an invalid email.";
}
// reports as being a valid email, checked against the supplied domain list.
if (emailInDomain($email2, $domains)) {
echo $email2." is a valid email.";
} else {
echo $email2." is an invalid email.";
}
// reports as being an invalid email, checked against the supplied domain list.
?>
Why use two expressions to determine if the email is valid and if the email domain is in the allowed array?
function emailInDomain($email, $domains) {
if (!is_array($domains)) {
trigger_error("Domain list must be supplied as an array.", E_USER_ERROR);
}
if (preg_match("/^([.0-9a-z_-]+)@(([0-9a-z-]+\\.)+[0-9a-z]{2,4})$/i", trim($email), $matches)) {
if (in_array($matches[2], $domains)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
This function would first validate the supplied email address using preg_match(). If the email is in proper email format, this would then match the domains with $matches[2] which is the domain chunk of the validated email address using in_array($matches[2], $domains) where $domains is a supplied array of all domains allowed by the application.
To add it to your existing code:
$companyEmail = array("test.com", "test.co.uk", "test.org");
// check the username is allowed (not already taken and valid for whatever)
if (isset($_REQUEST['username'])) {
/** replace
* if (!preg_match($company_regex, $_REQUEST['username']) ||
* !preg_match($email_regex, $_REQUEST['username'])) {
*/
## with:
if (!emailInDomain($_REQUEST['username'], $companyEmail)) {
##
$validation_message .= "<li>Username must be a valid $companyName email address.</li>";
$validated = FALSE;
} else if (!check_user_name($conn, $_REQUEST['username'], $DEBUG)) {
$validation_message .= '<li>Username taken please try another.</li> ';
$validated = FALSE;
}
}
this still does not recognise the domain and tells me i need a valid domain. If i take the array part out of the variable it works fine so what is it that makes array behave differently to a normal variable
could it be because the variable is external so in my function i am using global $companyName; to call the variable at the moment, should this still work if the variable is an array?
okay so im trying to figure out why this isnt working, and i tried to print out the array variable and it just prints “Array” instead of the names of the domains i have assigned to it