[英]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.