Jump to content

Using an array


dmsman

Recommended Posts

I need to use PHP on my WordPress site to restrict who can submit a specific form. I only want to allow those using a specific email domain to be able to submit a form. Below is the code I am using. The code works. The issue is I have 400+ domains that I need to whitelist. I am hoping to use an array but I don't know if this is the best way. Bottom line is I am hoping someone can help adjust the code below to show me how to add additional domains. In the code below, the only domain currently whitelisted is example.com.

add_filter('frm_validate_field_entry', 'check_valid_email', 10, 3);
function check_valid_email($errors, $posted_field, $posted_value){
 if($posted_field->id == 7){
     $postedemail=$_POST['item_meta'][7];
	  $alloweddomain = explode('@',$postedemail)[1];
	 if (!filter_var($postedemail, FILTER_VALIDATE_EMAIL) || $alloweddomain != 'example.com') {
       $errors['field'. $posted_field->id] = 'Sorry! You are not allowed to submit this form';
   }
 }
return $errors;
}
Link to comment
Share on other sites

I should have mentioned that I don't know how to code PHP. Formidable Forms provided the original PHP code which I tweaked to work my specific forms. @kicken, I like what you're saying however I don't know what code to write. If this requires a paid project, I'm happy to do that however I need guidance and almost copy-and-paste code.

Link to comment
Share on other sites

The easiest way to maintain your list of whitelisted domains is just to list them in a plain text file, say, whitelist.txt

domain1.com
domain2.com
domain3.com
...
domain400.com

Then use the file() function to load them into your whitelist array.

<?php
    $whitelist = file('whitelist.txt', FILE_IGNORE_NEW_LINES);
    
    if (in_array($userDomain, $whitelist)) {
        // use the domain
    }
    else {
        // reject domain
    }
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.