douzongluo7542 2017-05-26 04:16
浏览 66
已采纳

如何使用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!!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化