1

I have a simple login system in ionic that is operating normally. The system checks whether the user exists in the mysql database. If the user is there, it returns 1. If not, it returns 0. This return value is sent via json.

My problem is that I need to send user data to the next screen and in that data insert some extra information in the user registration. For example, after logging in the application takes the latitude and longitude of the user. I want to keep this latitude and longitude on their registration.

This is the login.php:

    <?php
session_start();
header("Access-Control-Allow-Origin: *");
$mysql_host = "localhost:3306";
$mysql_database = "melleve";
$mysql_user = "root";
$mysql_password = "";
// Create connection
$conn = new mysqli($mysql_host, $mysql_user, $mysql_password,$mysql_database);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$email = $_POST['email'];
$senha = $_POST['senha'];


 $sql = "SELECT * FROM cadast WHERE email='$email' AND senha='$senha'";

 $result = $conn->query($sql);


 $res=mysqli_fetch_array($result);

 $rs = mysqli_num_rows($result);


 if($rs>0)
 {

    if(!isset($_SESSION))
    session_start();

        $_SESSION['id']=$res['id'];         
        $_SESSION['nome']=$res['nome'];
        $_SESSION['email']=$res['email'];   
        $login = 1;
 }

else
{
    $login = 0;
}


echo (json_encode($login));
?>

This is the login controller:

 .controller('lg', function($scope, $state) {


$scope.logar = function(){


var formula = $('#formLogin').serialize();


$.ajax({


type : 'POST',
data : formula,
url : 'http://localhost/melleve/login.php',
success : function(data){

if(data == '' || data == 0){

alert('Login ou senha inválidos');

}


if(data == 1){

alert('Login realizado com sucesso');
//$state.go('app.atigmaps');



}


}


})


}



})
1
  • Welcome to StackOverflow! You are certain to get more helpful advice if you format your code cleanly. I recommend you do that so we can easily understand what you've already done! Commented Feb 16, 2016 at 23:15

1 Answer 1

2

I believe that the problems are this line:

   if(!isset($_SESSION))
    session_start();
  1. session_start() should be above any use of $_SESSION['anywhere'].

  2. Avoid always make vague connotations, so:

    if($_session)
    

    always check a variable specific session, as follows:

    if(isset($_session['here']) 
    

    Session variables are living in scope for a certain time or until you use unset

    ($ _ session [ 'here'])
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy Paul, problem solved, was of great help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.