I am trying to protect my website from xss attacks and I use htmlspecialchars and mysqli_real_escape_string for this.
Now I am trying to let a user login and register, while he can still use signs like < or >.
I am using the following code for the username: By signup:
$username = mysqli_real_escape_string($mysql,$_POST['username']);
$username = strtolower($username);
$username = htmlspecialchars($username);
And for login:
$username = mysqli_real_escape_string($mysql,$_POST['username']);
$username = strtolower($username);
$username = htmlspecialchars(htmlspecialchars($username));
echo "Username: $username<br>";
When I use a basic alert script at sign up for username, the username gets encrypted by htmlspecialchars and I see this in the database:
<script>alert(\'2\')</script>
When I try to login it says "Password or Username incorrect!", but the echo at login exactly matches the username in the database.
First I only used one htmlspecialchars at login, but then it would just echo the username and it wouldn't change anything and I couldn't login.
Does anyone know how I can do this?