Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 41001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:06:43+00:00 2026-05-10T15:06:43+00:00

Earlier today a question was asked regarding input validation strategies in web apps .

  • 0

Earlier today a question was asked regarding input validation strategies in web apps.

The top answer, at time of writing, suggests in PHP just using htmlspecialchars and mysql_real_escape_string.

My question is: Is this always enough? Is there more we should know? Where do these functions break down?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T15:06:43+00:00Added an answer on May 10, 2026 at 3:06 pm

    When it comes to database queries, always try and use prepared parameterised queries. The mysqli and PDO libraries support this. This is infinitely safer than using escaping functions such as mysql_real_escape_string.

    Yes, mysql_real_escape_string is effectively just a string escaping function. It is not a magic bullet. All it will do is escape dangerous characters in order that they can be safe to use in a single query string. However, if you do not sanitise your inputs beforehand, then you will be vulnerable to certain attack vectors.

    Imagine the following SQL:

    $result = 'SELECT fields FROM table WHERE id = '.mysql_real_escape_string($_POST['id']); 

    You should be able to see that this is vulnerable to exploit.
    Imagine the id parameter contained the common attack vector:

    1 OR 1=1 

    There’s no risky chars in there to encode, so it will pass straight through the escaping filter. Leaving us:

    SELECT fields FROM table WHERE id= 1 OR 1=1 

    Which is a lovely SQL injection vector and would allow the attacker to return all the rows. Or

    1 or is_admin=1 order by id limit 1 

    which produces

    SELECT fields FROM table WHERE id=1 or is_admin=1 order by id limit 1 

    Which allows the attacker to return the first administrator’s details in this completely fictional example.

    Whilst these functions are useful, they must be used with care. You need to ensure that all web inputs are validated to some degree. In this case, we see that we can be exploited because we didn’t check that a variable we were using as a number, was actually numeric. In PHP you should widely use a set of functions to check that inputs are integers, floats, alphanumeric etc. But when it comes to SQL, heed most the value of the prepared statement. The above code would have been secure if it was a prepared statement as the database functions would have known that 1 OR 1=1 is not a valid literal.

    As for htmlspecialchars(). That’s a minefield of its own.

    There’s a real problem in PHP in that it has a whole selection of different html-related escaping functions, and no clear guidance on exactly which functions do what.

    Firstly, if you are inside an HTML tag, you are in real trouble. Look at

    echo '<img src= '' . htmlspecialchars($_GET['imagesrc']) . '' />'; 

    We’re already inside an HTML tag, so we don’t need < or > to do anything dangerous. Our attack vector could just be javascript:alert(document.cookie)

    Now resultant HTML looks like

    <img src= 'javascript:alert(document.cookie)' /> 

    The attack gets straight through.

    It gets worse. Why? because htmlspecialchars (when called this way) only encodes double quotes and not single. So if we had

    echo '<img src= '' . htmlspecialchars($_GET['imagesrc']) . '. />'; 

    Our evil attacker can now inject whole new parameters

    pic.png' onclick='location.href=xxx' onmouseover='... 

    gives us

    <img src='pic.png' onclick='location.href=xxx' onmouseover='...' /> 

    In these cases, there is no magic bullet, you just have to santise the input yourself. If you try and filter out bad characters you will surely fail. Take a whitelist approach and only let through the chars which are good. Look at the XSS cheat sheet for examples on how diverse vectors can be

    Even if you use htmlspecialchars($string) outside of HTML tags, you are still vulnerable to multi-byte charset attack vectors.

    The most effective you can be is to use the a combination of mb_convert_encoding and htmlentities as follows.

    $str = mb_convert_encoding($str, 'UTF-8', 'UTF-8'); $str = htmlentities($str, ENT_QUOTES, 'UTF-8'); 

    Even this leaves IE6 vulnerable, because of the way it handles UTF. However, you could fall back to a more limited encoding, such as ISO-8859-1, until IE6 usage drops off.

    For a more in-depth study to the multibyte problems, see https://stackoverflow.com/a/12118602/1820

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Here's an example I found (the way you're obtaining a… May 11, 2026 at 10:00 am
  • added an answer I had the same problem, the solution was to use… May 11, 2026 at 10:00 am
  • added an answer the basic paradigm with wicket+ioc is: most dependencies should be… May 11, 2026 at 10:00 am

Related Questions

Earlier today a question was asked regarding input validation strategies in web apps .
I was going to Ask a Question earlier today when I was presented to
Earlier today I asked a question about environ , and one of the more
I had a discussion with a developer earlier today re identifying TCP packets going
I was thinking earlier today about an idea for a small game and stumbled
Earlier this week I ask a question about filtering out duplicate values in sequence
Earlier I asked this question How to correctly unit test my DAL? , one
Earlier versions of the Notes client would automatically turn a sent URL into a
Earlier I asked a question about why I see so many examples use the
Earlier I asked why this is considered bad: class Example { public: Example(void); ~Example(void);

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.