Community, the error I have is that the user, having finished the registration process, I want him to log in at once without having to go to a login page, the code I am using for this is the following:
if(isset($_POST) && !empty($_POST)){
$names = $_POST['nombres'];
$pass = $_POST['password_user'];
$nie = $_POST['nie'];
$tlf = $_POST['telefono'];
$email = $_POST['email'];
$d_entrega = $_POST['address_entrega'];
$d_facturacion = $_POST['address_fact'];
$query = "INSERT INTO usuarios (nombres,pass,nie,telefono,direccion_entrega,direccion_facturacion,email)
VALUES ('$names','$pass','$nie','$tlf','$d_entrega','$d_facturacion','$email')";
$sql = $con->query($query);
if($sql>0){
$query2 = "SELECT * FROM usuarios WHERE email = '$email' AND pass = '$pass'";
$sql2 = $con->query($query2);
$result = $sql2->num_rows;
echo $result;
$datos = $sql2->fetch_assoc();
session_start();
$_SESSION['usuario']=$datos;
header('Location:productos.php');
}
}
The num_rows gives me >0 but when it does the redirection it sends me to the page without the session started. As if not logged in.
First of all, there was an error in the order of the fields
insert
, which is a minor problem, with the insecure code being built as the greatest problem. You should prepare your sentences.If the session is not displayed or has problems, it was possibly because I had not started it with
session_start()
, thisdebería realizarlo en los dos archivos y en todos los que emplee sesiones
in the current one and inproductos.php
(at the beginning of the file)Another consideration to keep in mind is Is it necessary to perform a
select
when you are sure that the user has registered? . Perhaps you can skip this step. (possible final code)