[英]Pass $_SESSION variables to included file
我有第一个PHP文件:/home/www/subdomain1.domain.tld/file.php
<?php
session_start();
$_SESSION['foo']='bar';
include "/home/www/subdomain2.domain.tld/foo2.php";
?>
和/home/www/subdomain2.domain.tld/foo2.php:
<?php
session_start();
echo $_SESSION['foo'];
?>
我认为第一个文件中的“ include”会生成“ 500 Internal Server Error”,这是因为会话变量未传递给包含的文件,我该如何解决?
谢谢亚历克斯
编辑:我必须使用会话变量,以便在subdomain2的每个PHP文件上使用这些变量。
包含文件时无需使用会话。 具有相同名称空间的请求都是相同的。
file.php:
$foo = 'bar';
include 'foo2.php';
foo2.php
echo $foo; // returns 'bar'
您不应该在第二个文件中开始会话。 由于会话是在file.php中启动的,因此foo2.php已经可以使用它。
该错误可能是因为PHP输出警告,表明该会话已经启动。
要进行调试,请添加error_reporting(E_ALL); ini_set('display_errors', 1);
error_reporting(E_ALL); ini_set('display_errors', 1);
到第一个PHP脚本的开头。
您应该能够:
file.php
<?php
session_start();
$_SESSION['foo']='bar';
include "/home/www/subdomain2.domain.tld/foo2.php";
foo2.php
我有第一个PHP文件:/home/www/subdomain1.domain.tld/file.php
和/home/www/subdomain2.domain.tld/foo2.php:
<?php
// session_start(); // remove, do not need this here
echo $_SESSION['foo'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.