- Use
self::$datainstead of$data. Read How to access private variable in static function - Loop Condition to
count($this->dataself::$data) - Undefined index issue for password, as Indexing starts from 0
for ($i = 0,$l=count(self::$data);$i < $l;$i++){ $u = self::$data[$i][0]; // use 0 not 1 for username, as index start from 0 if ($u == $username){ $p = self::$data[$i][1]; // use 1 not 2, as index start from 0 if ($p == $pass){ setcookie("username="+$u);//+";password="+p; echo "Right password"; } else{echo "Wrong password";}; } else{ echo "Can't find this username. Please try a different name."; } } //end for And make your private variable $data to be static like,
private static $data = array( array("TestUser","Password"), array("Admin","BigA1r") ); Full code,
class MainController extends Controller { private static $data = array( array("TestUser","Password"), array("Admin","BigA1r") ); //$this->log($u,$p); public static function log($username,$pass) { //login echo "<title>Processing request...</title>"; echo "Logging in... Please wait."; for ($i = 0,$l=count(self::$data);$i < $l;$i++){ $u = self::$data[$i][0]; if ($u == $username){ $p = self::$data[$i][1]; if ($p == $pass){ setcookie("username="+$u);//+";password="+p; echo "Right password"; } else { echo "Wrong password"; } } else { echo "Can't find this username. Please try a different name."; } } // end for loop } // end function log } // end class MainController