PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] Why do I get the empty $_SESSION variable?

 September 08, 2022     ajax, php, session-state     No comments   

Issue

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!


Solution

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')


Answered By - Adison Masih
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing