simplexml_load_file 和 $_SESSION
我在 PHP 中使用 simplexml_load_file 和会话变量时遇到问题。
因此,我有 2 个 PHP 文件,一个生成一些 XML,另一个读取它。 生成 XML 的文件应该读取一些 $_SESSION 变量,这些变量已在另一个文件中设置,但它不会...
例如,假设第一个文件类似于:
FILE 1(读取 XML)
<?
session_start();
$_SESSION['test'] = 'test!!!';
echo '<b>In file 1</b><br />';
echo 'var = '.$_SESSION['test'].'<br />'; // This correctly outputs "test!!!"
echo '<b>Reading file 2</b><br />';
$xml = simplexml_load_file("http://www.someurl.com/2.php");
echo 'var = '.$xml['var']; // This does <b>NOT</b> output "test!!!"... why?
?>
FILE 2(生成 XML) )
<?
header('Content-type:application/xml');
session_start();
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<test>';
echo '<var>'.$_SESSION['test'].'</var>';
echo '</test>';
?>
奇怪的是,如果我直接打开文件 2,它会读取 $_SESSION["test"]
我已经尝试过的事情(但没有成功)
不在第二个文件中调用 session_start()
在第一个文件中的 simplexml_load_file 之前调用 session_write_close()
使用 fsockopen 而不是 simplexml_load_file 访问文件。这也会返回一个空标签...所以这不是 simplexml_load_file 的问题...
我有点没有想法...任何人都可以帮忙吗?
谢谢 尼科
I have a problem using simplexml_load_file and session vars in PHP.
So, I have 2 PHP files, one generates some XML and the other one reads it.
The file that generates XML should read some $_SESSION vars, which had been set in the other, but it won't...
For instance, say the first file is something like:
FILE 1 (reads XML)
<?
session_start();
$_SESSION['test'] = 'test!!!';
echo '<b>In file 1</b><br />';
echo 'var = '.$_SESSION['test'].'<br />'; // This correctly outputs "test!!!"
echo '<b>Reading file 2</b><br />';
$xml = simplexml_load_file("http://www.someurl.com/2.php");
echo 'var = '.$xml['var']; // This does <b>NOT</b> output "test!!!"... why?
?>
FILE 2 (generates XML)
<?
header('Content-type:application/xml');
session_start();
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<test>';
echo '<var>'.$_SESSION['test'].'</var>';
echo '</test>';
?>
The strange thing is, if I open file 2 directly it DOES read $_SESSION["test"]
Things I already tried (and did not work)
Not calling session_start() in the second file
Calling session_write_close() before simplexml_load_file in the first file
Accessing the file using fsockopen instead of simplexml_load_file. This also returns an empty tag... so it's not an issue with simplexml_load_file...
I'm a bit out of ideas... can anyone help?
Thanks
nico
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从用户/浏览器的角度来看,会话通过包含会话标识符的 cookie 在页面之间传递。
有点像这样:
这里,您有两个页面,但会话标识符不会从文件 1 传递到文件 2 —— 这意味着文件2 甚至不知道存在现有会话。
事实上,您有两个客户端:
From a user/browser point of view, the session is passed from page to page via a cookie that contains the session identifier.
A bit like this :
Here, you have two pages, but the session identifier is not passed from file 1 to file 2 -- which means file 2 doesn't even know there is an existing session.
In fact, you have two clients, here :