PHP $ _SESSION变量在Ionic/AngularJS应用程序中不起作用 [英] PHP $_SESSION variables not working in ionic/angularjs application

查看:65
本文介绍了PHP $ _SESSION变量在Ionic/AngularJS应用程序中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发环境信息:

  • 本地开发
  • 在我的PC上设置服务器的网络驱动器
  • 通过目录路径访问服务器文件
  • 服务器包含SQL数据库,服务器文件可以从中访问

在应用程序加载时

我已经在页面上设置了ng-init来运行函数 $ scope.nginit(); ,该函数会将$ _GET请求发送到我的服务器文件login.php:

I have set up ng-init on my page to run the function $scope.nginit(); which sends a $_GET request to my server file called login.php:

$http.get(url + "server/login.php", 
            { transformResponse: [] })
.success(function(data, status, headers, config) 
{
    data = JSON.parse(data);
    window.localStorage.setItem("session_id",data.session_id);
})
.error( function(data, status, headers, config) 
{
    console.log("Error: " + data + "; " + status);
});

此文件包含在页面顶部的每个服务器文件中:

// if(isset($_GET['session_id'])) {
//  session_id($_GET['session_id']);
// } else if(isset($_POST['session_id'])) {
//  session_id($_POST['session_id']);
// }

session_start();

include_once "../scripts/masterscript.php";

header("Access-Control-Allow-Origin: *");

$connection = connect_to_database();
try
{
    if($_SERVER['REQUEST_METHOD'] != 'POST')
    {
        setup_form();
    }
    else
    {
        process_form();
    }

    sqlsrv_close($connection);  
}
catch (Exception $e)
{
    exit("Exception $e->getMessage()");
}

这是login.php上的setup_form():

function setup_form()
{
    global $connection;
    header('Content-Type: application/json');
    $user_data = array();

    $user_data["session_id"] = session_id();

    echo json_encode($user_data);   
}

在submitForm()上;用于登录:

它将添加存储在localStorage中的session_id作为参数,以便可以将其设置回PHP会话.它将其设置在PHP文件的顶部,但该部分当前已被注释掉.

It adds the session_id stored in the localStorage as a parameter so it can be set back to the PHP session. It sets it on top of the PHP file but that section is currently commented out.

我的目的是如果它是管理员用户,然后运行第二个$ http.request并打开一个模式.

My intent is if it is an admin user then run the second $http.request and open a modal.

$http.post(url + "pages/login.php", data_string,
            {   headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;' }, 
                params : { 'session_id' : window.localStorage.session_id }})
.success(function(data, status, headers, config) 
{
    var data = data[0];

    if(data.is_admin)
    {
        $http.get(url + "server/get_more_info.php", 
                    { params : {'user_id' : data.user_id },
                    transformResponse: [] })
        .success(function(data, status, headers, config) 
        {
            // setup to open a modal
        })
        .error( function(data, status, headers, config) 
        {
        }); 
    }
})
.error( function(data, status, headers, config) 
{
});

由于这是$ _POST请求,它将转到process_form(),这里是:

function process_form()
{
    global $connection;
    header('Content-Type: application/json');
    $user_data = array();

    $data_string = file_get_contents("php://input");
    $data = json_decode($data_string);

    $user_data['status_msg'] = "Error";

    // runs SQL query, grabs user details where email = $data->email
    $UserObj = new User(0,$connection, $data->email); 

    if($UserObj->password == $data->password)
    {
        $user_data['status_msg'] = "OK";
        $user_data['user_id']    = $UserObj->user_id;
        $user_data['user_type'] = $UserObj->user_type;

        if($UserObj->admin_user == 1)
        {
            $user_data['is_admin'] = true;
            $_SESSION['is_admin'] = 1;
        }

        $_SESSION['user_id'] = $UserObj->user_id;
        $_SESSION['user_type'] = $UserObj->user_type;
    }

    unset($UserObj);
    echo json_encode($user_data);
 }

在get_more_info.php中

我尝试访问在login.php中设置的会话变量,但它们为空.我打算在SQL查询中使用这些变量:

I try and access the session variables set in login.php but they are blank. I intend to use those variables in SQL queries:

$user_id = $_SESSION['user_id'];
$admin_user = $_SESSION['admin_user'];
$user_type = $_SESSION['user_type'];

$sql = " select * from info_table where admin_user = ? and user_type = ?";
$params = array($admin_user,$user_type);
$result = sqlsrv_query($connection, $sql, $params);       

while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
{
}

sqlsrv_free_stmt($result);
unset($result);

其他信息

我做了一个var_dump();对于下面的这两个都返回空字符串.

I did a var_dump(); for both of these below and are returning empty strings.

ini_get('session.cookie_httponly');
ini_get('session.cookie_secure');

