I've done quite some research on this but I can't seem to find the EXACT answer to my problem as in what code to put where.
So I'm building a simple website for users to login and play javascript games. The site isn't up and running yet but I got a random email from a stranger saying my site is vulnerable to XSS and sent me a link which redirected to my website. When I got back to my website, an alert came up showing cookies and some other stuff I don't know about. The link is no longer active so I can't show you the script that was added to the input field of the login form.
Is there any way to prevent malicious users from doing this? Here is the code which makes up my login from if it helps.
<form method="post">
<p>Login</p>
<div class="form-group">
<label for="login" style="float:left">Email or username</label>
<input class="form-control" name="login" value="<?php echo addslashes($_POST['login']); ?>"/>
<label for="loginpassword" style="float:left">Password</label>
<input class="form-control" type="password" name="loginpassword" value="<?php echo addslashes($_POST['loginpassword']); ?>"/>
</div>
<input type="submit" class="btn btn-success btn-lg wrap" name="submit" value="login"/>
<?php
if($error) {
echo '<div class="alert alert-danger" id="errorDiv">'.addslashes($error).'</div>';
}
?>
</form>
From the login.php file:
<?php
if ($_POST['submit']=="login")
{
$login = mysqli_real_escape_string($connection_info, $_POST['login']);
$loginpw = $_POST['loginpassword'];
//check database see if correct login details
// if not found then $error = "incroorect login details" etc
// if login details then match assign id from database as session variable for authentication
}
?>
Can someone tell me exactly what I need to do avoid XSS for this particular form? From What I've read, it seems more useful to use htmlspecialchars instead of addslashes for the value of the text field but I'm not sure if this will resolve the problem.