For production environments, using a maintained library is often better than writing your own. These usually include checks to identify the card brand (Visa, Mastercard, etc.) before running the Luhn check.
Automated scripts that attempt to validate cards against payment gateways (often called carding scripts) are a significant security threat. To prevent abuse, payment gateways employ several countermeasures: cc checker script php best