在这里,我试图从类
获得会话数据这是一个示例代码
目录
classes
-- General.php
-- DB.php
templates
-- header.php
-- footer.php
index.php
config.php
GetAPI.php
的config.php
<?php
session_start();
ob_start();
/* Header Set */
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
/* Set Default Timezone */
date_default_timezone_set('Europe/Amsterdam');
/* Set locale information */
setlocale(LC_ALL, 'nl_NL');
/* Get Current URL */
$currentURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
$actual_link = $currentURL.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']).'/';
/* Define Base url it will use in whole website, it treat as global variable */
define('BASE_URL',$actual_link);
/* CURL Configuration */
function getCurl($link){
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,$link);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest"));
curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_handle, CURLOPT_CAINFO, "/etc/pki/tls/cert.pem");
$buffer = curl_exec($curl_handle);
$buffer_decode = json_decode($buffer);
curl_close($curl_handle);
return $buffer_decode;
}
的index.php
<?php
require_once("templates/header.php");
require_once("templates/footer.php");
GetAPI.php
<?php
/* Auto Load all classes */
require 'classes/Autoload.php';
/* Get Title */
function getTitle() {
$general = new General();
return $general->getTitle();
}
if(isset($_REQUEST['func'])):
echo json_encode(call_user_func($_REQUEST['func'], $_REQUEST));
endif;
General.php
<?php
Class General {
private $db;
public function __construct () {
$db = new DB();
$this->db = $db;
}
public function getTitle() {
$_SESSION['data'] = "xyz";
return $this->db->query("select * from general");
}
}
的header.php
<?php
include (__DIR__). "/../config.php";
$_SESSION['xxx'] = "x";
$d = getCurl(BASE_URL.'GetAPI.php?func=getTitle');
var_dump($d);
var_dump($_SESSION);
当我尝试打印$_SESSION
而不是仅显示$_SESSION['xxx']
时,它没有显示$_SESSION['data']
中General.php
的数据。
任何帮助将不胜感激!