PHP session is not configured

Asked

Viewed 386 times

0

I’m using session_start to create a session for the user when he is authenticated to the server, but the session is not being maintained in the page exchange, I am using AJAX requests to authenticate, so:

login.js + ajax -> login.php [creates the session if logged in] [message error if not logged in] login.js -> [if logged in redirects for panel.php]

The session is created in login.php(tested with empty($_SESSION)), but on the panel.php page the session no longer exists, I used print_r($_SESSION) and the variable is not recognised.

Can anyone tell the cause of the mistake?

login js.:

$(document).ready(function(){

$("#go").click(function(e){
    e.preventDefault();

    client = new clientSnotes("log");
    client.sendLogin($("#email").val(), $("#password").val(), "log");
});

});

functions used by login.js:

function clientSnotes(){

this.parseUser = function(data, log){
    var xml = $.parseXML(data);
    xml = $(xml).contents();

    var error = xml.attr('error');

    //No error
    if(error === '0'){
        var user = new userFields(xml.attr('id'), xml.attr('name'), xml.attr('email'), xml.attr('password'));
        window.location.href = "panel.php?" + user.getDataUrl();
    }
    else{
        $("#" + log).css("display", "inline");
        $("#" + log).html(xml.contents());
    }
};


this.assemblyRequisition = function(parser, url, data, error_log){

    $.ajax({
        type: 'post',
        url: url,
        data: data,
        success: function(data){
            parser(data, error_log);
        },
        error: function(){
            alert("Failed to send data to server.");
        }
    });
};

this.sendLogin = function(email, password, error_log){
    this.assemblyRequisition(this.parseUser, 'scripts_php/login.php', 'email=' + email + '&password=' + password, error_log);
};

login.php

        if(UserDataHandler::selectByEmail($_POST['email']) && UserDataHandler::selectByPassword($_POST['password'])){
        $user = UserDataHandler::selectByEmail($_POST['email'])[0];
        $assembler->addAttribute("error", "0");
        $assembler->addAttribute("id", $user->id);
        $assembler->addAttribute("name", $user->name);
        $assembler->addAttribute("email", $user->email);
        $assembler->addAttribute("password", $user->password);
        session_start();
        //testando
        if(empty($_SESSION))
            $assembler->addAttribute ("session", "no");
        else
            $assembler->addAttribute ("session", "ok");

        $_SESSION['con'] = 1;
    }
    ...
    echo $assembler->assembly("result");//escreve o resultado da operação na página

panel.php

        <div id="content">
        <?php
            print_r($_SESSION);
        ?>
    </div>
  • You can share the code to let us know where the problem is?

  • Why don’t you use cookies? It’s much simpler. But just like Weslley said, share your code.

  • The code has already been shared, could use cookies but the problem is that they can be blocked and Session already solves this problem by passing the data through the URL automatically.

  • One thing that has nothing to do with the question, but with the comment,Sesssions use Cookies to be identified by the server. The browser passes Session_id and apache identifies the browser as the carrier of such a session.

  • yes but if not available it uses URL propagation to pass SID.

1 answer

3


session_start() means telling the page that you are willing to deal with sessions.

To access sessions, it is also necessary to session_start().

Add on your.php panel page, at the beginning of the . php document

  • 1

    i added in the login.php file and the sessions depend on the connection and not the pages to stay alive, I’m sure?

  • I understood, thank you very much man, the session was recognized, now I know it is necessary to call the function on each page, even worth!

  • Alright.Arrange ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.