0

In checkout.php I need to block blacklisted users and deny checkout. The code below I added in PHP script works but needed fine tuning as it is having 2 issues that needed solution:

The topic is different from "Deny checkout if Street Address is on blacklist". Because, I need 2 conditions in each blacklist must be matched before denying checkout.

//Block if First name is in blacklist

$blacklistedfirstname = array("raj", "ken" ,"ber" ); 

foreach ($blacklistedfirstname  as $blacklist_firstname) {
    if (stripos($post['firstname'], $blacklist_firstname) !== false) {
        $error[$type . '_firstname'] = "Oops ! Something went wrong. Contact the administrator";
    }
}

// Block if address_1 in blacklist

$blacklistedaddress = array("1, Vee Street", "2,BB Street",  "street2","street3");

foreach ($blacklistedaddress  as $blacklist_address) {
    if (stripos($post['address'], $blacklist_address) !== false) {
        $error[$type . '_address_1'] = "Oops ! Something went wrong. Contact the administrator";
    }
}

Two issues are: Though it works individually and block if entered values are in blacklist, I face two issues.

Issue 1. The first name condition blocks all names starting with "Raj" as it is in blacklist. for ex. if "Raju" is entered, though the name is different from what is in blacklist, it still blocks. It must block only if "Raj" is entered exactly including lower case.

Issue 2. The address_1 field has values that are in blacklisted address. But I needed to block only if Firstname and address_1 values are both in blacklist. This is because, First name can be same for many people. Also street address can be same for some people. Genuine users must not be blocked. So, this unique combination of First name and address_1 blacklist check will ensure that, the particular user alone is blocked. Also instead of throwing errors independently for these 2 fields, a common error can be added when both Firstname and address_1 field values are in blacklist.

3
  • 1 & 2: Then stop using strpos and compare the complete text rather than bits of it Commented May 19, 2023 at 8:31
  • 1 & 2: Set a flag if you find data in the blacklist. Test both flags after the loops to see if both are set. simples Commented May 19, 2023 at 8:32
  • @ RiggsFolly Thank you. If possible can you please post example code of your method? Commented May 19, 2023 at 11:49

1 Answer 1

0

This question is not standard. You should learn more to use DB and better ways!

But to answer your question:

$blacklistedfirstname = ["raj", "ken" ,"ber"]; 
$blacklistedaddress = ["1, Vee Street", "2,BB Street",  "street2","street3"];

$is_f_bl = in_array($post['firstname'], $blacklistedfirstname);
$is_a_bl = in_array($post['address'], $blacklistedaddress);

if($is_f_bl && $is_a_bl){
  $error = "Oops ! Something went wrong. Contact the administrator";
}

There is no need to do a loop through arrays!

Just use in_array() function which doe's the job of searching in array! this function returns a boolean value: true if needle is found otherwise false.More docs here.

If both fields found in blacklist it will show error!

Sign up to request clarification or add additional context in comments.

12 Comments

@ Alijvhr IT WORKS good. Thank you. But can you please post example code for me on how to use DB, with this same php code parameters, as you said "This question is not standard. You should learn more to use DB and better ways!".
@VSR DB means Database. You can start using w3schools and progress. You can continue every page using next button!
@ Alijvhr I know DB means database. I was asking, how to implement it for the present code. Thank you. I will read it in W3schools.
By using DB for blacklist, please tell me, what is the difference if I am using it in fixed array as blacklist in php code above. Any special advantage using DB for the list. For validation anyways php must be used.
@ Alijvhr. Also why some people wanted to blacklist w3schools as in the Link ? webapps.stackexchange.com/questions/94490/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.