在Firefox上,出现了第一个对login.php的网络请求,它是 GET 请求,并且具有设置的$ _COOKIE.似乎可以在Chrome浏览器中使用,但不能在Firefox中使用.我目前正在运行 ionic serve --lab .在chrome上,它将继续运行并打开应有的模式,但在Firefox上则不会.

On Firefox, the first network request for login.php which is the GET request is appearing and has a set $_COOKIE. It seems like it works in chrome but not firefox. I am currently just runing ionic serve --lab. On chrome it proceeds and opens a modal as it should but on firefox it does not.

在firefox的网络标签上,用于login.php GET 请求,它显示在响应标头下设置了cookie,但在请求标头上没有与cookie相关的属性.

On the network tab of firefox for the login.php GET request, it shows set-cookie under response headers but no cookie related attribute on request headers.

对于login.php POST 请求,其结果与前一个cookie的结果相同,但此请求在网络"标签下显示为 OPTIONS 方法,而不是 POST .

For the login.php POST request it has the same result as the previous for the cookie but this request is appearing as OPTIONS method under the network tab instead of POST.

问题

  • 最初的问题是,在get_more_info.php中,会话变量为空白.这是因为它没有恢复到从 $ scope.nginit(); 开始的同一会话.它正在开始一个新的.
  • 提出了存储session_id以便可以恢复到同一会话的想法,这就是我在这里所做的.
  • 最初,我试图通过手动将session_id设置在我的PHP文件的顶部来设置它,但是提醒我,这不是必需的,它应该自动执行,而不是自动进行.
  • The initial issue was that the session variables were blank in get_more_info.php. This was because it was not resuming to the same session started at $scope.nginit();. It was starting a new one instead.
  • The idea of getting the session_id to be stored so it can resume to the same session was brought up so that is what i've done here.
  • Initially I was trying to set the session_id by manually setting it at the top of my PHP files but it was brought to my attention that this was not needed and it should be doing it automatically which it isn't.

推荐答案

后端

第一:此位是不必要:

if(isset($_GET['session_id'])) {
    session_id($_GET['session_id']);
} else if(isset($_POST['session_id'])) {
    session_id($_POST['session_id']);
}

more_details.php 文件开头唯一需要的一行是 session_start().

The only line you need at the start of your more_details.php file is session_start().

如果您查看此功能的文档,则会清楚说:

If you look at the documentation for this function it clearly says:

session_start()创建一个会话或恢复(*)当前会话,该会话基于通过GET或POST请求传递或通过cookie传递的会话标识符.

session_start() creates a session or resumes (*) the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

*强调我的

因此,在调用它之后,您可以安全地访问 $ _ SESSION 属性.

So after you call it, you can safely access $_SESSION properties.

第二,您需要设置正确的CORS标头.

Secondly, you need to set up the proper CORS headers.

header("Access-Control-Allow-Origin: http://localhost:8000");
header("Access-Control-Allow-Headers: content-type, authorization");
header("Access-Control-Allow-Credentials: true");

非常重要:无法将 Access-Control-Allow-Origin 设置为 * ,否则将失败.使用凭据时,需要明确设置允许的来源.

Very important: Access-Control-Allow-Origin can't be set to * or this will fail. When using credentials, the allowed origin need to be set explicitely.

此外,您需要考虑一个 OPTIONS 调用,该调用应返回相同的标头.浏览器将在POST请求之前进行此调用,作为飞行前的验证.

Also, you need to account for an OPTIONS call, that should return this same headers. This call will be made by the browser as a pre-flight verification before the POST request.

出于这个答案的目的,我有一个 common.php 文件,该文件为:

For the purposes of this answer I have a common.php file that goes:

<?php
session_start();
header( "Access-Control-Allow-Origin: http://localhost:8000" );
header( 'Content-Type: application/json' );
header( "Access-Control-Allow-Headers: content-type,authorization" );
header( "Access-Control-Allow-Credentials: true" );

if ( $_SERVER['REQUEST_METHOD'] == 'OPTIONS' ) {
    die();
}

另一个文件 one.php :

<?php
// one.php
<?php
require 'common.php';

$was_session_set     = $_SESSION['user_id'] ?? 'no session id yet';
$_SESSION['user_id'] = microtime( true );

echo '{"session": "' . $was_session_set . '"}';

die();

另一个 two.php :

<?php
require 'common.php';

$user_id = $_SESSION['user_id'] ?? 0;
echo '{"session": "' . $user_id  .'"}';

die();

前端

let app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
$http.get("http://localhost:8006/one.php",  { withCredentials: true })
    .then(function (response) {
      $scope.myWelcome = response.data;
      $http.post("http://localhost:8006/two.php", { withCredentials: true })
          .then(function (response2) {
            console.log(response2.data);
          })
    });
});

这篇关于PHP $ _SESSION变量在Ionic/AngularJS应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