Feed aggregator
Microsoft Admits GitHub Hosted Malware That Infected Almost a Million Devices
Read more of this story at Slashdot.
Feds Link $150M Cyberheist To 2022 LastPass Hacks
Read more of this story at Slashdot.
Volkswagen Bringing Back Physical Buttons, Says Removing Them Was a Mistake
Read more of this story at Slashdot.
How the AI Talent Race Is Reshaping the Tech Job Market
Read more of this story at Slashdot.
AI Isn't Creating New Knowledge, Hugging Face Co-Founder Says
Read more of this story at Slashdot.
Thousands of Freed Scam Center Workers Now Trapped in Overcrowded Detention Centers
Read more of this story at Slashdot.
Zuckerberg's Meta Considered Sharing User Data with China, Whistleblower Alleges
Read more of this story at Slashdot.
CodeSOD: Where is the Validation At?
As oft stated, the "right" way to validate emails is to do a bare minimum sanity check on format, and then send a verification message to the email address the user supplied; it's the only way to ensure that what they gave you isn't just syntactically valid, but is actually usable.
But even that simple approach leaves places to go wrong. Take a look at this code, from Lana.
public function getEmailValidationErrors($data): array { $errors = []; if (isset($data["email"]) && !empty($data["email"])) { if (!str_contains($data["email"], "@")) { $error["email"] = "FORM.CONTACT_DETAILS.ERRORS.NO_AT"; } if (!str_contains($data["email"], ".")) { $error["email"] = "FORM.CONTACT_DETAILS.ERRORS.NO_DOT"; } if (strrpos($data["email"], "@") > strrpos($data["email"], ".")) { $error["email"] = "FORM.CONTACT_DETAILS.ERRORS.NO_TLD"; } } if (isset($data["email1"]) && !empty($data["email1"])) { if (!str_contains($data["email1"], "@")) { $error["email1"] = "FORM.CONTACT_DETAILS.ERRORS.NO_AT"; } if (!str_contains($data["email1"], ".")) { $error["email1"] = "FORM.CONTACT_DETAILS.ERRORS.NO_DOT"; } if (strrpos($data["email1"], "@") > strrpos($data["email1"], ".")) { $error["email1"] = "FORM.CONTACT_DETAILS.ERRORS.NO_TLD"; } } if (isset($data["email2"]) && !empty($data["email2"])) { if (!str_contains($data["email2"], "@")) { $error["email2"] = "FORM.CONTACT_DETAILS.ERRORS.NO_AT"; } if (!str_contains($data["email2"], ".")) { $error["email2"] = "FORM.CONTACT_DETAILS.ERRORS.NO_DOT"; } if (strrpos($data["email2"], "@") > strrpos($data["email2"], ".")) { $error["email2"] = "FORM.CONTACT_DETAILS.ERRORS.NO_TLD"; } } if (isset($data["email3"]) && !empty($data["email3"])) { if (!str_contains($data["email3"], "@")) { $error["email3"] = "FORM.CONTACT_DETAILS.ERRORS.NO_AT"; } if (!str_contains($data["email3"], ".")) { $error["email3"] = "FORM.CONTACT_DETAILS.ERRORS.NO_DOT"; } if (strrpos($data["email3"], "@") > strrpos($data["email3"], ".")) { $error["email3"] = "FORM.CONTACT_DETAILS.ERRORS.NO_TLD"; } } return $errors; }Let's start with the obvious problem: repetition. This function doesn't validate simply one email, but four, by copy/pasting the same logic multiple times. Lana didn't supply the repeated blocks, just noted that they existed, so let's not pick on the bad names: "email1", etc.- that's just my placeholder. I assume it's different contact types for a customer, or similar.
Now, the other problems range from trivial to comical. First, the PHP function empty returns true if the variable has a zero/falsy value or is not set, which means it implies an isset, making the first branch redundant. That's trivial.
The way the checks get logged into the $error array, they can overwrite each other, meaning if you forget the "@" and the ".", it'll only complain about the ".", but if you forget the ".", it'll complain about not having a valid TLD (the "NO_DOT" error will never be output). That's silly.
Finally, the $errors array is the return value, but the $error array is where we store our errors, meaning this function doesn't return anything in the first place. And that means that it's a email validation function which doesn't do anything at all, which honestly- probably for the best.
[Advertisement] Keep all your packages and Docker containers in one place, scan for vulnerabilities, and control who can access different feeds. ProGet installs in minutes and has a powerful free version with a lot of great features that you can upgrade when ready.Learn more.Oscar-Winning Movie Criticized for Using AI To Correct Dialects
Read more of this story at Slashdot.
New Open Source Windows-Compatible Operating System Released
Read more of this story at Slashdot.
Adafruit Successfully Automates Arduino Development Using 'Claude Code' LLM
Read more of this story at Slashdot.
That Galaxy Next Door? It's Home to a Monster Black Hole
Read more of this story at Slashdot.
Can Ants Teach Us How to Program Self-Driving Cars?
Read more of this story at Slashdot.
'I Used to Teach Students. Now I Catch ChatGPT Cheats'
Read more of this story at Slashdot.
Professor Ends Bulwer-Lytton Bad Writing Contest After 43 Years
Read more of this story at Slashdot.
Stem Cell Therapy Trial Reverses 'Irreversible' Damage to Cornea
Read more of this story at Slashdot.
Mice Give First Aid
Read more of this story at Slashdot.
Will an 'AI Makeover' Help McDonald's?
Read more of this story at Slashdot.
How Your Gut Influences Your Brain
Read more of this story at Slashdot.
America's Justice Department Still Wants Google to Sell Chrome
Read more of this story at Slashdot.