简体   繁体   中英

Fatal error: Uncaught Error: Call to a member function

I get this error when trying to serve my PHP code from Apache:

Notice: Undefined variable: COMMUNITY in /var/www/Account/assets/includes/head/nav.php on line 1

Fatal error: Uncaught Error: Call to a member function getUser() on null in /var/www/Account/assets/includes/head/nav.php:1 Stack trace: #0 /var/www/Account/assets/includes/head.php(60): include_once() #1 /var/www/Account/cpanel/community.php(3): include('/var/www/Accoun...') #2 /var/www/Account/assets/classes/core.php(14): require_once('/var/www/Accoun...') #3 /var/www/Account/assets/includes/head.php(7): require_once('/var/www/Accoun...') #4 /var/www/Account/cpanel/home.php(3): include('/var/www/Accoun...') #5 {main} thrown in /var/www/Account/assets/includes/head/nav.php on line 1

On XAMPP in Windows it's working, I don't get the error; but on Linux with Apache doesn't. Both use PHP 7.1


Content in head.php :

<!DOCTYPE html>
<?php
include_once("config/min.php");

session_start();
define('ROOTPATH', __DIR__);
require_once(ROOTPATH.'/../classes/core.php');

if ($USER->is_logged_in())  {
  $stmt = $USER->runQuery("SELECT * FROM users WHERE id=:uid");
  $stmt->execute(array(":uid"=>$_SESSION['userSession']));
  $U_DATA = $stmt->fetch(PDO::FETCH_ASSOC);
}

?>
<html>
  <head>

    <title><?php echo $title; ?></title>

    <?php include_once("head/styles.php"); ?>

    <?php include_once("config/ads.php"); ?>
  </head>

    <body>
    <?php include_once("head/nav.php"); ?>

Content in core.php

<?php

require_once 'init.php';
require_once 'PasswordStorage.php';


require_once 'user.php';
$USER = new USER();

require_once 'community.php';
$COMMUNITY = new COMMUNITY();

?>

Content in nav.php

<?php if (!strpos($_SERVER['REQUEST_URI'], "a/")) { if ($USER->is_logged_in()) { $C_USER = $COMMUNITY->getUser($U_DATA['id']); } ?>
<div class="wrapper">
...

Content in community.php

<?php

class COMMUNITY
{
    private $conn;
    public function __construct() {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    private function request($path) {
        $url = "https://community.makeroid.io";
        $api_key = "XXX";
        $api_user = "system";
        $request = file_get_contents($url."/".$path."?api_key=".$api_key."&api_username=".$api_user);
        if ($request) {
            return json_decode($request, true);
        } else {
            return false;
        }

    }

    public function getUser($id) {
        $stmt = $this->conn->prepare("SELECT * FROM c_users WHERE u_id=:id;");
        $stmt->bindparam(":id",$id);
        $stmt->execute();
        $user = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if (count($user) == 1) {
            return $user[0];
        } else {
            return null;
        }
    }

    public function verifyUser($username, $id, $email) {
        $emails = $this->request("users/".$username."/emails.json");
        if ($emails) {
            if ($emails['email'] == $email) {
                $stmt = $this->conn->prepare("INSERT INTO c_users (u_id, username) VALUES(:uID, :username);");
                $stmt->bindparam(":uID",$id);
                $stmt->bindparam(":username",$username);
                $stmt->execute();
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function getUserData($username) {
        $user = $this->request("users/".$username.".json");
        return $user['user'];
    }

    public function getProfilePicture($username) {
        $user = $this->request("users/".$username.".json");
        return "https://community.makeroid.io".$user['user']['avatar_template'];
    }

    public function getId($username) {
        $user = $this->request("users/".$username.".json");
        return $user['user']['id'];
    }

    public function getTrustLevel($username) {
        $user = $this->request("users/".$username.".json");
        return $user['user']['trust_level'];
    }

    public function getTrustLevelRequirements($id) {
        $tl3_requirements = $this->request("admin/users/".$id.".json");
        if ($tl3_requirements['tl3_requirements']) {
            return $tl3_requirements['tl3_requirements'];
        } else {
            return false;
        }
    }
}

?>

Why the $USER and $COMMUNITY aren't shared?

I've finally fixed by setting in core.php

<?php
require_once(ROOTPATH.'/../classes/user.php');
$USER = new USER();

require_once(ROOTPATH.'/../classes/community.php');
$COMMUNITY = new COMMUNITY();

It seems like it wasn't getting the proper file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM