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 1053991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:20:11+00:00 2026-05-16T17:20:11+00:00

I would like to clarify what is the proper way to filter user input

  • 0

I would like to clarify what is the proper way to filter user input with php. For example I have a web form that a user enters information into. When submitted the data from the form will be entered into a database.

My understanding is you don’t want to sanitize the data going into the database, except for escaping it such as mysql_escape_string, you want to sanitize it when displaying it on the front end with something like htmlentities or htmlspecialchars. However if you want you can validate/filter the user input when they submit the form to make sure the data is in the proper format such as if a field is for an email address you want to validate that it has the proper email format. Is that correct?

My next question is what do you do with the data when you re-display it in a web form? Lets say the user is allowed to edit the information in that form after they filled it out and the information was added to the database. They then go back in and see the data in the fields they originally entered, do you have to sanitize the data for it to show correctly in the form fields? For example there is a field called My Title, the person enters My title is “Manager”. You see the quotations around manager, when you display it as is into the form field it breaks because of the quotations:

<input type="text" name="title" value="My title is "Manager"">

So don’t you have to do something like htmlentities to turn the quotations into its html entities? Otherwise the value of the field would look like My title is

Hope this makes sense.

  • 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. Editorial Team
    Editorial Team
    2026-05-16T17:20:11+00:00Added an answer on May 16, 2026 at 5:20 pm

    Nothing says you can’t sanitize data before database insertion. After all, if your script/site/company has a certain policy regarding what’s acceptable in a form field, it’s best to strip out anything that’s not allowed before saving it. That way you only sanitize once, before data insertion/update, rather than EVERY TIME you retrieve the data.

    If you allow HTML entities for (say) accented characters, but not HTML tags, then you have to both check for invalid entities (&foobar;?) and HTML tags as well. Since you don’t allow them, don’t bother storing them. If you require a valid email address, then check if it’s at RFC 5322 compliant and only store it once the user’s entered proper data. (Whether that email address actually exists is another matter).

    Now, let’s get one thing straight. There’s a difference between sanitization and escaping. Sanitization means literally to clean up – you’re removing anything you don’t want from the data. You can either silently drop it, or present an error to the user and tell them to fix it. On the other hand, escaping is just a means of encoding data so it’s displayed properly.

    With your My title is "Manager" string, you don’t need to sanitize it, as there’s nothing really wrong or offensive about it. What you do need to do is escape it, with at least htmlspecialchars(), so that the embedded double quotes don’t “break” your form. If you embed it verbatim, most browsers will see it as having value="My title is" and some bogus attribute/garbage Manager"". So, you run it through htmlspecialchars and end up My title is &quot;Manager&quot;, which embeds into the value="" perfectly with no trouble. No sanitization, just proper encoding.

    Now, when that form is submitted, then you do have to sanitize/validate again, as the data’s been in the hands of a potentially malicious user, and the data could have been changed to My title is <script>document.location='http://attacksite.com';</script>pwn me.

    Basically, the workflow should be:

    1. present form to user
    2. get data submitted.
    3. sanitize data
    4. if form is not correctly filled out, displays errors and go to 1)
    5. escape data for sql query
    6. insert into database

    then later

    1. retrieve data from database
    2. escape/encode as appropriate for however it will be displayed
    3. display data. if data’s going into a form, do 1-6 as before.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some questions regarding MVC that I would like to clarify. At our
I would just like to clarify that by 'design', I mean software design, not
i would like to know a good software engineering way to store user preferences
Before you start reading I would like to clarify: I have already thought of
I would like to use PHP to input a certain text and the output
I would like to have a reference for the pros and cons of using
I would like to use a language that I am familiar with - Java,
I would like to filter an array of items by using the map() function.
I would like to have an iframe take as much vertical space as it
I would like to have a VM to look at how applications appear and

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.