需要通过CURL将会话共享到PHP中相同域上的LOCAL psuedo服务器

not_a_generic_user

我正在使用本地php文件构建应用程序,该文件接受发布/获取输入并返回JSON结果。我这样做是为了使前端和后端操作脱钩,因为有可能最终将后端移动到其他地方(这很简洁,因为您可以仅使用浏览器和URL变量来测试后端操作。

需要明确的是,我没有立即或什至长期的计划来实际分离它们:现在它们甚至位于同一文件夹的同一服务器上-我只有一个backend.php文件,假装是一个远程服务器,所以我可以练习去耦。赢得该问题意味着要调用CURL并让后端接收该会话,后端可以更改/更新/添加到该会话,并且前端可以看到所有更改(基本上,一个会话用于前端和后端)。

问题是我一直在努力争取使两者之间的会议正常进行。当我使用JavaScript发出AJAX请求时,会话工作正常,因为它是在同一服务器上加载的页面,因此session_start()可以正常工作。但是当我使用CURL时,会话数据不会被传输。

我已经为此奋斗了几个月,所以我的curl功能非常凌乱,但是我无法弄清楚实现此功能的神奇组合。在这种情况下,我一直找不到稳定的问题或在线指南:

        // Call the backend using the provided URL and series of name/value vars (in an array)
    function backhand($data,$method='POST') {

        $url = BACKEND_URL;
        // Make sure the backend knows which session in the db to connect to
        //$data['session_id'] = session_id();
        // Backend will send this back in session so we can see the the connection still works.
        //$data['session_test'] = rand();
    
        $ch = curl_init();

        if ('POST' == $method) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $get_url = sprintf("%s?%s", $url, http_build_query($data));
        $_SESSION['diag']['backend-stuff'][] = $get_url;

        if ('GET' == $method) {
            curl_setopt($ch, CURLOPT_PUT, 1);
            $url = $get_url;
        }
    
        // Optional Authentication:
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
#       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
#       curl_setopt($ch, CURLOPT_VERBOSE, 1);
#       curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);

        // Retrieving session ID 
        // $strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';    
        $cookieFile = "cookies.txt";
        if(!file_exists($cookieFile)) {
            $fh = fopen($cookieFile, "w");
            fwrite($fh, $_SESSION);
            fclose($fh);
        }

#curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
#curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); // Cookie aware
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); // Cookie aware

        // We pass the sessionid of the browser within the curl request
        curl_setopt( $ch, CURLOPT_COOKIEFILE, $strCookie ); 
#       session_write_close();

        // Have to pause the session or the backend wipes the front
        if (!$result = curl_exec($ch)) {
            pre_r(curl_getinfo($ch));
            echo 'Cerr: '.curl_error($ch);
        }

        curl_close($ch);
#       session_start();

        // "true" makes it return an array
        return json_decode($result,true);
    }

我从前端这样调用该函数以从后端获取结果:

    // Get user by email or ID
    function get_user($emailorid) {
        // If it's not an email, see if they've been cached. If so, return them
        if (is_numeric($emailorid) && $_SESSION['users'][$emailorid])
            return $_SESSION['users'][$emailorid];

        return backhand(['get_user'=>$emailorid]);
    }

因此,如果我在前面的任何地方调用“ get_user”,它将跳到后面,运行数据库查询并将其全部转储到JSON中,并以关联的值数组形式返回给我。这可以正常工作,但是会话数据不能正常持久保存,并且会引起问题。

我什至尝试了一段时间的数据库会话,但这也不是一致的。我的想法不多了,可能必须通过使用db和自定义函数来构建某种替代的会话功能,但是我希望这可以正常工作……我只是还没有弄清楚该怎么做。

not_a_generic_user

经过反复的反复试验后,这似乎可行:

    function backhand($data,$method='POST') {

    $url = BACKEND_URL;
    // Make sure the backend knows which session in the db to connect to
    //$data['session_id'] = session_id();
    // Backend will send this back in session so we can see the the connection still works.
    $data['session_test'] = rand();

    $ch = curl_init();

    if ('POST' == $method) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $get_url = sprintf("%s?%s", $url, http_build_query($data));
    $_SESSION['diag']['backend-stuff'][] = $get_url;

    if ('GET' == $method) {
        curl_setopt($ch, CURLOPT_HTTPGET, 1);
        $url = $get_url;
    }

    // Optional Authentication:
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // required or it writes the data directly into document instead of putting it in a var below
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);         
    #curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']); 

    // Retrieving session ID 
    $strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';    

    // We pass the sessionid of the browser within the curl request
    curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );  
    curl_setopt($ch, CURLOPT_COOKIEJAR, 'somefile'); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, APP_ROOT.'/cookie.txt'); 
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000); 

    // Have to pause the session or the backend wipes the front
    session_write_close();
    if (!$result = curl_exec($ch)) {
        pre_r(curl_getinfo($ch));
        echo 'Cerr: '.curl_error($ch);
    }

    @session_start();

    curl_close($ch);

    // "true" makes it return an array
    return json_decode($result,true);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用PHP在同一服务器上的多个域之间共享会话

来自分类Dev

如何使用ajax请求javascript / PHP将会话存储在远程服务器上

来自分类Dev

SIPS服务器需要RADIUS服务器吗?

来自分类Dev

多服务器到单服务器

来自分类Dev

与IBM SBT的服务器到服务器通信

来自分类Dev

服务器到服务器回发

来自分类Dev

服务器到服务器 OAuth

来自分类Dev

服务器上的CPU /服务器超载

来自分类Dev

PHP服务器定义

来自分类Dev

PHP WebSocket服务器

来自分类Dev

在ubuntu服务器中配置Dns服务器

来自分类Dev

Ubuntu服务器中的“ Airport Express”服务器

来自分类Dev

Ubuntu服务器中的“ Airport Express”服务器

来自分类Dev

在ubuntu服务器中配置Dns服务器

来自分类Dev

通过HTTPS服务Flask服务器

来自分类Dev

在Apache服务器上设置域

来自分类Dev

SSH到不在同一域上的服务器

来自分类Dev

是否可以通过SSL在不同服务器上的子域之间共享cookie?

来自分类Dev

如何在共享服务器服务中运行服务器端Javascript?

来自分类Dev

我需要通过反向隧道RDP到服务器

来自分类Dev

新服务器上的php错误

来自分类Dev

PHP写入服务器上的文件

来自分类Dev

PHP在同一服务器上的不同域(cURL)上创建cookie

来自分类Dev

mysql使用PHP将服务器中的表复制到本地服务器

来自分类Dev

通过rc.local映射Ubuntu服务器来宾上的共享文件夹

来自分类Dev

PHP中的SOAP服务器错误?

来自分类Dev

在PHP中获取服务器时区

来自分类Dev

使用PHP中的会话或服务器变量获取URI

来自分类Dev

检索代理服务器(PHP)中的会话cookie