73

Given the following two HTML/PHP snippets:

<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" />

and

<textarea name="content"><?php echo $_POST['content']; ?></textarea>

what character encoding do I need to use for the echoed $_POST variables? Can I use any built-in PHP functions?

Please assume that the $_POST values have not been encoded at all yet. No magic quotes - no nothing.

1

3 Answers 3

115

Use htmlspecialchars($_POST['firstname']) and htmlspecialchars($_POST['content']).

Always escape strings with htmlspecialchars() before showing them to the user.

Sign up to request clarification or add additional context in comments.

9 Comments

However, htmlspecialchars() won't help you with value='single quotes' This is what happens: value='We're not using this in our &quot;code&quot;...' All you see is We
Note: It is important to use double quotes for the value attribute in <input> tags.
Unless you specify ENT_QUOTES as the second htmlspecialchars() argument, single quotes will not be escaped. Therefore any single quotes present in your $_POST value will break out of the <input> field.
Note: Don't be tempted to use htmlentities(). That breaks UTF-8 characters. As rid said, use htmlspecialchars().
if you don't use ENT_QUOTES parameter then single quotes are not escaped. This is not a problem if in the input tag you define the value parameter as value="double quotes". The double quotes are escaped in the user provided string and the part with value="... is provided by the server.
|
4

htmlspecialchars would work in both cases. Have a look at the different flag options to avoid quotation marks being a problem in the input case.

Comments

0

Given it is kind of long, I would put it in a function:

<?php
function encodeValue ($s) {
    return htmlentities($s, ENT_COMPAT|ENT_QUOTES, 'ISO-8859-1', true);
}
?>

This has ENT_QUOTES to make sure single and double quotes are encoded, but it will also encode special characters (like in José) instead of inserting an empty string.

Then you can do:

<input type="text" name="firstname" value="<?= encodeValue($_POST['firstname']) ?>" />

and

<textarea name="content"><?= encodeValue($_POST['content']) ?></textarea>

4 Comments

ENT_QUOTES only would be enough to escape both double and single.
Not clear why this answer specifies ISO-8859-1 rather than the more commonly used today, default, value UTF-8. If in doubt, start with simpler return htmlentities($s, ENT_QUOTES);
@ToolmakerSteve, why IOS-8859-1 vs UTF-8 because that is what worked and I never tested UTF-8. Welcome some investigation there. Just using ENT_QUOTES does not work for characters with accents and will screw up names.
I would have understood such an answer circa 2010. But in 2018 ISO-8859-1 was already unacceptable, and ages like a milk every year since then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.