I'm trying to figure out the best way to manage user authentication.
At the moment I'm using this way
include("Conection.php");
$usuario = $_POST["Nick"];
$contra = $_POST["Pass"];
$sql = "SELECT * FROM usuario WHERE Nick = '$usuario' AND Password = '$contra'";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
if ($result->num_rows == 0) {
echo '<script languaje=javacript>
alert ("Inicion de sesion rechazada")
window.location="../Entrar.html"
</script>';
}
else
{
// Inicias la sesion
session_start();
$_SESSION['Usuario'] = $row['Nick'];
$_SESSION['estado'] = 'Autenticado';
echo ("<script>location.href='../../Index.php'</script>");
// Muestras el contenido de la pagina
}
and if my user logs in he will have different menu options
<?php
session_start();
if(isset($_SESSION['Usuario']) and $_SESSION['estado'] == 'Autenticado') { ?>
<a href="Navegacion/Entrar.html">Logeado</a>
<?php }
else
{ ?>
<a href="Navegacion/Entrar.html">Entrar</a>
<a href="Navegacion/Registrarse.html">Registrarse</a>
<a href="Navegacion/Carrito.html"><img src="Images/Carrito.png" alt="" width="20" height="20" ></a>
<?php } ?>
I know this way it's way weaker with SQL injections and stuff like that.
My main question was, Which it's the best way to protect againts sql injections, seems that the answer it's PDO, since I can not use comments and let me thank here all you that help me, thank you mdfst13