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

  • SEARCH
  • Home
  • 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 311521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:55:57+00:00 2026-05-12T07:55:57+00:00

I am creating a forum software using php and mysql backend, and want to

  • 0

I am creating a forum software using php and mysql backend, and want to know what is the most secure way to escape user input for forum posts.

I know about htmlentities() and strip_tags() and htmlspecialchars() and mysql_real_escape_string(), and even javascript’s escape() but I don’t know which to use and where.

What would be the safest way to process these three different types of input (by process, I mean get, save in a database, and display):

  1. A title of a post (which will also be the basis of the URL permalink).
  2. The content of a forum post limited to basic text input.
  3. The content of a forum post which allows html.

I would appreciate an answer that tells me how many of these escape functions I need to use in combination and why.
Thanks!

  • 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-12T07:55:57+00:00Added an answer on May 12, 2026 at 7:55 am

    When generating HTLM output (like you’re doing to get data into the form’s fields when someone is trying to edit a post, or if you need to re-display the form because the user forgot one field, for instance), you’d probably use htmlspecialchars() : it will escape <, >, ", ', and & — depending on the options you give it.

    strip_tags will remove tags if user has entered some — and you generally don’t want something the user typed to just disappear 😉

    At least, not for the “content” field 🙂


    Once you’ve got what the user did input in the form (ie, when the form has been submitted), you need to escape it before sending it to the DB.

    That’s where functions like mysqli_real_escape_string become useful : they escape data for SQL

    You might also want to take a look at prepared statements, which might help you a bit 😉

    with mysqli – and with PDO

    You should not use anything like addslashes : the escaping it does doesn’t depend on the Database engine ; it is better/safer to use a function that fits the engine (MySQL, PostGreSQL, …) you are working with : it’ll know precisely what to escape, and how.


    Finally, to display the data inside a page :

    • for fields that must not contain HTML, you should use htmlspecialchars() : if the user did input HTML tags, those will be displayed as-is, and not injected as HTML.
    • for fields that can contain HTML… This is a bit trickier : you will probably only want to allow a few tags, and strip_tags (which can do that) is not really up to the task (it will let attributes of the allowed tags)
      • You might want to take a look at a tool called HTMLPUrifier : it will allow you to specify which tags and attributes should be allowed — and it generates valid HTML, which is always nice ^^
      • This might take some time to compute, and you probably don’t want to re-generate that HTML each time is has to be displayed ; so you can think about storing it in the database (either only keeping that clean HTML, or keeping both it and the not-clean one, in two separate fields — might be useful to allow people editing their posts ? )

    Those are only a few pointers… hope they help you 🙂

    Don’t hesitate to ask if you have more precise questions !

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'm using the alternative syntax feature. I start the template… May 12, 2026 at 7:00 pm
  • Editorial Team
    Editorial Team added an answer This might work (untested)... var clubs = ClubRepository.Where(c=>c.MemberShips.Any(m=>m.User.Age > 45)); May 12, 2026 at 7:00 pm
  • Editorial Team
    Editorial Team added an answer Ok I don’t know why the above doesn’t work, but… May 12, 2026 at 7:00 pm

Related Questions

I've been charged with creating a little GUI launcher app to go on a
I have an application which sends a POST request to the VB forum software
I have a written a client side HTTP cache and I need to store
Solution I did some googling and found this forum post , and here is

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.