通过 file_get_contents 共享会话

发布于 2025-01-14 06:28:03 字数 4448 浏览 2 评论 0原文

美好的一天,我想使用 PHP 从我的服务器读取 PHP 执行的文件(带有会话),但是一旦我通过传递会话 ID 调用 file_get_contents ,它就不起作用:

为了检查,我在我的服务器上做了一个小测试,这是 test_reader.php 文件:

session_start();

// if no test session, creating it & refreshing

if( !isset( $_SESSION['mysession'] ) ) {
    $_SESSION['mysession'] = 'hello world';
    header( 'Location: test_reader.php' );
}

// reading "test_readable.php"

$url = 'https://example.com/test_readable.php?';    

$parameters = http_build_query( array(
    'session' => session_id()
) );

$options = array( 'http' => array(
    'method'  => 'GET',
    'content' => $parameters
) );

$context = stream_context_create( $options );

echo ( $url.$parameters ).': <br>'.file_get_contents( ( $url.$parameters ), false, $context );

这是 test_reader.php 文件:

// if session ID is sent, opening the session

if( isset( $_GET['session'] ) ) {
    session_id( $_GET['session'] );
}

session_start();

// checking result
    
if( isset( $_SESSION['mysession'] ) ) {
    echo 'yes';
} else {
    echo 'no (id: '.session_id().')';
}

if( isset( $_GET['session'] ) ) {
    echo ' but session ID found';
}

首先,加载 test_reader.php 需要一分钟的时间> 访问时。

但除此之外,根本没有任何结果:

https://example.com/test_readable.php?session=x75277c2a8fccfcc4d446ec7c93d84006:

事实上,test_read.php 中没有任何内容被打印出来。

所以我对 file_get_contents 结果做了一个 var_dump 来检查响应:

bool(false)

我不明白为什么我的文件没有被读取?

您知道它是来自会话还是 file_get_contents 设置吗? 或者你知道它来自哪里吗?

预先感谢您的回复。

编辑 - 从头开始​​测试私有模式

test_reset.php

session_start();
$_SESSION['mysession'] = null;
unset( $_SESSION['mysession'] );
session_destroy();

test_read.php

// if session ID is sent, opening the session

if( isset( $_GET['session'] ) ) {
    session_id( $_GET['session'] );
}

session_start();

// checking result

if( isset( $_SESSION['mysession'] ) ) {
    echo 'yes (id: '.session_id().')';
} else {
    echo 'no (id: '.session_id().')';
}

if( isset( $_GET['session'] ) ) {
    echo ' and session ID found';
}

test_reader.php

session_id( 'session'.rand( 1, 9 ).rand( 1, 9 ).rand( 1, 9 ) );
session_start();

echo '<div>'.session_id().'&nbsp;</div>';

$_SESSION['mysession'] = 'hello world';

$parameters = http_build_query( array(
    'session' => session_id()
) );

$options = array( 'http' => array(
    'method'  => 'GET',
    'content' => $parameters
) );

$context = stream_context_create( $options );

$url = 'https://example.com/test_readable.php?';

$read = file_get_contents( ( $url.$parameters ), false, $context );
// $read = file_get_contents( ( $url.$parameters ) );

echo '<div>'.( $url.$parameters ).'&nbsp;</div>';

echo '<div>'.$read.'&nbsp;</div>';

var_dump( $read );

测试跟踪:(

https://example.com/test_reset.php
>> <empty page>

https://example.com/test_readable.php
>> no (id: 59081a4dfd8ea3acd10ebaafd432daaf)

https://example.com/test_reader.php
>> session432 
>> https://example.com/test_readable.php?session=session432 
>> 
>> bool(false)

相同的结果如果我不将 file_get_content 与上下文一起使用)。

编辑 2 - 在没有上下文的情况下进行测试/已解决

我没有更改其他文件,仅更改了以下文件:

test_reader.php

session_id( 'session'.rand( 1, 9 ).rand( 1, 9 ).rand( 1, 9 ) );
session_start();

$sessionID = session_id();
echo '<div>'.$sessionID.'&nbsp;</div>';

$_SESSION['mysession'] = 'hello world';
session_write_close();

$parameters = http_build_query( array(
    'session' => $sessionID
) );

$url = 'https://example.com/test_readable.php?';

$read = file_get_contents( ( $url.$parameters ) );

echo '<div>'.( $url.$parameters ).'&nbsp;</div>';

echo '<div>'.$read.'&nbsp;</div>';

var_dump( $read );

结果是:

https://example.com/test_readable.php
>> no (id: 2d3b1d3e9d2975134bf3141d3c49c3d8)

https://example.com/test_reader.php
>> session849 
>> https://example.com/test_readable.php? 
>> no (id: ce844dd021a0e1d167a316a2e4cd08a0) 
>> string(41) "no (id: ce844dd021a0e1d167a316a2e4cd08a0)"

Good day, I would like to read a PHP executed file (with a session) from my server with PHP, but once I call file_get_contents by passing the session ID, it doesnt works:

To check, I made a small test on my server, here is the test_reader.php file:

session_start();

// if no test session, creating it & refreshing

if( !isset( $_SESSION['mysession'] ) ) {
    $_SESSION['mysession'] = 'hello world';
    header( 'Location: test_reader.php' );
}

// reading "test_readable.php"

$url = 'https://example.com/test_readable.php?';    

$parameters = http_build_query( array(
    'session' => session_id()
) );

$options = array( 'http' => array(
    'method'  => 'GET',
    'content' => $parameters
) );

$context = stream_context_create( $options );

echo ( $url.$parameters ).': <br>'.file_get_contents( ( $url.$parameters ), false, $context );

