douzongluo7542
2017-05-26 04:16
浏览 61

如何使用Angular登录会话?

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.

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doufan6886 2017-05-26 04:33
    已采纳

    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!!

    点赞 打赏 评论

相关推荐 更多相似问题