0

I am receiving the following error while using the php code below:

[error] 7096#0: *410 FastCGI sent in stderr: "PHP message: PHP Fatal error: Using $this when not in object context in /var/www/html_offline/core.php on line 60" while reading response header from upstream, client: [...]

I've already tried to solve this issue but couldn't find a solution.

<?php

class CORE {

    private $db_sqlite;
    private $db_mysql;

    function __construct(){
        $this->db_mysql = $this->get_MySQL();
        $this->db_sqlite = $this->get_SQLite();
    }

    //
    //  MYSQL ADAPTER
    //
    function get_MySQL($db = null) {
        if ($db == null) {
            try {
                $db = new PDO("mysql:host=ip_address:port;dbname=database;charset=utf8","user","password", array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
            } catch(PDOException $ex) {
                return $db;
            }
        }
        return $db;
    }

    //
    //  SQLITE ADAPTER
    //
    function get_SQLite($db = null) {
        if ($db == null) {
            try {
                $db = new PDO("sqlite://var/www/html_offline/ip_throttling.db", null, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                $db->query("CREATE TABLE IF NOT EXISTS storage (IP VARCHAR(255), TIME VARCHAR(255), ID VARCHAR(255))");
            } catch(PDOException $ex) {
                return $db;
            }
        }
        return $db;
    }

    //
    //  IP QUERY RESTRICTION
    //
    function isThrottled($ip, $id) {
        //  SELECT stmt
        $stmt = $this->db_sqlite->prepare('SELECT TIME FROM storage WHERE IP = :ip AND ID = :id');
        $bind = array(
            ':ip' => $ip,
            ':id' => $id
        );
        $stmt->execute($bind);
        if ($stmt->rowCount() > 0) {
            $fetch = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($fetch['time'] <= strtotime('-30 seconds')) {
                return true;
            }
            //  DELETE stmt
            $stmt = $this->db_sqlite->prepare('DELETE FROM storage WHERE IP = :ip AND ID = :id');
            $stmt->execute($bind);
            return false;
        } else {
            return false;
        }
    }

?>
Shadow
  • 33,525
  • 10
  • 51
  • 64
LeVence
  • 41
  • 8
  • 1
    How do you call your class functions? – u_mulder Dec 30 '16 at 14:50
  • where is line 60? – harry Dec 30 '16 at 14:53
  • @DobeLee line 60 is located at "$stmt = $this->db_sqlite->prepare('SELECT TIME FROM storage WHERE IP = :ip AND ID = :id');" in script – LeVence Dec 30 '16 at 14:55
  • Possible duplicate of [upstream sent too big header while reading response header from upstream](http://stackoverflow.com/questions/23844761/upstream-sent-too-big-header-while-reading-response-header-from-upstream) – KhorneHoly Dec 30 '16 at 14:55
  • @u_mulder I'm accessing the core script through other php scripts via. "CORE::get_SQLite()" or "CORE::" in general case. – LeVence Dec 30 '16 at 14:57
  • 1
    Do you understand that using `::` means that no instance of class CORE is created? – u_mulder Dec 30 '16 at 14:59
  • @u_mulder Sorry, I've just replaced those with "$core->[...]" and initialized it at beginning "$core = new CORE();". – LeVence Dec 30 '16 at 15:35
  • Well, I'm receiving the "Call to a member function prepare() on null in /var/www/html_offline/core.php on line 60" error now. Line 60 is still "$stmt = $this->db_sqlite->prepare('SELECT TIME FROM storage WHERE IP = :ip AND ID = :id');" in core script. – LeVence Dec 30 '16 at 15:37
  • Then your SQLite connection did not get initialised. – Shadow Dec 30 '16 at 15:44

0 Answers0