久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='b8drh'></bdo><ul id='b8drh'></ul>

    <small id='b8drh'></small><noframes id='b8drh'>

    <tfoot id='b8drh'></tfoot>
    1. <i id='b8drh'><tr id='b8drh'><dt id='b8drh'><q id='b8drh'><span id='b8drh'><b id='b8drh'><form id='b8drh'><ins id='b8drh'></ins><ul id='b8drh'></ul><sub id='b8drh'></sub></form><legend id='b8drh'></legend><bdo id='b8drh'><pre id='b8drh'><center id='b8drh'></center></pre></bdo></b><th id='b8drh'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='b8drh'><tfoot id='b8drh'></tfoot><dl id='b8drh'><fieldset id='b8drh'></fieldset></dl></div>
      <legend id='b8drh'><style id='b8drh'><dir id='b8drh'><q id='b8drh'></q></dir></style></legend>

        如何使用Redis在NodeJs和PHP之間共享會話?

        How to share session between NodeJs and PHP using Redis?(如何使用Redis在NodeJs和PHP之間共享會話?)
          <tfoot id='r60Wm'></tfoot>

        1. <i id='r60Wm'><tr id='r60Wm'><dt id='r60Wm'><q id='r60Wm'><span id='r60Wm'><b id='r60Wm'><form id='r60Wm'><ins id='r60Wm'></ins><ul id='r60Wm'></ul><sub id='r60Wm'></sub></form><legend id='r60Wm'></legend><bdo id='r60Wm'><pre id='r60Wm'><center id='r60Wm'></center></pre></bdo></b><th id='r60Wm'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='r60Wm'><tfoot id='r60Wm'></tfoot><dl id='r60Wm'><fieldset id='r60Wm'></fieldset></dl></div>
              <tbody id='r60Wm'></tbody>
            • <legend id='r60Wm'><style id='r60Wm'><dir id='r60Wm'><q id='r60Wm'></q></dir></style></legend>
                <bdo id='r60Wm'></bdo><ul id='r60Wm'></ul>

                  <small id='r60Wm'></small><noframes id='r60Wm'>

                  本文介紹了如何使用Redis在NodeJs和PHP之間共享會話?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想使用 RedisNodeJs 應用程序和 PHP 應用程序之間共享服務器會話.我從這個 gist 中獲取了大部分代碼.

                  NodeJs 代碼:

                  app.use(session({商店:新的RedisStore({前綴:'session:php:'}),name: 'PHPSESSID',秘密:'node.js'}));app.use(function(req, res, next) {req.session.nodejs = 'node.js!';res.send(JSON.stringify(req.session, null, ' ') );});

                  它輸出:

                  <代碼>{曲奇餅": {原始最大年齡":空,過期":空,httpOnly":真,小路": "/"},護照": {},nodejs":node.js!"}

                  PHP 代碼(我使用 redis-session-php 和 Predis) :

                  require('redis-session-php/redis-session.php');RedisSession::start();$_SESSION['php'] = 'php';如果 (!isset($_SESSION["cookie"])) {$_SESSION["cookie"] = array();}var_dump($_SESSION);

                  它輸出:

                  array(2) {[php"] =>字符串(3)php"[cookie"] =>數組(0){}}

                  問題:我希望兩個會話看起來一樣,但它們不一樣(應用程序在同一個域上運行).使用 PredisClient 中的 set() 設置值有效(但值不會在會話變量上).我發現 這段代碼我認為可以用, 使用 set()get() ,但我覺得它會使代碼過于復雜.

                  你知道我做錯了什么嗎?

                  解決方案

                  我是要點的作者.該代碼一直有效,直到 express-session 開始強制簽名 cookie 并開始以不同的方式實現它們.

                  我已經更新了要點以使用最新版本的 express-session.為方便起見,以下是要點的副本:

                  app.js:

                  var express = require('express'),app = express(),cookieParser = require('cookie-parser'),session = require('express-session'),RedisStore = require('connect-redis')(session);app.use(express.static(__dirname + '/public'));app.use(function(req, res, next) {if (~req.url.indexOf('favicon'))返回 res.send(404);下一個();});app.use(cookieParser());app.use(會話({商店:新的RedisStore({//這是 redis-session-php 使用的默認前綴前綴:'會話:php:'}),//使用默認的 PHP 會話 cookie 名稱name: 'PHPSESSID',秘密:'node.js 規則',重新保存:假,保存未初始化:假}));app.use(function(req, res, next) {req.session.nodejs = '來自 node.js 的你好!';res.send('<pre>' + JSON.stringify(req.session, null, ' ') + '</pre>');});應用程序聽(8080);

                  app.php:

                  <?php//這必須與 Express 應用程序中的 express-session `secret` 匹配定義('EXPRESS_SECRET','node.js 規則');//==== 開始快速會話兼容性 ====//這個 id mutator 函數有助于確保我們查找//使用正確 id 的會話定義('REDIS_SESSION_ID_MUTATOR','express_mutator');函數 express_mutator($id) {if (substr($id, 0, 2) === "s:")$id = substr($id, 2);$dot_pos = strpos($id, ".");如果($dot_pos !== 假){$hmac_in = substr($id, $dot_pos + 1);$id = substr($id, 0, $dot_pos);}返回 $id;}//檢查現有的 express-session cookie ...$sess_name = session_name();如果(isset($_COOKIE[$sess_name])){//這里我們必須操作 cookie 數據以便//redis 中的查找工作正常//由于 express-session 現在強制簽署 cookie,我們有//在這里處理...if (substr($_COOKIE[$sess_name], 0, 2) === "s:")$_COOKIE[$sess_name] = substr($_COOKIE[$sess_name], 2);$dot_pos = strpos($_COOKIE[$sess_name], ".");如果($dot_pos !== 假){$hmac_in = substr($_COOKIE[$sess_name], $dot_pos + 1);$_COOKIE[$sess_name] = substr($_COOKIE[$sess_name], 0, $dot_pos);//https://github.com/tj/node-cookie-signature/blob/0aa4ec2fffa29753efe7661ef9fe7f8e5f0f4843/index.js#L20-L23$hmac_calc = str_replace("=", "", base64_encode(hash_hmac('sha256', $_COOKIE[$sess_name], EXPRESS_SECRET, true)));如果 ($hmac_calc !== $hmac_in) {//cookie數據已被篡改,可自行判斷//你想如何處理這個.對于這個例子,我們將//忽略 cookie 并生成一個新會話 ...未設置($_COOKIE[$sess_name]);}}} 別的 {//讓 PHP 為我們生成一個新的 idsession_regenerate_id();$sess_id = session_id();$hmac = str_replace("=", "", base64_encode(hash_hmac('sha256', $sess_id, EXPRESS_SECRET, true)));//根據 express-session 簽名的 cookie 格式對其進行格式化session_id("s:$sess_id.$hmac");}//==== 結束快速會話兼容性 ====require('redis-session-php/redis-session.php');RedisSession::start();$_SESSION["php"] = "你好來自 PHP";如果 (!isset($_SESSION["cookie"]))$_SESSION["cookie"] = array();echo "

                  ";回聲 json_encode($_COOKIE, JSON_PRETTY_PRINT);回聲 json_encode($_SESSION, JSON_PRETTY_PRINT);echo "</pre>";?>

                  I want to share the server session between a NodeJs app and a PHP app using Redis. I took most of the code from this gist.

                  NodeJs code:

                  app.use(session({
                      store: new RedisStore({prefix: 'session:php:'}),
                      name: 'PHPSESSID',
                      secret: 'node.js'
                  }));
                  
                  app.use(function(req, res, next) {
                      req.session.nodejs = 'node.js!';
                      res.send(JSON.stringify(req.session, null, '  ') );
                  
                  });
                  

                  And it outputs:

                  {
                      "cookie": {
                          "originalMaxAge": null,
                          "expires": null,
                          "httpOnly": true,
                          "path": "/"
                      },
                      "passport": {},
                      "nodejs": "node.js!"
                  }
                  

                  PHP code (I use redis-session-php and Predis) :

                  require('redis-session-php/redis-session.php');
                  RedisSession::start();
                  
                  $_SESSION['php'] = 'php';
                  
                  if (!isset($_SESSION["cookie"])) {
                      $_SESSION["cookie"] = array();
                  }
                  
                  var_dump($_SESSION);
                  

                  And it outputs:

                  array(2) {
                      ["php"] => string(3) "php"
                      ["cookie"] => array(0) { }
                  }
                  

                  The problem: I would expect both sessions to look the same, but they don't ( the app is run on the same domain ). Setting the values with set() from PredisClient works (but the values won't be on the session variable). I found this code that I think would work, using set() and get(), but I feel that it would overcomplicate the code.

                  Do you know what I am doing wrong?

                  解決方案

                  I'm the author of the gist. The code worked until express-session started forcing signed cookies and started implementing them in a different way.

                  I have updated the gist to work with the latest version of express-session. A copy of the gist follows for convenience:

                  app.js:

                  var express = require('express'),
                      app = express(),
                      cookieParser = require('cookie-parser'),
                      session = require('express-session'),
                      RedisStore = require('connect-redis')(session);
                  
                  app.use(express.static(__dirname + '/public'));
                  app.use(function(req, res, next) {
                    if (~req.url.indexOf('favicon'))
                      return res.send(404);
                    next();
                  });
                  app.use(cookieParser());
                  app.use(session({
                    store: new RedisStore({
                      // this is the default prefix used by redis-session-php
                      prefix: 'session:php:'
                    }),
                    // use the default PHP session cookie name
                    name: 'PHPSESSID',
                    secret: 'node.js rules',
                    resave: false,
                    saveUninitialized: false
                  }));
                  app.use(function(req, res, next) {
                    req.session.nodejs = 'Hello from node.js!';
                    res.send('<pre>' + JSON.stringify(req.session, null, '    ') + '</pre>');
                  });
                  
                  app.listen(8080);
                  

                  app.php:

                  <?php
                  // this must match the express-session `secret` in your Express app
                  define('EXPRESS_SECRET', 'node.js rules');
                  
                  // ==== BEGIN express-session COMPATIBILITY ====
                  // this id mutator function helps ensure we look up
                  // the session using the right id
                  define('REDIS_SESSION_ID_MUTATOR', 'express_mutator');
                  function express_mutator($id) {
                    if (substr($id, 0, 2) === "s:")
                      $id = substr($id, 2);
                    $dot_pos = strpos($id, ".");
                    if ($dot_pos !== false) {
                      $hmac_in = substr($id, $dot_pos + 1);
                      $id = substr($id, 0, $dot_pos);
                    }
                    return $id;
                  }
                  // check for existing express-session cookie ...
                  $sess_name = session_name();
                  if (isset($_COOKIE[$sess_name])) {
                    // here we have to manipulate the cookie data in order for
                    // the lookup in redis to work correctly
                  
                    // since express-session forces signed cookies now, we have
                    // to deal with that here ...
                    if (substr($_COOKIE[$sess_name], 0, 2) === "s:")
                      $_COOKIE[$sess_name] = substr($_COOKIE[$sess_name], 2);
                    $dot_pos = strpos($_COOKIE[$sess_name], ".");
                    if ($dot_pos !== false) {
                      $hmac_in = substr($_COOKIE[$sess_name], $dot_pos + 1);
                      $_COOKIE[$sess_name] = substr($_COOKIE[$sess_name], 0, $dot_pos);
                  
                      // https://github.com/tj/node-cookie-signature/blob/0aa4ec2fffa29753efe7661ef9fe7f8e5f0f4843/index.js#L20-L23
                      $hmac_calc = str_replace("=", "", base64_encode(hash_hmac('sha256', $_COOKIE[$sess_name], EXPRESS_SECRET, true)));
                      if ($hmac_calc !== $hmac_in) {
                        // the cookie data has been tampered with, you can decide
                        // how you want to handle this. for this example we will
                        // just ignore the cookie and generate a new session ...
                        unset($_COOKIE[$sess_name]);
                      }
                    }
                  } else {
                    // let PHP generate us a new id
                    session_regenerate_id();
                    $sess_id = session_id();
                    $hmac = str_replace("=", "", base64_encode(hash_hmac('sha256', $sess_id, EXPRESS_SECRET, true)));
                    // format it according to the express-session signed cookie format
                    session_id("s:$sess_id.$hmac");
                  }
                  // ==== END express-session COMPATIBILITY ====
                  
                  
                  
                  require('redis-session-php/redis-session.php');
                  RedisSession::start();
                  
                  $_SESSION["php"] = "Hello from PHP";
                  if (!isset($_SESSION["cookie"]))
                    $_SESSION["cookie"] = array();
                  
                  echo "<pre>";
                  echo json_encode($_COOKIE, JSON_PRETTY_PRINT);
                  echo json_encode($_SESSION, JSON_PRETTY_PRINT);
                  echo "</pre>";
                  
                  ?>
                  

                  這篇關于如何使用Redis在NodeJs和PHP之間共享會話?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
                  <legend id='TOepc'><style id='TOepc'><dir id='TOepc'><q id='TOepc'></q></dir></style></legend>
                    <tbody id='TOepc'></tbody>

                      1. <i id='TOepc'><tr id='TOepc'><dt id='TOepc'><q id='TOepc'><span id='TOepc'><b id='TOepc'><form id='TOepc'><ins id='TOepc'></ins><ul id='TOepc'></ul><sub id='TOepc'></sub></form><legend id='TOepc'></legend><bdo id='TOepc'><pre id='TOepc'><center id='TOepc'></center></pre></bdo></b><th id='TOepc'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='TOepc'><tfoot id='TOepc'></tfoot><dl id='TOepc'><fieldset id='TOepc'></fieldset></dl></div>

                          <bdo id='TOepc'></bdo><ul id='TOepc'></ul>
                          1. <tfoot id='TOepc'></tfoot>

                            <small id='TOepc'></small><noframes id='TOepc'>

                            主站蜘蛛池模板: av在线播放不卡 | 久久久久久国产精品 | 亚洲精品一二区 | 97免费视频在线观看 | 国产精品久久久久久久久久妞妞 | 国产91久久久久 | 国产精品视频久久 | 在线播放一区二区三区 | 色综合天天天天做夜夜夜夜做 | 欧美日韩一区二区三区在线观看 | 99视频在线播放 | 亚洲性人人天天夜夜摸 | 国产精品中文在线 | 亚洲狠狠 | 欧美极品在线 | 国产高清自拍视频在线观看 | 中文字幕丁香5月 | 国产精品视频一区二区三区四区国 | 日本三级线观看 视频 | 国产精品美女www爽爽爽 | 欧美精品在线一区二区三区 | 精品国产一区二区三区日日嗨 | 999免费网站 | 亚洲国产小视频 | 欧美激情久久久 | 亚洲天堂av网 | 北条麻妃99精品青青久久 | 一级黄在线观看 | 日韩精品免费在线观看 | 岛国av免费观看 | www.一级片 | 高清久久久 | 网站一区二区三区 | 伊人精品久久久久77777 | 免费观看一级毛片 | 亚洲成av人片在线观看无码 | 久久久久久久av | 欧美一级片中文字幕 | 91国内精品久久 | 国产视频1区2区 | 中文字幕av亚洲精品一部二部 |