i want to check if the user who logged in have role ‘admin’ and his com ‘1’ so put it in session and it is working in every php files correctly but not when crul is happen it get as undefined . so i make file with sessions value and dont know how to fetch data from it
my getData.php :
session_start();
if ($_SESSION['login'] == false) {
echo ' <script> location.replace("../logout.php"); </script>';
}
define("SESSION_FILE", "session_data.txt");
function fetchDataWithSession($url)
{
// Check if the session data file exists and read its contents
if (file_exists(SESSION_FILE)) {
$sessionData = unserialize(file_get_contents(SESSION_FILE));
$_SESSION = is_array($sessionData) ? $sessionData : [];
}
// Initialize cURL session
$ch = curl_init($url);
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Pass the session data in the request headers
$headers = [
'Cookie: ' . http_build_query($_SESSION, '', '; ')
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute cURL session and get the response
$response = curl_exec($ch);
// Check for cURL errors
if (curl_errno($ch)) {
// Handle error
echo 'Curl error: ' . curl_error($ch);
}
// Close cURL session
curl_close($ch);
// Write the updated session data to the file
file_put_contents(SESSION_FILE, serialize($_SESSION));
// End the session
session_write_close();
return $response;
}
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$path1 = rtrim(dirname($_SERVER['REQUEST_URI']), '/');
$path = "$protocol://$host$path1/action.php";
// $sessionData = fetchDataWithSession($url . '?getSessionData=true');
$ispData = fetchDataWithSession($path . '?getIsp=true');
echo $ispData;
die();
my action.php :
the path lead my in this file .
<?php
session_start();
if (isset($_SESSION['role']) && isset($_SESSION['comp'])) {
// Retrieve values of 'role' and 'comp'
$role = $_SESSION['role'];
$comp = $_SESSION['comp'];
} else {
echo 'Session variables "role" or "comp" not set.';
}
// Include the database configuration file
require_once '../db/config.php';
function getIsp($conn)
{
if ($_SESSION['role'] == 'admin' && $_SESSION['comp'] == 1) {
$stmt = $conn->query('SELECT isp_id, isp_name FROM isp');
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return json_encode($result);
} else {
$comp_id = $_SESSION['comp'];
$stmt = $conn->prepare('SELECT isp_id, isp_name FROM comp_isp WHERE comp_id = :comp_id');
$stmt->bindParam(':comp_id', $comp_id, PDO::PARAM_INT); // Assuming comp_id is an integer
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return json_encode($result);
}
}
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
if (isset($_REQUEST['getIsp'])) {
echo getIsp($conn);
}
break;
case 'POST':