Apologies if this has been asked before - but in this case I feel there is an overload of information out there, rather than too little. There also appears to be conflicting opinions everywhere I look. Any clarification would be much appreciated.
I have a user sign-up form. I have validated the different fields, but I haven't used any sanitization functions on the data (e.g. htmlspecialchars) because I read here that it's best to leave that until output (e.g on a user profile). I am using PDO prepared statements to insert my data, so I am safe there as far as I know.
However, having run my domain through various vulnerability scanners, they reported that my sign-up form was very unsafe - and could be victim to XSS attacks. I believe the snippet of code causing this is as follows:
<input type="text" placeholder="Username" name="user" maxlength="32" value="<?php echo test_input($user); ?>" pattern="^[a-zA-Z0-9]*$" required>
I am echo-ing that user's input back into the form in case they have an error in another field, so as to re-submit after correction. Therefore potentially harmful script could be echoed into the page.
However, surely this is only the input from that specific user? The only harm they could cause is to themselves? Is the scanner mistaken or am I largely unaware of the risk?
I have fields for general strings, email address, URLs. What steps do I take to ensure safety for visitors to my site?
Thank you very much for any help/clarification you can give me!
EDIT
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES | ENT_HTML5, 'UTF-8');
return $data;
}