1

Any ideas why I'm getting this error?

Parse error: syntax error, unexpected '$config' (T_VARIABLE), expecting function (T_FUNCTION) in D:\xampp\htdocs\Server\User-Side\Resources\Php\SQL\User.php on line 10

<?php

class User {

    $config = p_ini_file('../../../Config.ini'); 

    function addUser($ip, $userdata) {
        $data = explode($user_data);

        //Connect to the MYSQL server (Locally hosted at the moment)
        $SQL = new SQL("127.0.0.1", $config['username'], $config['password']);
        $IP = stripslashes($ip);
        $IP = mysql_real_escape_string($ip);
        $data[0] = stripslashes($data[0]);
        $data[0] = mysql_real_escape_string($data[0]);
        $data[1] = stripslashes($data[1]);
        $data[1] = mysql_escape_string($data[1]);
        $data[2] = stripslashes($data[2]);
        $data[2] = mysql_escape_string($data[2]);


        //Select MYSQL Database
        $SQL->selectDatabase($config['dbname']);

        //Check through SQL Databse for username & password match
        $SQL->query("SELECT * FROM users WHERE ip='$ip'");
        $rows = $SQL->getRows();

        if($rows == 1) {
            //User exists. Error.
            echo "Error: User exists already.";
        } else {
            //User doesn't exist. Add.
            $SQL->query("INSERT INTO users (ip, os, machine_name, java_v) VALUES ('$ip', '$data[0]', '$data[1]', '$data[2]')");
        }
        $SQL->close();      
    }

    function removeUser($ip) {
        //Connect to the MYSQL server (Locally hosted at the moment)
        $SQL = new SQL("127.0.0.1", $config['username'], $config['password']);
        //Select MYSQL Database
        $SQL->selectDatabase($config['dbname']);

        //Check through SQL Databse for username & password match
        $SQL->query("SELECT * FROM users WHERE ip='$ip'");
        $rows = $SQL->getRows();

        if($rows == 1) {
            //User exists. Remove.
            $SQL->query("REMOVE FROM users WHERE ip='$ip'");
        } else {
            //User doesn't exist. Error.
            echo "Error: User doesn't exists.";
        }
        $SQL->close();      
    }

    function heartBeat() {

    }
}
?>
Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
Jake Cross
  • 523
  • 1
  • 6
  • 14
  • 3
    Please include your script directly in the question, formatted using the code button (Ctrl+K). – Ry- May 26 '15 at 00:46

1 Answers1

3

This line is incorrect:

$config = p_ini_file('../../../Config.ini');

You're assigning to a variable, but it's in a class so it needs to be defined as a property of that class. This means prepending the property with one of the keywords public, protected or private.

More information on properties here:

http://php.net/manual/en/language.oop5.properties.php

Additionally you cannot call a function there.

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48