-2

login.js

     $http({
              url: 'http://ipadress/login.php',
              method: 'POST',
              data: {
                'var_id': $scope.form.txt_id,
                'var_pass': $scope.form.txt_pass
              }
            }//http

login.php

<?php
session_start();
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
mysql_connect("localhost","root","password");
mysql_select_db("db");
$data = file_get_contents("php://input");
$dataJsonDecode = json_decode($data);

$var_id = $dataJsonDecode->var_id;
$var_pass = $dataJsonDecode->var_pass;


$sql = "SELECT * FROM user WHERE user_login = '".($id)."' and user_pass = '".md5($pass)."'";
$query = mysql_query($sql);
$result = mysql_fetch_array($query);

$resource = mysql_query($sql);
$count_row = mysql_num_rows($resource);

if (!$result) {
    $results = '{"results":"not match"}';
} else {
    $_SESSION["users_login"] = $result["users_login"];
    session_write_close();
    if($count_row > 0){
        $results = '{"results":"match"}';
    } else {
        $results = '{"results":"Error"}';
    }
}
echo $results;
?>

$http.get('http://ipaddress/data_user.php')
      .then(function (response) {
        console.log(response.data.results);
        $scope.myData = response.data.results;
      }, function (error) {
        console.log(error);
      });

user_data.php

<?php
session_start();
if($_SESSION['users_login'] == ""){
    $results = '{"results":"Please Login"}';
    echo $results;
}
else{
    mysql_connect("localhost","root","password");
    mysql_select_db("db");
    mysql_query( "SET NAMES UTF8" );
    $sql = "SELECT * FROM users WHERE users_login = '".$_SESSION["users_login"]."' ";
    $query = mysql_query($sql);
    $count_row = mysql_num_rows($query);
    if($count_row > 0){
        while($result = mysql_fetch_array($query)){
            $rows[] = $result;
        }
        $data = json_encode($rows);
        $totaldata = sizeof($rows);
        $results = '{"results": '.$data.'}';
    }
    echo $results;
}
?>

I have problem with login. My $results = please login.

halfer
  • 19,824
  • 17
  • 99
  • 186
Peerawat
  • 3
  • 4
  • why you tagged ionic here? – varun aaruru May 26 '17 at 04:30
  • sorry this is first post. – Peerawat May 26 '17 at 04:34
  • Can you reccommend me. – Peerawat May 26 '17 at 04:34
  • **Do not put this live on the internet**. It needs rewriting, and you will get hacked in short order if you put this live. Your database queries are susceptible to SQL injection, and your password hashes are easily reversible, since you're using a fast hash with no salt. Unless you really know what you are doing, you should always use a good quality library to handle user accounts and authentication. Your database library is also no longer supported by the PHP core team. – halfer May 26 '17 at 09:58
  • Can you recommend libary? I'm new programmer. – Peerawat Jun 01 '17 at 03:35

1 Answers1

0

First check what $result["users_login"] contains.. You are getting the please login because you don't have the session data.

I am not sure about your approach, but don't mix php session and Ionic, You can create a kind of API to check user credentials from your ionic App, or else handle it in the ionic app.
Session normally used in websites to pass data through out the page, not for API calls. Think of scenario 10 users access your app simultaneously, How you going to use the Session to verify them?

My advise is, instead of using PHP sessions, you can handle your sessions inside your app using the session storage
Read this. There are plenty of plugin available. OR you can create your as shown here
Good Luck!!

Kuru
  • 738
  • 5
  • 18