将 PHP 会话与 Vue/Node.js 集成 [英] Integrate PHP sessions with Vue/Node.js

查看:62
本文介绍了将 PHP 会话与 Vue/Node.js 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PHP 应用程序,它对用户进行身份验证、执行 MySQL 查询并依赖于 $_SESSION cookie.

I have a PHP application which authenticates user, execute MySQL queries and relies on $_SESSION cookie.

一切正常,但我意识到我需要用前端做更多的事情,因此不得不使用 Vue.js 和 websockets.

Everything was working fine but I realized I need to do more with Frontend and hence had to use Vue.js and websockets.

因此,我使用 node.js 作为主域 localhost(为了生产)

Therefore, I used node.js for primary domain localhost (for production sake)

我在 localhost:8080 上提供了 PHP API,以便 Vue.js 可以与 PHP 后端通信.

I made PHP API available on localhost:8080 so that Vue.js could communicate with PHP Backend.

现在,这是我的问题

如何在 Vue/Websocket 中使用/存储 $_SESSION ID 并将其与向 PHP 服务器发出的每个请求一起发送?

How can I use/store $_SESSION ID in Vue/Websocket and send it along with every request made to PHP server?

我对 stackoverflow 做了一些研究,发现 session_id() 可以返回当前的会话 ID,但我不知道如何将 PHP 与 Vue 集成.

I did some research on stackoverflow and found session_id() which could return current Session ID but I have no clue about how to integrate PHP with Vue.

注意:我在 Node.js 中使用 Vue.js(Vue、路由器、Vuex)和 socket.io

推荐答案

由于 PHP 服务器和 node.JS 服务器运行在两个不同的端口上,因此它们在所有浏览器中被视为不同的来源.这意味着从 localhost:8080 发送的 cookie 不会发送到对 localhost:80 发出的任何请求.解决此问题的一种方法是删除会话 cookie 上的 http only 标志,以便您可以通过 javascript BUT 手动发送 cookie,如果有人管理,结果可能会非常糟糕在您的站点上运行 XSS 攻击.

Since the PHP server and the node.JS server is running on two different ports, they are considered as different origins in all browsers. That means that a cookie that was sent from localhost:8080 will not be sent to any requests made to localhost:80. One way to overcome this would be to remove the http only flag on session cookies so that you can manually send the cookie via javascript BUT that could end up pretty bad if someone manages to run XSS attacks on your site.

其他方法是 1) 只使用前端的两台服务器中的一台,然后让使用过的后端与另一个后端进行通信,这意味着您可以使 php 仅可用于节点应用程序或 2) 重新设计 PHP 以使用令牌.根据我的理解,您使用 php 作为 API,并且大多数 API 设计为使用令牌而不是会话,因为您通常从另一个域访问 API.

Other ways would be 1) to use only one of the two servers from your front-end and then have the used backend to communicate with the other backend meaning that you could make php available only to the node application or 2) Redesign PHP to use tokens. From my understanding you are using php as an API and most APIs are designed to use tokens instead of session since you are usually accessing the API from another domain.

这篇关于将 PHP 会话与 Vue/Node.js 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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