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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:41:21+00:00 2026-05-27T09:41:21+00:00

Ok, this subject is a hotbed I understand that. I also understand that this

  • 0

Ok, this subject is a hotbed I understand that. I also understand that this situation is dependent on what you are using as code. I have three situations that need to be resolved.

  1. I have a form in where we need to allow people to make comments and statements that use commas, tildes, etc… but still remain safe from attacks.

  2. I have people entering in dates like this: 10/13/11 mm/dd/yy in English, can this be sanitized?

  3. How do I understand how to use htmlspecialchars(), htmlentities() and real_escape_string() correctly? I’ve read the php.net site and some posts here but this seems to me to be a situation in where it all depends on the person reading the question what the right answer is.

I really can’t accept that… there has to be an answer wherein text formats similar to that which I am posting here can be sanitized. I’d like to know if and how it is possible.

Thanks… because it seems to me that when asking this question in other places it tends to annoy… I am learning what I need to know but I think I have hit a plateau in what I can know without an example of what it is meant to do…

Thanks in advance.

  • 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-27T09:41:22+00:00Added an answer on May 27, 2026 at 9:41 am

    It’s a very important question and it actually has a simple answer in the form of encodings. The problem you are facing it that you use a lot of languages at the same time. First you are in HTML, then in PHP and a few seconds later in SQL. All these languages have their own syntax rules.

    The thing to remember is: a string should at all times be in its proper encoding.

    Lets take an example. You have a HTML form and the user enters the following string into it:

    I really <3 dogs & cats ;')

    Upon pressing the submit button, this string is being send to your PHP script. Lets assume this is done through GET. It gets appended to the URL, which has its own syntax (the & character has special meaning for instance) so we are changing languages. This means the string must be transformed into the proper URL-encoding. In this case the browser does it, but PHP also has an urlencode function for that.

    In the PHP script, the string is stored in $_GET, encoded as a PHP string. As long as you are coding PHP, this is perfectly fine. But now lets put the string to use in a SQL query. We change languages and syntax rules, therefore the string must be encoded as SQL through the mysql_real_escape_string function.

    At the other end, we might want to display the string back to the users again. We retrieve the string from the database and it is returned to us as a PHP string. When we want to embed it in HTML for output, we’re changing languages again so we must encode our string to HTML through the htmlspecialchars function.

    Throughout the way, the string has always been in the proper encoding, which means any character the user can come up with will be dealt with accordingly. Everything should be running smooth and safe.

    A thing to avoid (sometimes this is even recommended by the ignorant) is prematurely encoding your string. For instance, you could apply htmlspecialchars to the string before putting it in the database. This way, when you retrieve the string later from the database you can stick it in the HTML no problem. Sound great? Yeah, really great until you start getting support tickets of people wondering why their PDF receipts are full of &amp; &gt; junk.

    In code:

    form.html:

    <form action="post.php" method="get">
        <textarea name="comment">
            I really <3 dogs &amp; cats ;')
        </textarea>
        <input type="submit"/>
    </form>
    

    URL it generates:

    http://www.example.org/form.php?comment=I%20really%20%3C3%20dogs%20&amp;%20cats%20;')
    

    post.php:

    // Connect to database, etc....
    
    // Place the new comment in the database
    $comment = $_GET['comment']; // Comment is encoded as PHP string
    
    // Using $comment in a SQL query, need to encode the string to SQL first!
    $query = "INSERT INTO posts SET comment='". mysql_real_escape_string($comment) ."'";
    mysql_query($query);
    
    // Get list of comments from the database
    $query = "SELECT comment FROM posts";
    
    print '<html><body><h2>Posts</h2>';
    print '<table>';
    
    while($post = mysql_fetch_assoc($query)) {
        // Going from PHP string to HTML, need to encode!
        print '<tr><td>'. htmlspecialchars($post['comment']) .'</td></tr>';
    }
    
    print '</table>';
    print '</body></html>'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in a bit of a doubt about this subject. Situation: I have conducted
I have done some research on this subject, and found out, that Gzipping and
I need a little help on this subject. I have a Web application written
For those that are knowledgeable in this subject (and hopefully faced a similar situation),
I have a problem finding references to this subject and would appreciate some help.
I found a forum with this subject. Using enum as id But I couldn't
I have seen many posts on this subject, but none have been answered, and
I know this subject has been covered before on here but I need a
I have in my table this value ART(\'O\') in the field Subject. How do
I know there have been tons of answers on this subject, but I still

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.