I've run into an error, where global seems to not work properly and can't access variable.
Here's my code:
function getComments($id)
{
global $conn;
$COMPONENT_NAME = "view_company_comments";
include_once "validateUser.php";
Just for context, if COMPONENT_NAME isn't going to be present in some defined list, script execution will stop using die() function.
Now inside "validateUser.php":
(explained everything in comments)
<?php
if (!isset($COMPONENT_NAME)) {
die(json_encode(["error" => "validating user: component was not set."]));
} else {
include_once "permissions.php";
$validateUser_allowedActions = permissionsInitActions();
//So far in, var_dump($COMPONENT_NAME) works properly here, and I get the component name succesfully.
//But watch next:
//"permissionsAllowed()" is a function from "permissions.php",
//this function returns "false" here, expected result is "true"
if (!permissionsAllowed($validateUser_allowedActions)) {
die(json_encode(["error" => $COMPONENT_NAME . ": Unvalidated user privillege."]));
}
}
And inside "permissions.php":
function permissionsAllowed($actions)
{
global $COMPONENT_NAME, $conn;
//Here, var_dump($COMPONENT_NAME) results to "null", which is weird
//because in "validateUser.php" it is a correct string value.
$sql = "SELECT id FROM permission_actions WHERE `name` = '$COMPONENT_NAME'";
$result = mysqli_query($conn, $sql);
$actionID = mysqli_fetch_assoc($result)["id"];
var_dump($COMPONENT_NAME);
if (in_array($actionID, $actions)) {
return true;
}
return false;
}
What is happening here? What am I missing?
Thanks for any help.
globalvariables be an option, if you can I would highly recommend doing so.globaljust by passing the value, but I'm still wondering whether am I making a mistake somewhere in the code logic, or I've just found another reason to hate working with PHP.