The Peril of Raw $_POST in Default Values
You are asking a question that strikes at the heart of Cross-Site Scripting (XSS), specifically Stored XSS. The short answer is: you should never echo raw $_POST data directly into HTML attributes or content. There is no character encoding you can apply that will "fix" this specific usage pattern because the fundamental approach is flawed.
In your first example, you are attempting to set a default value for an input field using untrusted user data:
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" />
And in your second, you are doing the same with a textarea:
<textarea name="content"><?php echo $_POST['content']; ?></textarea>
If a user submits <script>alert(1)</script> into $_POST['firstname'], that exact string becomes the default value of the input. When the page re-renders, the browser interprets that script tag as executable code rather than text. The resulting HTML looks like this in the source:
<input type="text" name="firstname" value="<script>alert(1)</script>" />
The browser renders it as an alert box executing immediately upon page load. This is Stored XSS, and it persists across requests.
Why "Encoding" Is Not a Panacea Here
You asked what character encoding you need to use. The answer is that standard HTML entity encoding (using htmlspecialchars) is the correct tool for output, but applying it to default values requires a shift in how you construct the markup, not just a function call.
If you wrap your output with htmlspecialchars(), you convert < to < and > to >. This breaks the script tag into harmless text:
<input type="text" name="firstname" value="<?php echo htmlspecialchars($_POST['firstname'], ENT_QUOTES, 'UTF-8'); ?>" />
However, relying on this pattern for default values creates a fragile architecture. You are essentially telling the browser, "Here is some data that might need escaping, but I'm doing it manually every time." If you forget to pass ENT_QUOTES or miss an option, you introduce a vulnerability immediately. Furthermore, if you ever change how the input is rendered (e.g., using a framework like Laravel which might handle attributes differently), your manual escaping logic could become misaligned with the output context.
The Correct Approach: Server-Side Storage
The robust solution is to treat user input as data, not as HTML or attributes. If you need to display previously submitted values, store them in a database and retrieve them when needed, ensuring they are escaped at the point of view.
In modern PHP development, we rarely manually construct HTML strings like this anymore. Frameworks handle the context-aware escaping automatically. For instance, in Laravel, the old() helper function is specifically designed for this scenario:
<input type="text" name="firstname" value="{{ old('firstname') }}" />
When combined with Blade's default escaping (which happens by default unless you explicitly tell it not to), or when using the value directive which respects the context, frameworks prevent these injections by design. If you are using a framework like Laravel, ensure your view engine is configured to escape output by default, as manual string interpolation in HTML attributes bypasses these safeguards.
Handling Textareas and Content
The second example with the <textarea> presents a similar issue but with slightly different implications regarding content length and potential line breaks. The logic remains identical: raw output is unsafe.
If you must use raw PHP inside your view (perhaps outside of a templating engine), you must apply htmlspecialchars() to every single instance of user data entering an HTML attribute or body element. You cannot rely on "magic quotes" as they are deprecated and removed in modern PHP versions. You also cannot rely on the server's default encoding; you must explicitly specify UTF-8 to ensure characters like accented letters or special symbols are handled correctly during the conversion.
Implementation Trade-offs
The primary trade-off here is between convenience and security. Manually echoing $_POST variables feels convenient because it reduces boilerplate, but it introduces a single point of failure for every developer reading that code. If one developer forgets to escape an attribute while another escapes content, the application becomes vulnerable.
By adopting a pattern where you always pass data through an escaping function (like htmlspecialchars) or rely on framework helpers like Laravel's old() function, you externalize this responsibility. You are no longer "escaping HTML form input default values"; you are simply retrieving stored data for display, and the security mechanism is abstracted away.
In summary, do not seek a specific character encoding to save you from XSS in default values. Seek a coding pattern where user input is never treated as trusted markup. Use htmlspecialchars() with ENT_QUOTES and UTF-8, or better yet, leverage framework helpers that enforce this separation of data and presentation automatically.