file_get_contents(或curl或fopen)会话数据的问题 [英] file_get_contents (or curl, or fopen) problem with session data

查看:204
本文介绍了file_get_contents(或curl或fopen)会话数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面显示会话中的值,让它调用www.domain-a.com/master.php,如果我直接从浏览器键入它,它显示我的会话值。

i have a page that shows a value from session, lets call it www.domain-a.com/master.php and if i type it directly from the browser, it shows me the session value.

但是当我尝试使用file_get_contents(或其他方法)从另一个域(如www.domain-b.com/slave.php)下载它时,它不会检索受会话,只是一个空白页。

but when i try to download it with file_get_contents (or other method) from another domain, like www.domain-b.com/slave.php, it is not retrieving the content protected by the session, just a blank page.

我知道这是因为server-b正在尝试检索内容,而不是用户...

i know it is because the server-b is trying to retrieve the content, not the user...

任何人都知道如何告诉域名 - 检索信息的人是用户?有一种方法可以检索会话值?

anyone knows how to tell the domain-a that who is retrieving the information is the user? there is a way to retrieve the session value?

问候,

josé

推荐答案

有一个有用的解决方案。

There is one usefull solution.

将PHPSESSID发送到另一个服务器没有意义,因为会话数据存储在服务器上的文件中,这就是为什么file_get_contents阻止http服务的原因。很简单。客户端使用http连接到服务器,服务器打开包含会话数据的文件,以便写入。 file_get_contents创建连接到同一服务器的另一个连接(另一个线程)。如果设置了会话ID,则服务器使用会话数据打开相同的文件,但此文件已打开。

Sending PHPSESSID to another server doesn't make a sense, because session data are stored in a file on the server and that is the reason why file_get_contents block http service. It is simple. Client connect to server using http and the server opens file with session data for writing of course. file_get_contents create another connection (another thread) that connect to the same server. If session id is set then server opens same file with session data, but this file is already opened.

因此,这是一个很好的解决方案, >

so here is a good solution that prevents this collision:

$opts = array( 'http'=>array( 'method'=>"GET",
              'header'=>"Accept-language: en\r\n" .
               "Cookie: ".session_name()."=".session_id()."\r\n" ) );

$context = stream_context_create($opts);
session_write_close();   // this is the key
$obsah = file_get_contents( 'http://blablabla.cz', false, $context);

它工作正常。是是是

这篇关于file_get_contents(或curl或fopen)会话数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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