Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
How to tell if a string has been passed through htmlentities in PHP? And it needs html_entity_decode?

We save data in our DB after passing it via htmlentities method to avoid any injection attacks

And while using it back, we do html_entity_decode to get back the original value. In some cases in our code, the htmlentities is done 2-3 times on the same peice of data due to our saving techniques (hard to explain). So we basically want to avoid encoding the same strings multiple times.

Is there a good reliable way to detect if a string has been passed via htmlentities method already?

I guess one way is to check for stuff like " in the ecoded string, but anything more reliable? Any in built method that just tells me if a string is encoded or not?

We are using Laravel, maybe a helper method in there that could help us?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is fundamentally the wrong approach IMO. Store the data raw, as it comes. While it's in the database it can't do you any harm.

Pass it through htmlentities only when you're just about to put it into a HTML document - because that's the only context in which it could be dangerous.

That will save all the encoding and decoding - you simply encode it on demand as necessary.

over 4 years ago · Santiago Trujillo Report

0

As mentioned by ADyson, you're handling the data incorrectly throughout your system.

Laravel's templating engine "Blade" will automatically send your variables through htmlspecialchars() to prevent XSS attacks.

From the documentation:

Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks.

This will protect your site from potential XSS attacks, without you needing to encode/decode your data as you run INSERT/SELECT queries against your database.

Provided you're using Laravel's built-in DB Builder / Eloquent, your SQL queries are also protected automatically:

The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks. There is no need to clean strings being passed as bindings.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!