I have a login, that when I enter a username and password in a form and press "enter" and I have established the connection to the database, use the following line of code:
$pass = $mysqli->real_escape_string($_POST['txtpass']);
I am informing myself on the internet, but I do not understand the ->real_escape_string
what does it mean? How does it protect me from SQL-INJECTION?
I leave you the full piece of code
<?php
if(isset($_POST['txtpass']))
{
session_start();
//variable de conexion: recibe dirección del host , usuario, contraseña y el nombre base de datos
$mysqli = new mysqli("localhost", "root", "", "bdpersona") or die ("Error de conexion porque: ".$mysqli->connect_errno);
// comprobar la conexión
if (mysqli_connect_errno())
{
printf("Falló la conexión: %s\n", mysqli_connect_error());
exit();
}
$login = $mysqli->real_escape_string($_POST['txtlogin']);
$pass = $mysqli->real_escape_string($_POST['txtpass']);
$resultado = $mysqli->query("SELECT * FROM tbusuario where login='$login' and pass='$pass' and activo!=0");
$valida=$resultado->num_rows;
if($valida != 0)
{
$datosUsu = $resultado->fetch_row();
$_SESSION['nombreusu'] = $datosUsu[3];
$_SESSION['perfil'] = $datosUsu[4];
echo "<META HTTP-EQUIV='Refresh' CONTENT='0; URL=listar.php'>";
}
else
{
echo
"<script>
var textnode = document.createTextNode('Usuario ó Password Incorrecto');
document.getElementById('msg').appendChild(textnode);
</script>";
}
}
? >
Thank you.