Home > OS >  Why do I get the empty $_SESSION variable?
Why do I get the empty $_SESSION variable?

Time:06-30

It turns out that when I get the variable $_SESSION it's empty, and I do not understand why. Months ago it worked perfectly but then one day it no longer recognized the sessions and returns them null, when I call var_dump().

I added session_start() at the beginning of everything, and I receive the $_POST parameters correctly, but when I save the session variable on another page it is lost and null arrives.

What can be the causes of this occurring, if before it worked well? I regret if the question is not the right one, I would like to know the causes of why now they are not received and before if, it is possible to clarify that I am in a hosting.

<?php
session_start();

include "db.php";

$stmr = $con->prepare("SELECT * FROM USUARIOS WHERE NOMBRE = ? AND PASSWORD = ? ");

$usuario = $_POST["usuario"] ?: "";

$password = $_POST["password"] ?: "";

$stmr->bind_param("ss", $usuario, $password);

$stmr->execute();

$resultadoCons = $stmr->get_result();

if ($resultadoCons->num_rows > 0) {
    while ($row = $resultadoCons->fetch_assoc()) {
        if ($row["ID_TIPO"] == 1) {
            $_SESSION["administrador"] = $usuario;
            echo "administrador";
        } else if ($row["ID_TIPO"] == 3) {
            $_SESSION["admin"] = $usuario;
            echo "admin";
        } else {
            $_SESSION["usuario"] = $usuario;
            echo "usuario";
        }
    }
} else {
    echo "error";
}

$con->close();

This is the validate. I'm using AJAX

/* Login */

$(document).ready(function() {
    $('#formulario').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: 'config/validate.php',
            data: $(this).serialize(),
            success: function(response)
            {
                // var jsonData = JSON.parse(response);
                if (response == "administrador")
                {
                    location.href = 'admin.php';
                }else if(response == "usuario"){
                    location.href = 'homeUsu.php';
                }else if(response == "admin"){
                    location.href = 'home.php';
                }
                else
                {
                    Swal.fire({
                        icon: 'error',
                        title: 'Oops...',
                        text: '¡Sus credenciales son incorrectas,reintente!',
                      })
                }

           }
       });

     });
});

If you need more code, or context I will be attentive, thank you very much!

CodePudding user response:

First Check If You Have A Cookie Named: PHPSESSID or not in your browser.

Also It Can be that The Directory Where Your Sessions Are To Be Stored Is Not Writeable And You Don't Have Sufficient Permissions. In Most Cases, It's The /tmp directory.

You Can Use the following code to determine if your sessions dir is writeable or not:

$session_dir = session_save_path();
if (is_writable($session_dir)) {
    echo 'It Is Writeable'; 
} else {
    echo 'Not Writeable!';
}

If you get Non Writeable, then go ahead and make the directory writeable or change the session save dir using the following code:

ini_set('session.save_path', '/path/to/your/writeable/folder')
  • Related