dmsman Posted November 1, 2022 Share Posted November 1, 2022 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; } Quote Link to comment https://forums.phpfreaks.com/topic/315482-using-an-array/ Share on other sites More sharing options...
ginerjm Posted November 1, 2022 Share Posted November 1, 2022 I would choose a db table instead of an array. That way when you have to make additions or removals it's a simple update to a table rather than a coding exercise. Quote Link to comment https://forums.phpfreaks.com/topic/315482-using-an-array/#findComment-1602152 Share on other sites More sharing options...
kicken Posted November 1, 2022 Share Posted November 1, 2022 Create your array of domains, then use the in_array function to check if $alloweddomain is contained in that array. Quote Link to comment https://forums.phpfreaks.com/topic/315482-using-an-array/#findComment-1602154 Share on other sites More sharing options...
dmsman Posted November 1, 2022 Author Share Posted November 1, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315482-using-an-array/#findComment-1602155 Share on other sites More sharing options...
Barand Posted November 1, 2022 Share Posted November 1, 2022 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/315482-using-an-array/#findComment-1602156 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.