doubt with Ssion

Asked

Viewed 43 times

0

I’m trying to open a Session in php but it does not save value, I wondered if it is the way I do the redirect so I wanted to take my doubt, basically what I do is take the login and password and pass to a login page by ajax, in this login page I authenticate via json and if it returns valid redireciono in ajax to representative.php, but when I try to get the values of my Section the values are not there, I wonder if it is because of the javascript redirection, if anyone can help me find the error thank you, I’m new in php, I’ll leave explained.

Here the function that sends by ajax the login and password for 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 receive them in the login.php do the authentication 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;

Right after in the ajax function I redirect it to the.php representative that uses a function from the listRepresentante.php

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

In the representative list I try to acquire the values of Session but will not

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 wanted to know if it’s the way I’ve directed by ajax and if I can also help solve, Thank you

  • session_start does not have to be in the first line, that is, before echo $_SESSION['username'];?

  • something else Does not output (echo) before sending the headers!

  • Yes @Leocaracciolo that echo was from a test before, the question is that it always comes empty even I put after, ie in that condition if he always enters and redirects pro index, and that of the output before sending the headers as well? What part are you talking about?

  • send an echo $_SESSION['username']; before a header('Location:index.php'); https://answall.com/questions/4251/erro-cannot-modify-header-information

No answers

Browser other questions tagged

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