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

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

报告相同问题?

悬赏问题

  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?
  • ¥15 如何用QDomDocument读取内容为空格的xml数据