And here is the test_readable.php file:

// if session ID is sent, opening the session

if( isset( $_GET['session'] ) ) {
    session_id( $_GET['session'] );
}

session_start();

// checking result
    
if( isset( $_SESSION['mysession'] ) ) {
    echo 'yes';
} else {
    echo 'no (id: '.session_id().')';
}

if( isset( $_GET['session'] ) ) {
    echo ' but session ID found';
}

First of all it takes a minute to load the test_reader.php when visiting it.

But in addition to this, there is no results at all:

https://example.com/test_readable.php?session=x75277c2a8fccfcc4d446ec7c93d84006:

Indeed, nothing from test_readable.php has been printed.

So I made a var_dump on the file_get_contents result to check the response:

bool(false)

And I don't understand why do my file is not read?

Do you know if it's coming from a session or a file_get_contents set up?
Or do you know where it can come from?

Thank you in advance for your reply.

EDIT - TEST PRIVATE MODE FROM SCRATCH

test_reset.php

session_start();
$_SESSION['mysession'] = null;
unset( $_SESSION['mysession'] );
session_destroy();

test_readable.php

// if session ID is sent, opening the session

if( isset( $_GET['session'] ) ) {
    session_id( $_GET['session'] );
}

session_start();

// checking result

if( isset( $_SESSION['mysession'] ) ) {
    echo 'yes (id: '.session_id().')';
} else {
    echo 'no (id: '.session_id().')';
}

if( isset( $_GET['session'] ) ) {
    echo ' and session ID found';
}

test_reader.php

session_id( 'session'.rand( 1, 9 ).rand( 1, 9 ).rand( 1, 9 ) );
session_start();

echo '<div>'.session_id().' </div>';

$_SESSION['mysession'] = 'hello world';

$parameters = http_build_query( array(
    'session' => session_id()
) );

$options = array( 'http' => array(
    'method'  => 'GET',
    'content' => $parameters
) );

$context = stream_context_create( $options );

$url = 'https://example.com/test_readable.php?';

$read = file_get_contents( ( $url.$parameters ), false, $context );
// $read = file_get_contents( ( $url.$parameters ) );

echo '<div>'.( $url.$parameters ).' </div>';

echo '<div>'.$read.' </div>';

var_dump( $read );

Test trace:

https://example.com/test_reset.php
>> <empty page>

https://example.com/test_readable.php
>> no (id: 59081a4dfd8ea3acd10ebaafd432daaf)

https://example.com/test_reader.php
>> session432 
>> https://example.com/test_readable.php?session=session432 
>> 
>> bool(false)

(Same result if I do not use the file_get_content with a context).

EDIT 2 - TEST WITHOUT CONTEXT / SOLVED

I didn't change other files, only the following-one:

test_reader.php

session_id( 'session'.rand( 1, 9 ).rand( 1, 9 ).rand( 1, 9 ) );
session_start();

$sessionID = session_id();
echo '<div>'.$sessionID.' </div>';

$_SESSION['mysession'] = 'hello world';
session_write_close();

$parameters = http_build_query( array(
    'session' => $sessionID
) );

$url = 'https://example.com/test_readable.php?';

$read = file_get_contents( ( $url.$parameters ) );

echo '<div>'.( $url.$parameters ).' </div>';

echo '<div>'.$read.' </div>';

var_dump( $read );

Result was:

https://example.com/test_readable.php
>> no (id: 2d3b1d3e9d2975134bf3141d3c49c3d8)

https://example.com/test_reader.php
>> session849 
>> https://example.com/test_readable.php? 
>> no (id: ce844dd021a0e1d167a316a2e4cd08a0) 
>> string(41) "no (id: ce844dd021a0e1d167a316a2e4cd08a0)"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情 2025-01-21 06:28:03

确保您销毁了第一个会话,使用以下命令:

 
session_abort(); // add this line

// set the new session
session_id( $_GET['session'] );

session_start();

// checking result
    
if( isset( $_SESSION['mysession'] ) ) {
    echo 'yes';
} else {
    echo 'no (id: '.session_id().')';
}

if( isset( $_GET['session'] ) ) {
    echo ' but session ID found';
}

make sure that you destroyed the first session use this:

 
session_abort(); // add this line

// set the new session
session_id( $_GET['session'] );

session_start();

// checking result
    
if( isset( $_SESSION['mysession'] ) ) {
    echo 'yes';
} else {
    echo 'no (id: '.session_id().')';
}

if( isset( $_GET['session'] ) ) {
    echo ' but session ID found';
}
吝吻 2025-01-21 06:28:03

流上下文的使用在那里没有什么意义。 content 指定要作为请求正文发送的内容 - 但 GET 请求不会在正文中发送数据。您应该完全删除流上下文,然后只需将参数附加到 URL 中。

当使用基于文件的会话存储时,只要一个脚本实例仍在“使用”会话文件,PHP 就会对会话文件加一把锁。因此,尝试在向应该使用同一会话的另一个脚本发出请求的同一脚本中设置会话值,只有在您调用 session_write_close 之前才能正常工作。要求。这会将所有会话更改写入磁盘,然后释放会话文件上的锁定。

The usage of the stream context makes little sense there. content specifies what is to be send as the request body - but GET requests don't send their data in the body. You should completely remove the stream context, and then simply append your parameter to the URL.

And when using file-based session storage, PHP puts a lock on the session file, as long as one script instance is still "using" it. So trying to set the session value here in the same script that makes the request to the other one that is supposed to use the same session, will only properly work, if you call session_write_close, before you make the request. That writes all session changes out to the disk, and then releases the lock on the session file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文