美好的一天,我想用 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_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';
} 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_readable.php內容被列印出來。
所以我var_dump在 file_get_contents 結果上做了一個檢查回應:
bool(false)
而且我不明白為什么我的檔案沒有被讀取?
你知道它是來自會話還是 file_get_contents 設定?或者你知道它是從哪里來的嗎?
預先感謝您的回復。
編輯 - 從頭開始??測驗私人模式
test_reset.php
session_start();
$_SESSION['mysession'] = null;
unset( $_SESSION['mysession'] );
session_destroy();
測驗可讀的.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)"
uj5u.com熱心網友回復:
確保你銷毀了第一個會話使用這個:
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';
}
uj5u.com熱心網友回復:
流背景關系的使用在那里沒有什么意義。content指定要作為請求正文發送的內容 - 但 GET 請求不會在正文中發送它們的資料。您應該完全洗掉流背景關系,然后只需將引數附加到 URL。
并且當使用基于檔案的會話存盤時,PHP 會鎖定會話檔案,只要一個腳本實體仍在“使用”它。session_write_close因此,如果您在發出請求之前呼叫 ,嘗試在向另一個應該使用相同會話的腳本發出請求的同一腳本中設定會話值,只會正常作業。這會將所有會話更改寫入磁盤,然后釋放會話檔案上的鎖定。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/447227.html
標籤:php session file-get-contents
上一篇:PHP會話發送標頭“Cache-Control:no-store,no-cache,must-revalidate”。如何更改為“Cache-Control:s-maxage=10,public,ma
