doubts with session

0

I'm trying to open a session in php but it does not write value, I wondered if it's the way I do the redirect so I wanted to get my question answered, basically what I do is get the login and password and move to a login page by ajax, in this login page I do the authentication via json and if it returns valid redirectiono in ajax for representative.php, however when I try to get the values of my session the values are not there, I was wondering if it is because of the redirect by javascript, if anyone can help me find the error thank you, I'm new to php, I'll let you explained.

Here the function that ajax sends the login and password to the Login.php

$(document).ready( function() {

     $('#logar').click(function(){
        var username = $("#username").val();
        var password = $("#password").val(); 

   $.ajax({ 

         url: 'controllers/Login.php',
         data: "username=" +username + "&password="+password ,
         type: 'GET',
         success: function(output) {
          alert(output);
           if(output != ''){
              window.location.href = "http://bravetech.com.br/politizar/representante.php";
           }
           else {
             alert('não logou');
           }
         }
      })
     });
  });

Then I get them in login.php I authenticate by json and return in ajax the isValid that returns a boolean

$username = $_GET['username'];
$password = $_GET['password'];
 //echo "<script>alert('$username');</script>";

 session_name("start");
 session_start("start");

        $data = array(
            "username" => $username, 
            "password" => $password
                     );

        $data_string = json_encode($data);                                                                                   
        $ch = curl_init('http://politizar.azurewebsites.net/api/account/login');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );                                                                                                                   

        $result = curl_exec($ch);
        curl_close($ch);
        $dados = json_decode($result, true);
        //print_r($dados);
        $access = $dados['object']['access_token'];
        $sucess = $dados['isValid'];

        //echo $sucess;
        //echo "<script>alert('$sucess');</script>";
        if($sucess) {
          // echo "<script>alert('$sucess');</script>";

           $_SESSION['username'] = $username;
           $_SESSION['password'] = $password;
        }
        else {
            session_destroy();
            //echo "<script>alert('Nao foi');</script>";
           unset ($_SESSION['username']);
           unset ($_SESSION['password']);
        }
        echo $sucess;

Soon after in the function ajax redirects to the Representative.php that uses a function of the ListPrepresentante.php

session_start("start");
$representantes = listarRepresentante();

In the delegate list I try to get the session values, but it will not go

echo $_SESSION['username'];

session_start("start");
if((!isset ($_SESSION['username']) == true) and (!isset ($_SESSION['password']) == true))
{
    unset($_SESSION['username']);
    unset($_SESSION['password']);
    header('location:index.php');
} 

$user = $_SESSION['username'];

I would like to know if it is the way I redirected through ajax and if it can also help to solve, Thanks

    
asked by anonymous 24.07.2017 / 19:22

0 answers