简体   繁体   English

使用Java脚本共享PHP会话

[英]Share PHP session with Javascript

I have a PHP app that renders HTML pages for a social media application that I'm creating. 我有一个PHP应用程序,可为我正在创建的社交媒体应用程序呈现HTML页面。 Then, JavaScript initializes and makes things interactive. 然后,JavaScript初始化并使事物具有交互性。 The PHP side of things logs into a separate webservice with curl . PHP端使用curl登录到单独的Web服务。

Now, I can't figure out a way to share the session started in PHP with JavaScript, so when I make a AJAX request in JavaScript to the data server, its authenticated. 现在,我想不出一种方法来共享用JavaScript在PHP中启动的会话,因此,当我在JavaScript中向数据服务器发出AJAX请求时,该请求已通过身份验证。

Is there a way to share a PHP session with JavaScript? 有没有办法与JavaScript共享PHP会话? Or to share authentication initially created with PHP with JavaScript? 还是共享最初使用PHP和JavaScript创建的身份验证?

I would say it sounds like there is something wrong with your architecture. 我想说您的体系结构似乎有问题。 In my opinion, the web server itself, should be the only peer providing data to the client/browser. 我认为,Web服务器本身应该是向客户端/浏览器提供数据的唯一对等方。 It's a two party conversation only. 这只是两方对话。

When trying to hit a third-party server from the browser, you violate the browsers Same-Origin Policy, unless you specifically allow CORS by explicitly setting various request and response headers. 尝试从浏览器访问第三方服务器时,您违反了浏览器的“同源策略”,除非您通过显式设置各种请求和响应标头明确允许CORS。 - and you would only do so in very special situations. -并且您只会在非常特殊的情况下这样做。

The best solution might be to create proxy services at the web server, that can be hit directly (locally) by the browser. 最好的解决方案可能是在Web服务器上创建代理服务,该服务可以被浏览器直接(本地)访问。 The web server can then (acting as controller) forward the data-request to the data server (model) and finally return the response to the browser (view). 然后,Web服务器可以(充当控制器)将数据请求转发到数据服务器(模型),最后将响应返回到浏览器(视图)。

You can read out the session cookie set by PHP (SID I guess) through JavaScript containing the session ID. 您可以通过包含会话ID的JavaScript来读取PHP设置的会话Cookie(我猜是SID)。 When you make a query, use 进行查询时,请使用

http://example.com/?sid=SessionID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM